Json.NET 6.0 Release 3 - Serialize All The F#

MOAR F#

Json.NET 6.0 added support for F# discriminated unions - this release adds support for F# collections. F# lists, sequences, sets and maps now serialize and deserialize automatically.

type Movie = {
    Name : string
    Year: int
}
 
[<EntryPoint>]
let main argv = 
 
    let movies = [
        { Name = "Bad Boys"; Year = 1995 };
        { Name = "Bad Boys 2"; Year = 2003 }
    ]
 
    let json = JsonConvert.SerializeObject(movies)
 
    let deserializedMovies = JsonConvert.DeserializeObject<Movie list>(json)
 
    deserializedMovies |> List.iter (fun x -> printfn "Name: %s, Year: %d" x.Name x.Year)
    // Name: Bad Boys, Year: 1995
    // Name: Bad Boys 2, Year: 2003
 
    Console.ReadKey() |> ignore
    0

To all future creators of immutable .NET collections: If your collection of T has a constructor that takes IEnumerable<T> then Json.NET will automatically work when deserializing to your collection, otherwise you're all out of luck.

Metadata Property Handling

Some Json.NET serializer features like preserving types or references require Json.NET to read and write metadata properties, e.g. $type, $id and $ref. Because of the way Json.NET deserialization works these metadata properties have had to be ordered first in a JSON object. This can cause problems because JSON object properties can't be ordered in JavaScript and some other JSON frameworks.

This release adds a new setting to allow metadata properties to be located anywhere in an object: MetadataPropertyHandling.ReadAhead

string json = @"{
    'Name': 'James',
    'Password': 'Password1',
    '$type': 'MyNamespace.User, MyAssembly'
}";
 
object o = JsonConvert.DeserializeObject(json, new JsonSerializerSettings
{
    TypeNameHandling = TypeNameHandling.All,
    // $type no longer needs to be first
    MetadataPropertyHandling = MetadataPropertyHandling.ReadAhead
});
 
User u = (User)o;
 
Console.WriteLine(u.Name);
// James

Internally this setting will instruct the serializer to load the entire JSON object into memory. Metadata properties will then be read out of the object, and then deserialization will continue as normal. There is a slight cost in memory usage and speed but if you require a feature that uses metadata properties and can't guarantee JSON object property order then you will find this useful.

And The Rest

DateFormatString is now used as a fallback when parsing dates during deserialization, lots of bug fixes, and a couple of small but significate performance improvements.

Changes

Here is a complete list of what has changed since Json.NET 6.0 Release 2.

  • New feature - Added MetadataPropertyHandling
  • New feature - Added support for reading MS format JSON dates to ReadAsDateTime
  • New feature - Added support for serializing F# lists, sets and maps
  • New feature - Added support for XML document type
  • Change - Blank XML elements will be written as an empty string instead of null
  • Change - JValue with a null value will be written as null instead of empty string
  • Change - DateFormatString is now used when reading JSON
  • Fix - Fixed deserializing null values with extension data
  • Fix - Fixed converting certain XML namespaces to JSON
  • Fix - Fixed error with whitespace only JSONPath
  • Fix - Fixed property query path that starts with $
  • Fix - Fixed array query path with open brace after $
  • Fix - Fixed parsing certain JSON with comments into a JObject
  • Fix - Fixed bug where matching JSONPath incorrectly raises an error
  • Fix - Fixed non-public base class properties being used instead of child class properties
  • Fix - Fixed hiding generic properties sometimes not being detected
  • Fix - Fixed potential race condition serializing F# objects
  • Fix - Fixed schema divisible sometimes incorrectly validating to false
  • Fix - Fixed not calling virtual ShouldSerialize methods
  • Fix - Fixed invalid cast with DateParseHandling.DateTimeOffset and IsoDateTimeConverter
  • Fix - Fixed StringEnumConverter thread safety
  • Fix - Fixed using FloatParseHandling.Decimal with XmlNodeConverter
  • Fix - Fixed using DateParseHandling.DateTimeOffset with XmlNodeConverter
  • Fix - Fixed type name handling when a property already has a base type assigned

Links

Json.NET GitHub Project

Json.NET 6.0 Release 3 Download - Json.NET source code and assemblies

Json.NET 6.0 Release 1 - JSONPath and F# Support

JSONPath

Json.NET has supported basic path queries with SelectToken since forever. Json.NET 6.0 supes up SelectToken with full support for JSONPath, an XPath like querying language for JSON.

