BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News Architectural Changes in JSON.NET 5

Architectural Changes in JSON.NET 5

This item in japanese

Newtonsoft’s JSON.NET has received a pair of architectural updates to make it easier to use in projects that involve third party libraries. The two features are known as DefaultSettings and Extension Data.

In JSON.NET 5r5, developers are allowed to override the serialization/deserialization options using the JsonSerializerSettings class. This is normally passed to the JsonConvert methods each time it is called, or used to create a new JsonSerializer.

Libraries that want to opt-in to the new default scheme can use JsonSerializer.CreateDefault. This will use the defaults the application has defined via the JsonConvert.DefaultSettings method. Libraries that don’t want to this behavior can continue creating instances of JsonSerializer normally.

It should be noted that JsonConvert.DefaultSettings accepts a Func<JsonSerializerSettings> rather than an instance of JsonSerializerSettings. This means a new copy of the default JsonSerializerSettings will be created each time it is needed.

The other change is support for extension data. This is data that is available in the raw JSON payload but isn’t otherwise represented in the class structure. Usually this occurs when the server definition changes but the client hasn’t been updated to match.

In WCF, extension data is made available via the IExtensibleDataObject interface. This offers a single property of type ExtensionDataObject, an opaque class that is nearly useless. Any data contained with the ExtensionDataObject is unavailable except to a data contract serializer. This means it only comes into play in those exceedingly rare cases where you serialize some data, deserialize it with an olded version of the code, reserialize, and then deserialize it with the original version again.

JSON.Net exposes its extension data as a dictionary of strings and JToken objects. This dictionary is exposed via a property or field tagged with the [JsonExtensionData] attribute. By using an attribute instead of an interface, developers are allowed to maintain encapsulation by marking the field as private.

Rate this Article

Adoption
Style

BT