DateTime column type becomes String type after deserializing DataTable

The basic problem here is that Json.NET’s DataTableConverter infers each DataColumn.DataType by looking at token values present in the first row only. It works this way because it streams the JSON for the table in rather than loading the entirety into an intermediate JToken hierarchy. While streaming gives better performance with reduced memory use, it … Read more

How to apply ObjectCreationHandling.Replace to selected properties when deserializing JSON?

You have a few alternatives to force your list to be replaced rather than reused: You can add an attribute to the list property indicating that it should be replaced not reused: public class Configuration { [JsonProperty(ObjectCreationHandling = ObjectCreationHandling.Replace)] public List<Tuple<int, int, int>> MyThreeTuple { get; set; } } You can use an array instead … Read more

Repeated serialization and deserialization creates duplicate items

The reason this is happening is due to the combination of two things: Your class constructors automatically add default items to their respective lists. Json.Net calls those same constructors to create the object instances during deserialization. Json.Net’s default behavior is to reuse (i.e. add to) existing lists during deserialization instead of replacing them. To fix … Read more

JObject.Parse modifies end of floating point values

@Ilija Dimov is correct–JSON.NET parses JSON floats as doubles by default. If you still want to use JObject instead of creating a full blown POCO for deserialization, you can use a JsonTextReader and set the FloatParseHandling option: var reader = new JsonTextReader(new StringReader(clientString)); reader.FloatParseHandling = FloatParseHandling.Decimal; JObject obj = JObject.Load(reader); Console.WriteLine(obj[“max”].Value<decimal>()); // 1214.704958677686

Accessing properties with a dot in their name

You could create a root class to deserialize into and use JsonProperty public class Root { // Use the proper type instead of object [JsonProperty(PropertyName = “en.pickthall”)] public IEnumerable<object> EnPickthall { get; set; } public Root() { } } Used as follows Root stuff = JsonConvert.DeserializeObject<Root>(result); foreach(var x in stuff.EnPickthall) { }

Deserialize json that has some property name starting with a number

You should use JSON.NET or similar library that offers some more advanced options of deserialization. With JSON.NET all you need is adding JsonProperty attribute and specify its custom name that appears in resulting JSON. Here is the example: public class MyClass { [JsonProperty(PropertyName = “24hhigh”)] public string Highest { get; set; } … Now to … Read more

Modify existing object with new partial JSON data using Json.NET

You want JsonSerializer.Populate() or its static wrapper method JsonConvert.PopulateObject(): Populates the JSON values onto the target object. For instance, here it is updating an instance of your Calendar class: public static class TestPopulate { public static void Test() { var calendar = new Calendar { Id = 42, CoffeeProvider = “Espresso2000”, Meetings = new[] { … Read more

tech