12

How can I make JSON.NET return a decimal instead of a double when parsing floats from JSON? (it's an object with a Dictionary<string,object> that is serialized).

I've tried writing a JsonConverter but the CanConvert method doesn't get called with a Double type in order to try and convert it. There's other code regarding overriding JsonTextReader but this doesn't seem possible in the latest versions of Json.Net.

James Crowley
  • 3,911
  • 5
  • 36
  • 65
  • How are you reading the JSON? I'd expect if you had a class defined, with a `decimal` property, and used the `JsonConvert.DeserializeObject` method, it would deserialize just fine as a decimal. – Joe Enos Mar 25 '13 at 14:58
  • Ah, sorry - Markdown munged my description - it's an object with IDictionary – James Crowley Mar 25 '13 at 15:01

1 Answers1

19

In Json.NET 5.0, Newtonsoft.Json.JsonSerializerSettings class has the new property FloatParseHandling, you can set this property value Newtonsoft.Json.FloatParseHandling.Decimal

Jens Kloster
  • 11,099
  • 5
  • 40
  • 54
  • 1
    Any hints on how to do this for a particular class or property? I.e. not to impact the global deserialization settings (also when I don't have access to those), but override this behavior in very specific places. – Hilarion Sep 19 '19 at 10:46