BT
x Your opinion matters! Please fill in the InfoQ Survey about your reading habits!

Json.NET Updates: Merge, Dependency Injection, F# and JSONPath Support

by Roopesh Shenoy on Aug 11, 2014 |

Json.NET 6.0 received 4 releases this year, the latest last week. Over these releases, several new features have been added, including several F# specific features, support for JSONPath querying, ability to integrate with Dependency Injection frameworks, ability to Merge JSON objects and more.

Serializers shipped with the .NET framework, the Data Contract JSON Serializer and the JavaScriptSerializer, leave room for improvement both on features and performance front. Json.NET fills this gap and is already a popular open-source alternative within the .NET community. It comes with several features such as LINQ-to-JSON, BSON support, support for anonymous types, support for popular ORMs (EF and NHiberate) and more, not present in the built-in counterparts. This, combined with better performance. (see the feature comparison and detailed benchmarks)

Some important features introduced this year that make Json.NET even better -

  • JSON Merge - allows you to merge a JSON object or an array into another.

    This is similar to the jQuery's extend method, but is also recursive and gives more options in handling arrays - when merging arrays (including those nested within objects), you can specify whether they should be concatenated together, unioned, merged by position, or completely replaced.

    Merge also works with LINQ-to-JSON. Have a look at the tests to understand this feature better and the various use cases it supports.
  • Integration with Dependency Injection - you can now deserialize interfaces or abstract types directly if you specify a mechanism to resolve such a type at runtime, such as using a dependency injection framework like Autofac or Ninject. All you have to do is inherit from the DefaultContractResolver, and use your own resolver in the serializer.
  • Several F# specific features - support for discriminated unions, F# collections, F# lists, sequences, sets and maps. For new immutable collections, if collection of T has a constructor that takes IEnumerable<T> then Json.NET will automatically work when deserializing to that collection.
  • Ability to handle metadata properties, without having to guarantee JSON object property order. Json.NET uses metadata for some features such as preserving types or references and requires the metadata to be ordered before other properties for these features to work fine. A setting called MetadataPropertyHandling.ReadAhead can be set to enable this, at a slight cost of increased memory usage and lesser speed. 
  • Full support for JSONPath querying. Similar to XPath, this is useful for navigating JSON data and can be useful if you only need specific values without necessarily deserializing the entire JSON object.

Other interesting features are ability to deserialize nested DataTables as well as arrays within DataTables, support for single-line comments within JSON, support for Windows Phone 8.1 and more. Refer to posts from James Newton-King for more details.

Another interesting and performant third-party alternative for serialization in .NET is ServiceStack.Text (now commercial open source).

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
Community comments

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

Discuss

Educational Content

General Feedback
Bugs
Advertising
Editorial
InfoQ.com and all content copyright © 2006-2014 C4Media Inc. InfoQ.com hosted at Contegix, the best ISP we've ever worked with.
Privacy policy
BT