Architectural Changes in JSON.NET 5

by Jonathan Allen on May 09, 2013 |

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


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.

Tell us what you think

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

Email me replies to any of my messages in this thread

ServiceStack.Text by Chuck Wagner

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

Email me replies to any of my messages in this thread

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

Email me replies to any of my messages in this thread

1 Discuss
General Feedback
Marketing and all content copyright © 2006-2016 C4Media Inc. hosted at Contegix, the best ISP we've ever worked with.
Privacy policy

We notice you're using an ad blocker

We understand why you use ad blockers. However to keep InfoQ free we need your support. InfoQ will not provide your data to third parties without individual opt-in consent. We only work with advertisers relevant to our readers. Please consider whitelisting us.