diff --git a/Doc/SerializationGuide.html b/Doc/SerializationGuide.html new file mode 100644 index 00000000..c00e2f71 --- /dev/null +++ b/Doc/SerializationGuide.html @@ -0,0 +1,228 @@ + + + + Customizing JSON serialization with attributes + + + + + + + +
+ Json.NET - Quick Starts & API Documentation
+ Serialization Guide
+ +
+ + +

The Json.NET serializer can serialize a wide variety of .NET objects. This guide + looks at how it works at a high level and in more detail.

+

Summary

+

At a high level, the Json.NET serializer will convert primitive .NET values into + primitive JSON values, .NET arrays and collections to JSON arrays and everything + else to JSON objects.

+

Json.NET will throw an error if it encounters incorrect JSON when deserializing a + value. For example if the serializer encounters a JSON property with an array of + values and the type of matching .NET property is not a collection then an error + will be thrown, and vice-versa.

+

Complex Types

+ + + + + + + + + + + + + + + + + +
+ .NET + JSON
+ IList, IEnumerable, IList<T>, Array + Array
+ IDictionary, IDictionary<TKey, + TValue> + Object
+ Object + (more detail below)Object
+

Primitive Types

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ .NET .NET + JSON
+ String + String
+ Byte
+ SByte
+ UInt16
+ Int16
+ UInt32
+ Int32
+ UInt64
+ Int64
+ Integer
+ Float
+ Double
+ Decimal
+ Float
+ Enum + Integer (can be string with + StringEnumConverter)
+ DateTime + String (Serializing Dates in JSON)
+ Byte[] + String (base 64 encoded)
+ Type + String (type name)
+ Guid + String
+ + TypeConverter (convertible to String)String
+

Breakdown of Type Serialization

+

Objects

+

.NET types that don’t fall into any other category listed below (i.e. aren’t + lists, dictionaries, dynamic, implement ISerializable, etc) are serialized as + JSON objects.

+

By default types are serialized in opt-out mode. What that means is all public + fields and properties with getters are automatically serialized to JSON and + members that shouldn’t be serialized are opted-out using the + JsonIgnoreAttribute. To serialize private members the JsonPropertyAttribute can + be placed on private fields and properties or the DefaultMembersSearchFlags can + be changed on DefaultContractResolver to change how members are serialized on + all types.

+

Types can also be serialized using opt-in mode. Only fields that have a + JsonPropertyAttribute or DataMemberAttribute on them will be serialized. Opt-in + mode for an object is specified using the JsonObjectAttribute on the class.

+

IEnumerable, Lists and Arrays

+

.NET lists (types that inherit from IEnumerable) and .NET arrays are converted to + JSON arrays. Because JSON arrays only support a range of values and not + properties, any additional properties and fields declared on .NET collections + are not serialized. In situations where a JSON array is not wanted the + JsonObjectAttribute can be placed on a .NET type that implements IEnumerable to + force the type to be serialized as a JSON object instead.

+

Note that if TypeNameHandling or PreserveReferencesHandling has been enabled for + JSON arrays on the serializer then JSON arrays are wrapped it a containing + object. The object will have the type name/reference properties and a $values + property which will have the collection data.

+

When deserializing if a member is typed as the interface IList then it will be + deserialized as a List.

+

Read more about serializing collections here: + Serializing Collections

+

Dictionarys and Hashtables

+

.NET dictionaries (types that inherit from IDictionary) are converted to JSON + objects. Note that only the dictionary name/values will be written to the JSON + object when serializing and properties on the JSON object will be added to the + dictionary’s name/values when deserializing. Additional members on the .NET + dictionary are ignored during serialization.

+

When deserializing if a member is typed as the interface IDictionary + then it will be deserialized as a Dictionary.

+

Read more about serializing collections here: + Serializing Collections

+

Untyped Objects

+

.NET properties on a class that don’t specify a type (i.e. they are just object) + are serialized as usual. When untyped properties are deserialized the serializer + has no way to know what type to create (unless type name handling is enabled and + the JSON contains the type names).

+

For these untyped properties the Json.NET serializer will read the JSON into LINQ + to JSON objects and set them to the property.

