Deserialize using JSON.net

Your classes are pretty close, it looks like you possibly tried to pretty things up a bit such as changing codes to Codes but in so doing the properties no longer match. You can change class names but not property names (at least not that way): Public Class CodeLinkContainer <JsonProperty(“codes”)> Public Property Codes As IList(Of … Read more

JsonConvert.Deserializer indexing issues

Since this is a known behavior of Json.NET, as noted by this answer, a custom JsonConverter can be used when deserializing a stack that pushes items on in the correct order. The following universal converter can be used with Stack<T> for any T: /// <summary> /// Converter for any Stack<T> that prevents Json.NET from reversing … Read more

How to apply a general rule for remapping all property names when serializing with Json.NET?

Assuming you are working with Json.NET 9.0.1 or later, this can be done with a custom NamingStrategy. For instance, here’s one based on SnakeCaseNamingStrategy and StringUtils.ToSnakeCase() by James Newton-King: public class CustomNamingStrategy : NamingStrategy { public CustomNamingStrategy(bool processDictionaryKeys, bool overrideSpecifiedNames) { ProcessDictionaryKeys = processDictionaryKeys; OverrideSpecifiedNames = overrideSpecifiedNames; } public CustomNamingStrategy(bool processDictionaryKeys, bool overrideSpecifiedNames, bool processExtensionDataNames) … Read more

Json.NET JSONPath query not returning expected results

Json.NET’s JSONPath parser allows the filter (script) expression [?( )] to query for nested properties inside child objects of the candidate array item(s). Thus var test = json.SelectTokens(@”$.projects[?(@.client.code == ‘DEF’)].client”); returns, as desired, [ { “code”: “DEF”, “name”: “Client 2” } ] Working .Net fiddle. Upon experimentation it seems none of the JSONPath implementations at … Read more

JSON.net (de)serialize untyped property

If you serialize your class with TypeNameHandling.All or TypeNameHandling.Auto, then when the UntypedProperty property would be serialized as a JSON container (either an object or array) Json.NET should correctly serialize and deserialize it by storing type information in the JSON file in a “$type” property. However, in cases where UntypedProperty is serialized as a JSON … Read more

JSON deserialization – Map array indices to properties with JSON.NET

You can do this with a JsonConverter. A simple converter for this purpose would be: public class PersonConverter : JsonConverter { public override bool CanConvert(Type objectType) { return objectType == typeof(Person); } public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { if (reader.TokenType == JsonToken.Null) return null; var array = JArray.Load(reader); var … Read more

Json.net `JsonConstructor` constructor parameter names

When Json.NET invokes a parameterized constructor, it matches JSON properties to constructor arguments by name, using an ordinal case-ignoring match. However, for JSON properties that also correspond to type members, which name does it use – the member name, or the override type member name specified by JsonPropertyAttribute.PropertyName? It appears you are hoping it matches … Read more

Serializing an interface/abstract object using NewtonSoft.JSON

To enable output of $type information at the root level for a polymorphic object with TypeNameHandling.Auto, use the following overload: JsonConvert.SerializeObject Method (Object, Type, JsonSerializerSettings). From the docs: public static string SerializeObject( Object value, Type type, JsonSerializerSettings settings ) type Type: System.Type The type of the value being serialized. This parameter is used when TypeNameHandling … Read more

Cannot deserialize the current JSON array (e.g. [1,2,3])

I think the problem you’re having is that your JSON is a list of objects when it comes in and it doesnt directly relate to your root class. var content would look something like this (i assume): [ { “id”: 3636, “is_default”: true, “name”: “Unit”, “quantity”: 1, “stock”: “100000.00”, “unit_cost”: “0” }, { “id”: 4592, … Read more

tech