Json.NET 4.5 Release 8 – Multidimensional Array Support, Unicode Improvements

Multidimensional Array Support

Json.NET now supports serializing and deserializing multidimensional arrays. There isn't anything you need to do, if one of your types has a multidimensional array property It Just Works™.

string[,] famousCouples = new string[,]
  {
    { "Adam", "Eve" },
    { "Bonnie", "Clyde" },
    { "Donald", "Daisy" },
    { "Han", "Leia" }
  };
 
string json = JsonConvert.SerializeObject(famousCouples, Formatting.Indented);
// [
//   ["Adam", "Eve"],
//   ["Bonnie", "Clyde"],
//   ["Donald", "Daisy"],
//   ["Han", "Leia"]
// ]
 
string[,] deserialized = JsonConvert.DeserializeObject<string[,]>(json);
 
Console.WriteLine(deserialized[3, 0] + ", " + deserialized[3, 1]);
// Han, Leia

Unicode Performance Improvements

Prior versions of Json.NET allocated a new array and string object to the heap for every Unicode character; a potentially expensive situation if your application is writing a lot of Unicode characters.

This release improves Unicode serialization so that Json.NET will only allocate a single array for all Unicode text and then writes the text directly to the underlying output stream. These changes improve Json.NET performance of Unicode intensive objects by about 20% and significantly reduces the time spent doing garbage collection.

Changes

Here is a complete list of what has changed since Json.NET 4.5 Release 7.

  • New feature - Serialize and deserialize multidimensional arrays
  • New feature - Members on dynamic objects with JsonProperty/DataMember will now be included in serialized JSON
  • New feature - LINQ to JSON load methods will read past preceding comments when loading JSON
  • New feature - Improved error handling to return incomplete values upon reaching the end of JSON content
  • Change - Improved performance and memory usage when serializing Unicode characters
  • Change - The serializer now creates objects using GetUninitializedObject when deserializing a Serializable type
  • Fix - Added SecurityTransparent attribute to WinRT build
  • Fix - Fixed infinite loop error caused by error handling upon reaching the end of JSON content

Links

Json.NET CodePlex Project

Json.NET 4.5 Release 8 Download – Json.NET source code and assemblies

Json.NET 4.5 Release 5 – JsonProperty enhancements

JsonProperty enhancements

JsonPropertyAttribute now has options on it to customize a property’s collection items. When ItemConverter, ItemIsReference, ItemTypeNameHandling or ItemReferenceLoopHandling is set on JsonProperty and the property’s type is a collection then those settings will be applied to every collection item.

public class Event
{
  public string EventName { get; set; }
  public string Venue { get; set; }
 
  [JsonProperty(ItemConverterType = typeof(JavaScriptDateTimeConverter))]
  public IList<DateTime> Performances { get; set; }
}

Usage:

Event e = new Event
  {
    EventName = "Blackadder III",
    Venue = "Gryphon Theatre",
    Performances = new List<DateTime>
      {
        DateTime.Parse("8 Tue May 2012, 6:30pm"),
        DateTime.Parse("9 Wed May 2012, 6:30pm"),
        DateTime.Parse("10 Thu May 2012, 8:00pm")
      }
  };
 
string json = JsonConvert.SerializeObject(e, Formatting.Indented);
//{
//  "EventName": "Blackadder III",
//  "Venue": "Gryphon Theatre",
//  "Performances": [
//    new Date(1336458600000),
//    new Date(1336545000000),
//    new Date(1336636800000)
//  ]
//}    

Changes

Here is a complete list of what has changed since Json.NET 4.5 Release 4.

  • New feature - Added ItemIsReference, ItemReferenceLoopHandling, ItemTypeNameHandling, ItemConverterType to JsonPropertyAttribute
  • New feature - Added ItemRequired to JsonObjectAttribute
  • New feature - Added Path to JsonWriterException
  • Change - Improved deserializer call stack memory usage
  • Change - Moved the PDB files out of the NuGet package into a symbols package
  • Fix - Fixed infinite loop from an input error when reading an array and error handling is enabled
  • Fix - Fixed a base object error not being handled when deserializing

Links

Json.NET CodePlex Project

Json.NET 4.5 Release 5 Download – Json.NET source code, documentation and binaries

Json.NET 4.5 Release 4 – Portable Class Library Build

They took my dressing room, my parking space, even my writer, so I don't have a funny third item.Portable Class Library Build

Json.NET now has a Portable Class Library build that can be found in the /Bin/Portable directory from the zip download on CodePlex. The portable build targets the absolute basics (.NET 4.0, SL4, WP7, Metro) and so hopefully should run on just about everything.

Improved Collection Serialization

JsonArrayAttribute and JsonDictionaryAttribute now have properties to customize the serialization of collection items. ItemConverter, ItemTypeNameHandling, ItemReferenceLoopHandling and ItemIsReference will apply to all the items of that list/dictionary when serializing and deserializing.

Changes

  • New feature – Added Portable Class Library build
  • New feature - Added support for customizing the JsonConverter, type name handling and reference handling of collection items
  • New feature - Added Path to JsonReaderException/JsonSerializationException messages
  • New feature - Added DateParseHandling to JsonReader
  • New feature - Added JsonContainerContract
  • New feature - Added JsonDictionaryAttribute
  • Change – Instances of Exception have been changed to be a more specific exception
  • Fix – Fixed Windows Application Certification tool error by removing AllowPartiallyTrustedCallersAttribute
  • Fix - Fixed JsonWriters not using DateTimeZoneHandling

Links

Json.NET CodePlex Project

Json.NET 4.5 Release 4 Download – Json.NET source code, documentation and binaries

Json.NET 4.5 Release 2 – Serializable support and bug fixes

Serializable Support

Json.NET now detects types that have the SerializableAttribute and serializes all the fields on that type, both public and private, and ignores the properties. This is useful when you’re interested in round-tripping the data on a type and don’t care what the JSON looks like.

If you are serializing types that have the attribute and don’t want the new behaviour, it can either be overridden on a type using the JsonObjectAttribute or disabled globally by setting IgnoreSerializableAttribute on DefaultContractResolver to true.

Update: IgnoreSerializableAttribute is true by default in release 3. If you want it you can still achieve this effect by either setting IgnoreSerializableAttribute to false or adding [JsonObject(MemberSerialization.Fields)] to your class.

Bug fixes

Interesting bug fixes this release include correctly reading JSON from slow streams (e.g. a network stream that is only returning one character at a time), fixing invalid Unicode surrogate characters, and ignoring property order when comparing two JObjects (JavaScript objects aren’t ordered and so could come back serialized in a different order).

Changes

  • New feature - Added support for the SerializableAttribute and serializing a type's internal fields
  • New feature - Added MaxDepth to JsonReader/JsonSerializer/JsonSerializerSettings
  • New feature - Added support for ignoring properties with the NonSerializableAttribute
  • Fix - Fixed deserializing a null string throwing a NullReferenceException
  • Fix - Fixed JsonTextReader incorrectly reading from a slow stream
  • Fix - Fixed CultureInfo not being overridden on JsonSerializerProxy
  • Fix - Fixed full trust security check in .NET 2.0 & .NET 3.5
  • Fix - Fixed XmlNodeConverter not turning all attribute properties into attributes
  • Fix - Fixed comparing JObjects to ignore property order
  • Fix - Fixed reading invalid Unicode surrogate pairs

Links

Json.NET CodePlex Project

Json.NET 4.5 Release 2 Download – Json.NET source code, documentation and binaries