+

Dynamic

+

.NET dynamic objects (introduced in .NET 4) are serialized as JSON objects. A + property is written for every member name returned by + IDynamicMetaObjectProvider.GetDynamicMemberNames.

+

When deserializing the serializer first attempts to set JSON property values on a + normal .NET member with the matching name. If no .NET member is found with the + property name then the serializer will call SetMember on the dynamic object.

+

Because a dynamic property on a class doesn’t specify a type, Json.NET has no way + to know what type to create for it when deserializing (unless type name handling + is enabled and the JSON contains the type name). LINQ to JSON objects will be + created for the JSON.

+

ISerializable

+

Types that implement ISerializable are serialized as JSON objects. When + serializing only the values returned from ISerializable.GetObjectData are used; + members on the type are ignored. When deserializing the constructor with a + SerializationInfo and StreamingContext is called, passing the JSON object’s + values.

+

In situations where this behaviour is not wanted the JsonObjectAttribute can be + placed on a .NET type that implements ISerializable to force it to be serialized + as a normal JSON object.

+

LINQ to JSON

+

LINQ to JSON types (e.g. JObject, JArray) are automatically serialized and + deserialized to their equivalent JSON when encountered by the Json.NET + serializer.

+

JsonConverter

+

Serialization of values that are convertible by a JsonConverter (i.e. CanConvert + returns true for its type) is completely overridden by the JsonConverter. The + test to see whether a value can be converted by a JsonSerializer takes + precedence over all other tests.

+

JsonConverters can be defined and specified in a number of places: in an + attribute on a member, in an attribute on a class and added to the + JsonSerializer’s converters collection. The priority of which JsonConverter is + used is the JsonConverter defined by attribute on a member then the + JsonConverter defined by an attribute on a class and finally any converters + passed to the JsonSerializer.

+ + + +
+ + + + \ No newline at end of file diff --git a/Doc/SerializationSettings.html b/Doc/SerializationSettings.html new file mode 100644 index 00000000..bd32746b --- /dev/null +++ b/Doc/SerializationSettings.html @@ -0,0 +1,174 @@ + + + + Customizing JSON serialization with attributes + + + + + + +
+ Json.NET - Quick Starts & API Documentation
+ Serialization Settings
+ +
+ + +

JsonSerializer has a number of properties on it to customize how it serializes + JSON. These can also be used with the methods on JsonConvert via the + JsonSerializerSettings overloads.

+

ReferenceLoopHandling

+

ReferenceLoopHandling controls how circular referencing objects, e.g. a Person + object referencing itself via a Manager property, are serialized.

+ + +

MissingMemberHandling

+

MissingMemberHandling controls how missing members, e.g. JSON contains a property + that isn't a member on the object, are handled during deserialization.

+ +

NullValueHandling

+

NullValueHandling controls how null values on .NET objects are handled during + serialization and how null values in JSON are handled during deserialization.

+ +

+ DefaultValueHandling

+

+ DefaultValueHandling controls how Json.NET uses default values set using the .NET + DefaultValueAttribute when serializing and deserializing.

+ +

ObjectCreationHandling

+

ObjectCreationHandling controls how objects are created and deserialized to + during deserialization.

+ +

TypeNameHandling

+

TypeNameHandling controls whether Json.NET includes .NET type names during + serialization with a $type property and reads .NET type names from that property + to determine what type to create during deserialization.

+ +

TypeNameAssemblyFormat

+

TypeNameAssemblyFormat controls how type names are written during serialization.

+ +

Read more about the valid values at + + FormatterAssemblyStyle.

+

Binder

+

The serialization binder is used to resolve type names to a .NET type.

+

Read more about the serialization binder here: + SerializationBinder

+

ConstructorHandling

+

ConstructorHandling controls how constructors are used when initializing objects + during deserialization.

+ +

Converters

+

This is the collection of JsonConverters that will be used during serialization + and deserialization.

+

JsonConverters allows JSON to be manually written during serialization and read + during deserialization. This is useful for particularly complex JSON structures + or for when you want to change how a type is serialized.

+