JObject o = JObject.Parse(@"{
  ""Manufacturers"": [
    {
      ""Name"": ""Acme Co"",
      ""Products"": [
        {
          ""Name"": ""Anvil"",
          ""Price"": 50
        }
      ]
    },
    {
      ""Name"": ""Contoso"",
      ""Products"": [
        {
          ""Name"": ""Elbow Grease"",
          ""Price"": 99.95
        },
        {
          ""Name"": ""Headlight Fluid"",
          ""Price"": 4
        }
      ]
    }
  ]
}");
 
// manufacturer with the name 'Acme Co'
var acme = o.SelectToken("$.Manufacturers[?(@.Name == 'Acme Co')]");
 
Console.WriteLine(acme);
// { "Name": "Acme Co", Products: [{ "Name": "Anvil", "Price": 50 }] }

A SelectTokens (plural) method has been added for returning a range of results from a JSONPath query.

// name of all products priced 50 and above
var pricyProducts = o.SelectTokens("$..Products[?(@.Price >= 50)].Name");
 
Console.WriteLine(pricyProducts);
// Anvil
// Elbow Grease

While LINQ to JSON offers more features and flexibility, JSONPath being string based makes it a good choice for persisting a queries or constructing dynamic queries.

F# Support

Json.NET 6.0 adds support for serializing and deserializing F# discriminated unions. There is nothing you need to do, F# discriminated unions will now Just Work.

type Shape =
    | Rectangle of width : float * length : float
    | Circle of radius : float
    | Empty
 
[<EntryPoint>]
let main argv = 
 
    let shape1 = Rectangle(1.3, 10.0)
 
    let json = JsonConvert.SerializeObject(shape1)
    // {
    //   "Case": "Rectangle",
    //   "Fields": [
    //     1.3,
    //     10.0
    //   ]
    // }
 
    let shape2 = JsonConvert.DeserializeObject<Shape>(json)
 
    Console.ReadKey() |> ignore
    0

Assembly Version Happenings

Json.NET has had a static assembly version since 4.5 to avoid binding redirects. The problem with having a static assembly version is if a strongly named assembly with the same version number is found in the GAC, the GAC version will be used ahead for the /bin version. Some people have been encountering the problem that their applications break when someone else GACs an old Json.NET 4.5 on their server. I’m looking at you .NET CMSes.

The plan going forward is to increase the assembly version with major Json.NET releases. 6.0 Release 1 –> 6.0.0.0, 6.0 Release 2 –> 6.0.0.0, 7.0 Release 1 –> 7.0.0.0. Hopefully this will provide a balance between binding redirects and having the GAC ruin your day.

And The Rest

Tons of smaller features like parsing single line comments in JSON, reading multiple pieces of JSON content from a stream with one JsonReader, obsoleting of bad methods and lots of bug fixes.

Changes

Here is a complete list of what has changed since Json.NET 5.0 Release 8.

  • New feature - Added support for JSONPath
  • New feature - Added support for serializing F# discriminated unions
  • New feature - Added support for deserializing nested DataTables and arrays in DataTables
  • New feature - Added support for reading multiple pieces of JSON with a JsonReader
  • New feature - Added AllowIntegerValues setting to StringEnumConverter
  • New feature - Added Decimal and DateTimeOffset constructors to JValue
  • New feature - Added support for reading JSON single line comments
  • New feature - Improved number parsing error messages
  • Change - Changed assembly version to 6.0.0.0
  • Change - .NET 4 Portable build targets MonoTouch and MonoDroid in NuGet package
  • Change - .NET 4 Portable build targets WP8 and SL5 instead of WP7 and SL4
  • Removed - DefaultMemberSearchFlags on DefaultContractResolver is obsolete
  • Removed - SerializeObjectAsync, DeserializeObjectAsync, PopulateObjectAsync on JsonConvert are obsolete
  • Fix - Fixed JObject ICustomTypeDescriptor properties returning incorrect value
  • Fix - Fixed error when casting dynamic base64 string to byte array
  • Fix - Fixed EntityKeyMemberConverter not using property name resolve
  • Fix - Fixed serializing JValues with readonly JsonConverters
  • Fix - Fixed formatting override on SerializeObject methods
  • Fix - Fixed error when wrapping an exception in a JsonConverter
  • Fix - Fixed using extension data with a non-default constructor
  • Fix - Fixed Uri serialization roundtripping with Uri.OriginalString
  • Fix - Fixed TypeNameHandling.Auto with JsonSerializer.Serialize inside a JsonConverter

Links

Json.NET GitHub Project

Json.NET 6.0 Release 1 Download - Json.NET source code and assemblies

Fixing JArray.GetEnumerator() Method Not Found Bug

Getting this method not found error requires a rare combination of factors. If you haven’t seen it then feel free to ignore this blog post.

tl;dr; just tell me how to fix it

