"Newtonsoft.json json.net" 02.02, serialization Guidelines serialization guide

Source: Internet
Author: User

Json.NET serializer can be serialized in a variety of ways. NET object. This leads to a focus on how it works, first to understand it in a high-level perspective, and then to learn more about its details.

Profile

In general, Json.NET serializer is converted in this way:

Original. NET value → Raw json value

. NET array, collection →json array

Other →json objects

If an incorrect JSON is encountered, an error will be found.

Complex types

. NET

Json

IList, IEnumerable, Ilist<t>, Array

Array (Properties on the collection won't be serialized)

IDictionary, Idictionary<tkey, tvalue>

Object (dictionary name/values only, properties on the dictionary won't be serialized)

Object (more detail below)

Object

Basic type

. NET

Json

String

String

Byte

SByte

UInt16

Int16

UInt32

Int32

UInt64

Int64

Integer

Float

Double

Decimal

Float

Enum

Integer (Can is the enum value name with Stringenumconverter)

Datetime

String (serializing Dates in JSON)

Byte[]

String (Base encoded)

Type

String (type name)

Guid

String

TypeConverter (convertible to String)

String

Decomposition objects for serialized types:

(i.e aren ' t lists, dictionaries, dynamic, implement ISerializable, etc.) These. NET type is serialized as a JSON object.

You can use Jsonobjectattribute annotations to force One. NET type serialized into JSON object

The default property of a type is serialized using the opt-out pattern, which automatically serializes all public fields and the public properties of the getter to JSON, in addition to the fields and properties that have jsonignoreattribute labels. You can use Jsonpropertyattribute to serialize private members.

Can be set to opt-in mode for serialization. Only fields or properties that have the Jsonpropertyattribute annotation added will be serialized.

Finally, you can also serialize using the fields pattern. Only fields that are public or private will be serialized at this time, and the properties will be ignored. You can use Jsonobjectattribute to set memberserialization.fields on a type or use. NET SerializableAttribute set Ignoreserializableattribute or Defaultcontractresolver to false.

IEnumerable, Lists, and Arrays:

. NET lists (types, inherit from IEnumerable) and. NET arrays is converted to JSON arrays. Because JSON Arrays only support a range of values and not properties, any additional properties and fields declared on. N ET collections is not serialized. In situations where a type implements IEnumerable but a JSON array was not wanted and then the Jsonobjectattribute can be plac Ed on the type to force it-be serialized as a JSON object instead.
Jsonarrayattribute have options on it to customize the Jsonconverter, type name handling, and reference handling that is a Pplied to collection items.
Note that if typenamehandling or preservereferenceshandling have been enabled for JSON arrays on the serializer and then JSON Arrays is wrapped in a containing object. The object would have the type Name/reference properties and a $values property, which would have the collection data.
When deserializing, if a member are typed as the interface Ilist<t>, then it'll be deserialized as a list<t>.
You can read more about serializing collections Here:serializing collections

Dictionaries and Hashtables

. NET dictionaries (types, inherit from IDictionary) is converted to JSON objects. Note that the dictionary name/values is written to the JSON object when serializing, and properties on the JSON Object would be added to the dictionary's name/values when deserializing. Additional members on the. NET dictionary is ignored during serialization.
When serializing a dictionary, the Keys of the dictionary is converted to strings and used as the JSON object property na Mes. The string written for a key can is customized by either overriding ToString () for the key type or by implementing a Typec Onverter. A TypeConverter would also support converting a custom string back again when deserializing a dictionary.
Jsondictionaryattribute have options on it to customize the Jsonconverter, type name handling, and reference handling that is applied to collection items.
When deserializing, if a member are typed as the interface Idictionary<tkey, tvalue> then it'll be deserialized as A dictionary<tkey, Tvalue>
You can read more about serializing collections Here:serializing collections

Untyped Objects

. NET properties on a class of that don ' t specify a type (i.e. they is just object) is serialized as usual. When untyped properties is deserialized, the serializer has no on the knowing what type to create (unless type name Handl ING is enabled and the JSON contains the type names).
For these untyped properties, the Json.NET serializer would read the JSON into LINQ to JSON objects and set them to the pro Perty. Jobject is created for JSON objects; Jarray'll is created for JSON arrays, and jvalue'll be is created for primitive JSON values.

Dynamic

There is different usages of dynamic (introduced in. NET 4) in. Net. The first is. NET properties with a type of dynamic. Dynamic properties behave like properties declared as Object:any value can is assigned to it, but the difference being th At properties and methods can is called on a dynamic property without casting. In Json.NET, dynamic properties is serialized and deserialized exactly the same as untyped objects:because dynamic isn ' t An actual-type, Json.NET falls back-to-deserializing the JSON as LINQ to JSON objects.
The second usage of dynamic in. NET is by the types that implement IDynamicMetaObjectProvider. This interface lets the implementer create dynamic objects that intercept the property and method calls on an object and U Se them. ExpandoObject is a good example of a dynamic object.
Dynamic objects is serialized as JSON objects. A property was written for every member name returned by Dynamicmetaobject.getdynamicmembernames (). A Dynamic object ' s normal properties aren ' t serialized by default, but can is included by placing the Jsonpropertyattribute on them.
When deserializing dynamic objects, the serializer first attempts to set JSON property values in a normal. NET member with The matching name. If No. NET member is found with the property name, then the serializer would call Setmember on the dynamic object. Because There is no type information for dynamic members on a dynamic object, the values assigned to them would be LINQ to JSON objects.

ISerializable

Types that implement ISerializable is serialized as JSON objects. When serializing, only the values of returned from ISerializable.GetObjectData is used; Members on the type is ignored. When deserializing, the constructor with a SerializationInfo and StreamingContext are called, passing the JSON object ' s Val UEs.
In situations where this behavior isn't wanted, the jsonobjectattribute can be placed on a. NET type that implements Iser Ializable to force it-be serialized as a normal JSON object.

LINQ to JSON

LINQ to JSON types (e.g. Jobject and Jarray) is automatically serialized and deserialized to their equivalent JSON when E Ncountered by the Json.NET serializer.

Jsonconverter

serialization of values that is convertible by a jsonconverter (i.e. Canconvert returns True For it type) is completely overridden by the jsonconverter. The test to see whether a value can is converted by the Jsonserializer takes precedence through all other tests.
Jsonconverters can defined and specified in a number of places:in an attribute on a member, in an attribute on a C Lass, and added to the Jsonserializer ' s converters collection. The priority of which Jsonconverter is used are the jsonconverter defined by attribute on a member and then the Jsonconverter defined by an attribute on a class, and finally any converters passed to the Jsonserializer.
Note Note
Because a jsonconverter creates a new value, a converter won't work with readonly properties Because t This is no-to-assign the new value to the property. Either change the property to has a public setter or place a jsonpropertyattribute or DataMemberAttribute on the property .

"Newtonsoft.json json.net" 02.02, serialization guidelines serialization guide

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.