Archives
-
Json.NET 4.0 Release 4 – Bug fixes
This release contains bug fixes and some minor changes.
Custom type names with TypeNameHandling
Note that support for custom type names was added in a previous Json.NET release.
One commonly asked for feature is the ability to customize the value of the $type property when using TypeNameHandling. Fortunately new in .NET 4 is the virtual SerializationBinder.BindToName method that translates a Type to a custom string.
To customize the $type property in Json.NET just create a class that inherits from SerializationBinder and override the BindToName and the BindToType methods with your custom naming logic. The example SerializationBinder example below serializes types to just their name without any namespace or assembly information, those details are passed into the binder itself via a constructor parameter and used during deserialization.
public class TypeNameSerializationBinder : SerializationBinder
{
public string TypeFormat { get; private set; }
public TypeNameSerializationBinder(string typeFormat)
{
TypeFormat = typeFormat;
}
public override void BindToName(Type serializedType, out string assemblyName, out string typeName)
{
assemblyName = null;
typeName = serializedType.Name;
}
public override Type BindToType(string assemblyName, string typeName)
{
string resolvedTypeName = string.Format(TypeFormat, typeName);
return Type.GetType(resolvedTypeName, true);
}
}
The simplest way to use a custom serialization binder is with JsonConvert and JsonSerializerSettings.
TypeNameSerializationBinder binder = new TypeNameSerializationBinder("YourAppNamespace.{0}, YourAppAssembly");
IList<object> values = new List<object>
{
new Customer
{
Name = "Caroline Customer"
},
new Purchase
{
ProductName = "Elbow Grease",
Price = 5.99m,
Quantity = 1
}
};
string json = JsonConvert.SerializeObject(values, Formatting.Indented, new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.Auto,
Binder = binder
});
//[
// {
// "$type": "Customer",
// "Name": "Caroline Customer"
// },
// {
// "$type": "Purchase",
// "ProductName": "Elbow Grease",
// "Price": 5.99,
// "Quantity": 1
// }
//]
To deserialize just reuse the custom SerializationBinder with DeserializeObject.
Changes
Here is a complete list of what has changed since Json.NET 4.0 Release 3.
- Change - JsonTextReader.Culture is now CultureInfo.InvariantCulture by default
- Change - KeyValurPairConverter no longer depends on the order of the key and value properties
- Change - Time zone conversions now use new TimeZoneInfo instead of TimeZone
- Fix - Fixed boolean values sometimes being capitalized when converting to XML
- Fix - Fixed error when deserializing ConcurrentDictionary
- Fix - Fixed serializing some Uris returning the incorrect value
- Fix - Fixed occasional error when converting non-long integer properties to XML
- Fix - Fixed deserializing byte arrays with type name information
- Fix - Fixed flag enum items not being correctly camel cased
- Fix - Fixed JsonValidatingReader validating the first array item twice
- Fix - Fixed JsonSchema not correctly validating integers as a subset of decimal
- Fix - Fixed bad BSON when writing long strings of complex UTF8 characters
- Fix - Fixed error not being raised for additional content in JSON string for JArray.Parse and JObject.Parse
- Fix - Fixed DataTableConverter including nulls with NullValueHandling.Ignore
Links
Json.NET 4.0 Release 4 Download – Json.NET source code, documentation and binaries
-
Json.NET 4.0 Release 3 – LINQ to JSON performance
One of Json.NET’s most popular features - LINQ to JSON – has gotten a speed boost.
Previously the LINQ to JSON collection objects (JObject and JArray) internally used a linked list to keep track of their children. While this gave them complete control over the order of properties inside objects, it made lookups by key (a property name for an object, an index for an array) slow over large JSON documents.
Json.NET 4.0 Release 3 changes the internal structure of the collection objects to eliminate linked lists – JArray now uses a list internally to store child values and JObject uses a combination of a list and dictionary, letting it preserve property order and make property name lookups O(1).
From your perspective the API for LINQ to JSON is exactly the same, it is just faster.
Assembly file names
One thing to note in this release is that to play nicely with NuGet the different versions of Json.NET now all use the same assembly file name: Newtonsoft.Json.dll
If you want to find out exactly which version of the dll you are using the Windows file properties dialog display’s a .NET assembly’s title as the File description.
Changes
Here is a complete list of what has changed since Json.NET 4.0 Release 2.
- New feature - Improved support for deserializing objects using non-default constructors
- New feature - JsonConverterAttribute now allowed on constructor parameters
- New feature - JsonPropertyAttribute now allowed on constructor parameters
- New feature - Added Serializable attribute to exceptions
- New feature - JsonObject and JsonProperty attributes can now be placed on an interface and used when serializing implementing objects
- New feature - Improve TypeNameHandling.Auto to skip adding an unneeded type name for collection interfaces
- New feature - LINQ to JSON internals rewritten to use lists and dictionaries rather than linked lists
- New feature - Added support for deserializing to readonly collections and dictionaries on classes with non-default constructors
- New feature - Added serialization constructors to all Exceptions
- New feature - Added support for serialization event attributes on base classes
- New feature - Added support for BindToName on SerializationBinder
- New feature - Missing JSON errors can now be handled using JsonSerializer error handling
- New feature - Added Populate and IgnoreAndPopulate options to DefaultValueHandling for automatically populating default values during deserialization
- New feature - Added support for setting readonly fields when marked up with JsonPropertyAttribute
- New feature - Added Order to JsonPropertyAttribute to override the order of serialized JSON
- New feature - Added Culture to JsonTextReader to use when converting values from JSON text
- New feature - Added support for reading byte arrays from JSON integer arrays
- New feature - Added support for deserializing IDictionary properties
- New feature - Added ToObject to JToken for deserializing LINQ to JSON objects to a .NET object
- New feature - Added support for Guid, TimeSpan and Uri to LINQ to JSON
- Change - Changed WriteEndObject, WriteEndArray, WriteEndConstructor on JsonWriter to be virtual
- Change - Moved JPropertyDescriptor to Newtonsoft.Json.Linq namespace
- Change - Additional content after the $ref property now ignored when reading schema references
- Change - Changed JToken.Children to return an empty iterator rather than erroring
- Change - Changed the assembly file names to all be Newtonsoft.Json.dll to fix NuGet referencing
- Change - Changed $id and $ref properties to allow null
- Fix - Changed .NET 2.0 version to use LinqBridge source code rather than ilmerge to fix error
- Fix - Fixed deserializing to IEnumerable<T> properties
- Fix - Fixed DataTable and DataColumn names not being modified by CamelCasePropertyNamesContractResolver
- Fix - Fixed JObject loading JSON with comments
- Fix - Fixed error when using a Specified property with no setter
- Fix - Fixed transient constructor error on Windows Phone 7
- Fix - Fixed deserializing null values into nullable generic dictionaries
- Fix - Fixed potential casting error when writing JSON using a JsonReader
- Fix - Fixed converting emtpy XML elements with an array attribute not writing other attributes
- Fix - Fixed deserializing null values into DataTables
- Fix - Fixed error when deserializing readonly IEnumerable<T> array properties
- Fix - Fixed not including type name for byte[] values
- Fix - Fixed BsonWriter failing silently when writing values outside of an Object or Array
- Fix - Fixed serializer attempting to use dynamic code generation in partial trust
- Fix - Fixed serializing objects with DataContract and DataMember attributes on base classes
Links
Json.NET 4.0 Release 3 Download – Json.NET source code, documentation and binaries
-
Json.NET 4.0 Release 2 – NuGet 1.2 and Dynamic
This release builds on two areas introduced in Json.NET 4.0: NuGet and .NET 4 dynamic support.
NuGet 1.2
NuGet is fast becoming the preferred way to install libraries in .NET. NuGet 1.2 was recently released and among other improvements it adds support for the Windows Phone.
Json.NET 4.0 Release 2 has been updated to support NuGet 1.2 and Windows Phone – there is nothing you need to do, it should just work. Windows Phone brings Json.NET’s NuGet support up to 7 different frameworks:
- .NET 4
- .NET 4 Client Profile
- .NET 3.5
- .NET 3.5 Client Profile
- .NET 2
- Silverlight 4
- Windows Phone
Dynamic Improvements
Feedback on the initial release of dynamic support in Json.NET 4.0 was that values didn’t behave as expected. Concatenating two integers would throw an exception for example – less than ideal.
Release 2 adds support for common value operations: comparing, adding, subtracting, multiplying, dividing, incrementing and decrementing all now work like they should.
dynamic product = new JObject();
product.ProductName = "Elbow Grease";
product.Enabled = true;
product.Price = 4.90m;
product.StockCount = 9000;
product.StockValue = 44100;
// All Elbow Grease must go sale!
// 50% off price
product.Price = product.Price / 2;
product.StockValue = product.StockCount * product.Price;
product.ProductName = product.ProductName + " (SALE)";
string json = product.ToString();
// {
// "ProductName": "Elbow Grease (SALE)",
// "Enabled": true,
// "Price": 2.45,
// "StockCount": 9000,
// "StockValue": 22050.0
// }
And More
Like always many smaller features has been added (EnumMemberAttribute serializer support for example) and bugs have been fixed.
Changes
Here is a complete list of what has changed since Json.NET 4.0 Release 1.
- New feature - Added commonly used primitive value operations to dynamic JValue
- New feature - Added IComparable to JValue
- New feature - Added JsonConstructorAttribute to explicitly define which constructor to use during deserialization
- New feature - Added IFormattable to JValue
- New feature - Added Load and Parse helper methods to JToken
- New feature - Added ExpandoObjectConverter that deserializes to primitive values rather than LINQ to JSON objects for ExpandObjects
- New feature - Added EnumMemberAttribute support to StringEnumConverter
- New feature - Added CloseInput/CloseOutput to JsonReader/JsonWriter to control whether the underlying stream is closed. Default to true
- Change - JValue ToString now calls ToString on the internal value
- Change - NuGet spec file includes client profiles
- Change - BsonReader/BsonWriter close the underlying stream by default when closed
- Change - CamelCasePropertyNamesContractResolver updates dictionary and dynamic property names
- Fix – DefaultValueHandling no longer includes a property when the default value is the same value but a different type
- Fix – Objects created with paramatized constructors fixed to not set properties that aren't in JSON
- Fix – Fixed comments in JSON sometimes causing errors when deserializing
- Fix – Error when reflecting over type that has multiple index properties inherited fixed
- Fix – Error when reusing JsonSerializer with preserve references enabled fixed
- Fix – Poor error message when dynamic type cannot be created during deserialization fixed
- Fix – Error when setting a null value on a dynamic type during deserialization fixed
- Fix – JsonTextReader.ReadAsDecimal not attempting to convert string values to decimal fixed
- Fix – Duplicate type name within an assembly error fixed
- Fix – Deserializing a DataTable property fixed
- Fix – Code emit error when deserializing interfaces in certain situations fixed
- Fix – Performance issues when deserializing with partial type names fixed
- Fix – Deserializing some decimal values on classes with non-default constructors fixed
Links
Json.NET 4.0 Release 2 Download – Json.NET source code, documentation and binaries
-
Json.NET 4.0 Release 1 – .NET 4 and Windows Phone support
By popular demand: Json.NET 4.0! This is the first Json.NET release to target .NET 4 and integrates the many new features added in the latest version of .NET. Behind the scenes Json.NET’s source code has been upgraded to VS2010.
Other major changes in this release are two new builds and the removal of an existing build. Removed is Compact Framework. VS2010 no longer supports Compact Framework so 3.5 r8 will be its last release if you are targeting the Compact Framework. New on the other hand are builds for .NET 3.5 (the main build has been upgraded to .NET 4) and Windows Phone.
Windows Phone dll
Json.NET 4.0 comes with a Windows Phone specific dll, compiled using the Windows Phone tools. The Silverlight build now targets Silverlight 4 so is no longer compatible with Windows Phone. A couple of little bonuses of Windows Phone having its own dll is that it doesn’t prompt with a warning when added. There is also some additional XML support. Because LINQ to XML is included in Windows Phone by default (it is an add-on dll for Silverlight) the Json.NET Windows Phone build has some XML features that are missing out of Silverlight release.
Dynamic
One of the more interesting features introduced in .NET 4 is the dynamic keyword. Succinctly it allows variables and members to be statically typed as dynamic. The .NET type system that you know and love remains but operations involving dynamic values are evaluated at runtime.
Json.NET 4.0 adds support for dynamic in a couple of areas. The first and less visible of the two is in the JsonSerializer. Because there is no static list of fields or properties for a dynamic type the serializer interrogates the value for its members prior to serializing and deserializing. The end result is serializing should Just Work for any type that implements IDynamicMetaObjectProvider.
dynamic value = new DynamicDictionary();
value.Name = "Arine Admin";
value.Enabled = true;
value.Roles = new[] {"Admin", "User"};
string json = JsonConvert.SerializeObject(value, Formatting.Indented);
// {
// "Name": "Arine Admin",
// "Enabled": true,
// "Roles": [
// "Admin",
// "User"
// ]
// }
dynamic newValue = JsonConvert.DeserializeObject<DynamicDictionary>(json);
string role = newValue.Roles[0];
// Admin
The second area with new dynamic support is LINQ to JSON. JObject properties can be accessed like that were members on a type and JValues can be converted to .NET types without casting, saving you the consumer precious KLOCs.
JObject oldAndBusted = new JObject();
oldAndBusted["Name"] = "Arnie Admin";
oldAndBusted["Enabled"] = true;
oldAndBusted["Roles"] = new JArray(new[] { "Admin", "User" });
string oldRole = (string) oldAndBusted["Roles"][0];
// Admin
dynamic newHotness = new JObject();
newHotness.Name = "Arnie Admin";
newHotness.Enabled = true;
newHotness.Roles = new JArray(new[] { "Admin", "User" });
string newRole = newHotness.Roles[0];
// Admin
NuGet
Json.NET has a NuGet package available from the official NuGet package source. Right now it has Json.NET 3.5 Release 8 but expect it to be updated to Json.NET 4.0 in a couple of days.
JSON Schema
Json.NET’s JSON Schema implementation has been updated to match version 3 of the specification. Notable new additions are patternProperties, exclusiveMinimum and exclusiveMaximum. Also new is the removal of optional which has been replaced with required. If you are using JSON Schema then you should check whether this change effects your schemas.
BSON Binary
The BSON spec has changed how binary values inside BSON should be written, deprecating the way Json.NET use to write binary values. Json.NET has been changed to use the new method.
Also worth noting is Json.NET had a bug with how it use to read and write the old binary values – this is fixed in Json.NET 4.0 but existing incorrect binary values will remain. Setting JsonNet35BinaryCompatibility on BsonReader will fix reading any existing BSON after upgrading to 4.0 but because of this bug and the change in how the spec says BSON binary values should be written it is recommended to update existing BSON to keep things consistent. This can be done by setting compatibility flag to true, reading BSON and then writing it back out again.
These changes only effect BSON that has binary values (i.e. byte arrays) written inside BSON.
Changes
Here is a complete list of what has changed since Json.NET 3.5 Release 8.
- New feature - Added Windows Phone 7 project
- New feature - Added dynamic support to LINQ to JSON
- New feature - Added dynamic support to serializer
- New feature - Added INotifyCollectionChanged to JContainer in .NET 4 build
- New feature - Added ReadAsDateTimeOffset to JsonReader
- New feature - Added ReadAsDecimal to JsonReader
- New feature - Added covariance to IJEnumerable type parameter
- New feature - Added XmlSerializer style Specified property support
- New feature - Added support for deserializing to JToken
- New feature - Added CamelCaseText flag to StringEnumConverter
- New feature - Added WriteArrayAttribute to XmlNodeConverter to include an attribute to help round tripping JSON and XML
- New feature - Added OmitRootObject to XmlNodeConverter to skip writing the root JSON object when converting XML
- New feature - Added DeepClone and ICloneable implementation to JToken
- New feature - Added JSON schema PatternProperties, ExclusiveMinimum and ExclusiveMaximum for JSON schema spec 3
- New feature - Added .NET 3.5 project
- Change - Updated source code to VS2010
- Change - Updated main project to .NET 4
- Change – Updated Silverlight project to Silverlight 4
- Change - ICustomTypeDescriptor implementation moved to JObject
- Change - Improved error message when converting JSON with an empty property to XML
- Change - JSON schema Required replaced with Optional, as defined in JSON schema spec 3
- Change - JSON schema MaxDecimals replaced with DivisibleBy, as defined in JSON schema spec 3
- Remove - Compact Framework project removed. Compact Framework is not supported in VS2010
- Remove - JTypeDescriptionProvider and JTypeDesciptor removed
- Fix - BSON writing of binary values fixed. Added JsonNet35BinaryCompatibility flag for reading existing incorrect BSON binary values
- Fix - Timezone information no longer lost when deserializing DateTimeOffsets
- Fix – Decimal precision no longer lost when deserializing decimals
- Fix - SelectToken no longer skips the last project name when it is a single character
- Fix – Deserializing structs correctly set when reusing existing values is true
- Fix – Private getter/setters on base classes are now correctly accessible
- Fix – Nullable structs correctly deserialize
- Fix - JSON Schema option label now written correctly
- Fix – Deserializing a DataSet when it is a property of another object no longer breaks
- Fix - JToken Load and Parse methods now check that content is complete
Links
Json.NET 4.0 Release 1 Download – Json.NET source code, documentation and binaries