Attributes can be used to control how JSON. Net serializes and deserializes. Net objects.
> Jsonobjectattribute -- mark a class to control how the class is serialized as a JSON object)
> Jsonarrayattribute -- mark on the set to control how the set is serialized as a JSON set (JSON array)
> Jsonpropertyattribute -- it is marked on fields and attributes to control how it is serialized into attributes in a JSON object.
> Jsonconverterattribute -- mark the class, field, and attribute to specify the JSON converter (jsonconverter) during serialization)
Like using the built-in JSON. NET attributes, JSON. Net also looks for datacontract and datamember attributes when determining how JSON is serialized and deserialized. If both exist, the JSON. Net serialization feature will be used first.
Example:
[Jsonobject (memberserialization. optout)]
Public class person
{
// "John Smith"
[Jsonproperty]
Public string name {Get; set ;}
// "2000-12-15t22: 11: 03"
[Jsonproperty]
[Jsonconverter (typeof (isodatetimeconverter)]
Public datetime birthdate {Get; set ;}
// New date (976918263055)
[Jsonproperty]
[Jsonconverter (typeof (javascriptdatetimeconverter)]
Public datetime lastmodified {Get; set ;}
// Not serialized
Public String Department {Get; set ;}
}
-> Jsonobjectattribute
The memberserialization mark indicates that the specified member is serialized as opt-in (each member must mark jsonproperty or datamember attribute to be serialized) or opt-out (by default, all members except those marked as jsonignoreattribute will be serialized, Which is JSON. net)
JSON. Net serializes the. NET class that implements the ienumerable interface into a JSON set, and fills the set with the value of ienumerable. Mark jsonpropertyattribute to override this behavior, and force the serializer to serialize the fields and attributes of this class.
-> Jsonpropertyattribute
Jsonpropertyattribute has many functions
1. By default, JSON attributes and. NET attributes have the same name. This attribute allows custom attribute names.
2. When member serialization is set to opt-in, the specified attribute should be serialized.
3. Non-public attributes are included in serialization and deserialization.
-> Jsonignoreattribute
Exclude specified fields or attributes from serialization
-> Jsonconverterattribute
Jsonconverterattribute specifies that the JSON serializer (jsonserializer) is used to convert an object.
This feature can be used to mark a class or a member. When it is used to mark a class, jsonconverter specified by attribute will provide the default serialization method for the class. when it is used to mark a field or attribute, the specified jsonconverter will always be used to serialize the value.
The priority order of using jsonconverter is: first member, then class, and finally any converter passed to jsonserializer.
Example:
Public class memberconverterclass
{
Public datetime defaultconverter {Get; set ;}
[Jsonconverter (typeof (isodatetimeconverter)]
Public datetime memberconverter {Get; set ;}
} // Note: this is an example of jsonconverterattribute applied to attributes.
Test:
Datetime date = convert. todatetime ("1970-01-01t00: 00: 00Z"). touniversaltime ();
Memberconverterclass c = new memberconverterclass
{
Defaultconverter = date,
Memberconverter = Date
};
String JSON = jsonconvert. serializeobject (C, formatting. indented );
Console. writeline (JSON );
Output result:
{
"Defaultconverter": "\/date (0 )\/",
"Memberconverter": "1970-01-01t00: 00: 00Z"
}