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

Bookmarks

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

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Community comments

  • ServiceStack.Text

    by Chuck Wagner,

    Your message is awaiting moderation. Thank you for participating in the discussion.

    ServiceStack.Text still seems to be the best performing option for handling JSON

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

BT