When a JsonConverter has been added to a JsonSerializer it will be checked for + every value that is being serialized/deserialized using its CanConvert to see if + it should be used. If CanConvert returns true then the JsonConverter will be + used to read or write the JSON for that value. Note that while a JsonConverter + gives you complete control over that values JSON, many Json.NET serialization + features are no longer available like type name and reference handling.

+

To create your own custom converter inherit from the JsonConverter class. Read + more about the built-in JsonConverters below:

+ +

ContractResolver

+

Internally for every .NET type the JsonSerializer will create a contract of how + the type should be serialized and deserialized, based on type metadata and + attributes applied to the class. Specifying a custom IContractResolver allows + the creation of contracts to be customized.

+

Read more about Contract Resolvers here: Contract + Resolvers

+

Error

+

The Error event can catch errors during serialization and either handle the event + and continue with serialization or let the error bubble up and be thrown to the + application.

+

Read more about error handling here: + Serialization Error Handling

+ + + +
+ + + + \ No newline at end of file diff --git a/Doc/SerializingJSON.html b/Doc/SerializingJSON.html index c2913961..ab6b2dc4 100644 --- a/Doc/SerializingJSON.html +++ b/Doc/SerializingJSON.html @@ -1,7 +1,7 @@ - Serializing and deserializing JSON + Serializing and Deserializing JSON @@ -10,7 +10,7 @@
Json.NET - Quick Starts & API Documentation
- Serializing and deserializing JSON
+ Serializing and Deserializing JSON
@@ -51,6 +51,11 @@
+

+ SerializeObject and DeserializeObject both have overloads that take a + JsonSerializerSettings object. JsonSerializerSettings lets you use many of the JsonSerializer settings listed below while still using the simple serialization methods. +

+

JsonSerializer

@@ -83,56 +88,8 @@

JsonSerializer has a number of properties on it to customize how it serializes JSON. These can also be used with the methods on JsonConvert via the JsonSerializerSettings overloads.

- -
ReferenceLoopHandling
-

Controls how circular referencing objects are serialized. Error, ignore or - serialize.

-
MissingMemberHandling
-

Controls how missing members (e.g. JSON contains a property that isn't a member - on the object) are handled during deserialization. Ignore or error.

-
NullValueHandling
-

Controls how null values are handled during serialization and deserialization. - Include or ignore.

-
DefaultValueHandling
-

Controls whether a value will be written to JSON or not if it matches the value specified in -the member's DefaultValueAttribute. Include or ignore.

-
ObjectCreationHandling
-

Controls how objects are created during deserialization. Auto, reuse, replace.

-
TypeNameHandling
-

Controls whether .NET type names are included in serialized JSON and read during deserialization when creating objects. None, Objects, Arrays or All.

-
ConstructorHandling
-

Controls how constructors are used when initializing objects during deserialization. Default or AllowNonPublicDefaultConstructor.

-
Converters
-

A collection of JsonConverters that will be used during serialization and - deserialization.

- - -

JsonConverters

-

JsonConverters allows JSON to be manually written during serialization and read during deserialization. This is useful for particularly complex JSON structures or for when you want to change how a type is serialized.

-

To create your own custom converter inherit from the JsonConverter class. Json.NET also comes with a number of JsonConverters:

- - - -
DateTime JSON Converters
-

Json.NET comes with a number of JsonConverters for serializing and deserializing DateTimes. Read - more about dates and Json.NET here.

- -
XmlNodeConverter
-

Converts an XmlNode to and from JSON. Note that to convert a JSON object it must have only a single property - or you must define a root node name to be inserted when using this converter. This is required because properties are converted into nodes and - well formed XML can only have one root node. XmlNodeConverter has an option to - insert a root node for you.

- - -
BinaryConverter
-

Converts binary data like the SqlBinary object to JSON. The binary - data is written as a string in JSON and is encoded in Base64.

- -
CustomCreationConverter
-

An abstract JsonConverter for customizing how an object is create during deserialization. - Inherit from this class and implement the Create method with your own code to create and return an object. - The object will then be populated with JSON values by the serializer.

-

A possible example of using this converter would be to call out to a dependency injection framework to resolve what object should be created.

+

Read more about the available JsonSerializer settings here: + Serialization Settings