If you’re an end user and you get this error then make sure the version of Json.NET your application is loading is 5.0.8. If you have 5.0.8 in your \bin directory and you still get this error then check the GAC as well and update it if necessary.

If you’re a package author and a user reports getting this error from your code then downgrade the version of Json.NET your package is using to 5.0.4, recompile and release a new version of your package. If you can’t downgrade then another option is to add an IEnumerable<JToken> cast to the erroring foreach loop.

foreach (JToken item in (IEnumerable<JToken>)array)
{
  // stuff
}

Another option is to change the foreach loop to a for loop.

The Cause

In Json.NET 5.0.5 I changed JArray.GetEnumerator’s visibility from interface explicit to public. The side effect of GetEnumerator being public is the C# compiler will no longer add a IEnumerable<JToken> cast to foreach loops. The cast is required when GetEnumerator is interface explicit and is only accessible when the object is cast to IEnumerable<JToken>.

The error then occurs when an application or package that has a foreach loop over a JArray and is compiled with a public GetEnumerator is run using an older version of Json.NET, possible out of the GAC, where GetEnumerator is not public. Because there is no cast to IEnumerable<JToken> then .NET can’t find the GetEnumerator method and an exception is thrown.

Json.NET 6.0 Long Term Fix

Rather than have this bug keep popping up for users I’m going to change JArray.GetEnumerator’s visibility back to interface explicit – the visibility it had in Json.NET 5.0.4 and earlier. Because this is a binary breaking change I’m going to increase Json.NET’s version number to 6.0.

Update: I have left GetEnumerator as public and updated Json.NET's assembly version number to 6.0.0.0 instead.

Sorry about this bug. It has sprung up because of a rare combination of factors and unfortunately Json.NET meets all of them.

Json.NET 5.0 Release 7 – Immutable Collections

Immutable Collections

The biggest new feature in Json.NET 5.0.7 is support for serializing and deserializing the offical new .NET Immutable Collections types.

string json = @"[
  'Volibear',
  'Teemo',
  'Katarina'
]";
 
// deserializing directly to an immutable collection, what sorcery is this?!
ImmutableList<string> champions = JsonConvert.DeserializeObject<ImmutableList<string>>(json);
 
Console.WriteLine(champions[0]);
// Volibear

There is nothing you need to do to make immutable collection and Json.NET work together. Upgrade to Json.NET 5.0 Release 7, add the immutable collections NuGet package to your project and you can start using immutable collections with Web API, SignalR or directly from Json.NET like the example above.

Round-trip Extension Data

Extension data is now written when an object is serialized. Reading and writing extension data makes it possible to automatically round-trip all JSON without adding every property to the .NET type you’re deserializing to. Only declare the properties you’re interested in and let extension data do the rest.

public class CustomerInvoice
{
  // we're only modifing the tax rate
  public decimal TaxRate { get; set; }
 
  // everything else gets stored here
  [JsonExtensionData]
  private IDictionary<string, JToken> _additionalData;
}
string json = @"{
  'HourlyRate': 150,
  'Hours': 40,
  'TaxRate': 0.125
}";
 
var invoice = JsonConvert.DeserializeObject<CustomerInvoice>(json);
 
// increase tax to 15%
invoice.TaxRate = 0.15m;
 
string result = JsonConvert.SerializeObject(invoice);
// {
//   'TaxRate': 0.15,
//   'HourlyRate': 150,
//   'Hours': 40
// }

Using extension data to round-trip JSON like this also means you don’t need to worry about third-party sources adding additional JSON because it will automatically be preserved when serializing/deserializing. Nifty.

If you don’t want extension data serialized (or deserialized) then disable that functionality by setting WriteData and ReadData properties on ExtensionDataAttribute to false.

Bug fixes

A couple of bugs crept into Json.NET after the flurry of releases earlier in the year. I have consulted with other developers and the consensus was that bugs are bad so this release fixes all known bugs.

Changes

Here is a complete list of what has changed since Json.NET 5.0 Release 6.

  • New feature - Added support for Immutable Collections
  • New feature - Added WriteData and ReadData settings to DataExtensionAttribute
  • New feature - Added reference and type name handling support to extension data
  • New feature - Added default value and required support to constructor deserialization
  • Change - Extension data is now written when serializing
  • Fix - Added missing casts to JToken
  • Fix - Fixed parsing large floating point numbers
  • Fix - Fixed not parsing some ISO date timezones
  • Fix - Fixed schema validation of integer value when type was number
  • Fix - Fixed writing of IConvertible values when TypeCode returned was Object
  • Fix - Fixed serializing proxy objects with no default constructor

Links

Json.NET CodePlex Project

Json.NET 5.0 Release 7 Download – Json.NET source code and assemblies