Click or drag to resize
Json.NETSerialization Attributes

Attributes can be used to control how Json.NET serializes and deserializes .NET objects.

  • JsonObjectAttribute - Placed on classes to control how they should be serialized as a JSON object.

  • JsonArrayAttribute - Placed on collections to control how they should be serialized as a JSON array.

  • JsonDictionaryAttribute - Placed on dictionaries to control how they should be serialized as a JSON object.

  • JsonPropertyAttribute - Placed on fields and properties to control how they should be serialized as a property in a JSON object.

  • JsonConverterAttribute - Placed on either classes or fields and properties to specify which JsonConverter should be used during serialization.

Standard .NET Serialization Attributes

As well as using the built-in Json.NET attributes, Json.NET also looks for the SerializableAttribute (if IgnoreSerializableAttribute on DefaultContractResolver is set to false) DataContractAttribute, DataMemberAttribute, and NonSerializedAttribute and attributes when determining how JSON is to be serialized and deserialized.

Note Note

Json.NET attributes take precedence over standard .NET serialization attributes (e.g. if both JsonPropertyAttribute and DataMemberAttribute are present on a property and both customize the name, the name from JsonPropertyAttribute will be used).

Serialization Attributes Example
 2public class Person
 4    // "John Smith"
 5    [JsonProperty]
 6    public string Name { get; set; }
 8    // "2000-12-15T22:11:03"
 9    [JsonProperty]
10    public DateTime BirthDate { get; set; }
12    // new Date(976918263055)
13    [JsonProperty]
14    [JsonConverter(typeof(JavaScriptDateTimeConverter))]
15    public DateTime LastModified { get; set; }
17    // not serialized because mode is opt-in
18    public string Department { get; set; }
Json.NET Serialization Attributes

The MemberSerialization flag on this attribute specifies whether member serialization is opt-in (a member must have the JsonProperty or DataMember attribute to be serialized), opt-out (everything is serialized by default but can be ignored with the JsonIgnoreAttribute, Json.NET's default behavior) or fields (all public and private fields are serialized and properties are ignored).

Json.NET serializes .NET classes that implement IEnumerable as a JSON array populated with the IEnumerable values. Placing the JsonPropertyAttribute overrides this behavior and forces the serializer to serialize the class's fields and properties.

The DataContractAttribute can be used as substitute for JsonObjectAttribute. The DataContractAttribute will default member serialization to opt-in.


The JsonArrayAttribute and JsonDictionaryAttributes are used to specify whether a class is serialized as that collection type.

The collection attributes have options to customize the JsonConverter, type name handling, and reference handling that are applied to collection items.


JsonPropertyAttribute has a number of uses:

  • By default, the JSON property will have the same name as the .NET property. This attribute allows the name to be customized.

  • JsonPropertyAttribute indicates that a property should be serialized when member serialization is set to opt-in.

  • It includes non-public properties in serialization and deserialization.

  • It can be used to customize type name, reference, null, and default value handling for the property value.

  • It can be used to customize the property's collection items JsonConverter, type name handling, and reference handling.

The DataMemberAttribute can be used as a substitute for JsonPropertyAttribute.


Excludes a field or property from serialization.

The NonSerializedAttribute can be used as substitute for JsonIgnoreAttribute.


The JsonConverterAttribute specifies which JsonSerializer is used to convert an object.

The attribute can be placed on a class or a member. When placed on a class, the JsonConverter specified by the attribute will be the default way of serializing that class. When the attribute is on a field or property, then the specified JsonConverter will always be used to serialize that value.

The priority of which JsonConverter is used is member attribute, then class attribute, and finally any converters passed to the JsonSerializer.

JsonConverter Attribute
 1public class SerializationEventTestObject
 3    // 2222
 4    // This member is serialized and deserialized with no change.
 5    public int Member1 { get; set; }
 7    // The value of this field is set and reset during and 
 8    // after serialization.
 9    public string Member2 { get; set; }
11    // This field is not serialized. The OnDeserializedAttribute 
12    // is used to set the member value after serialization.
13    [JsonIgnore]
14    public string Member3 { get; set; }
16    // This field is set to null, but populated after deserialization.
17    public string Member4 { get; set; }
19    public SerializationEventTestObject()
20    {
21        Member1 = 11;
22        Member2 = "Hello World!";
23        Member3 = "This is a nonserialized value";
24        Member4 = null;
25    }
27    [OnSerializing]
28    internal void OnSerializingMethod(StreamingContext context)
29    {
30        Member2 = "This value went into the data file during serialization.";
31    }
33    [OnSerialized]
34    internal void OnSerializedMethod(StreamingContext context)
35    {
36        Member2 = "This value was reset after serialization.";
37    }
39    [OnDeserializing]
40    internal void OnDeserializingMethod(StreamingContext context)
41    {
42        Member3 = "This value was set during deserialization";
43    }
45    [OnDeserialized]
46    internal void OnDeserializedMethod(StreamingContext context)
47    {
48        Member4 = "This value was set after deserialization.";
49    }

This example shows the JsonConverterAttribute being applied to a property.

JsonConverter Example
 1SerializationEventTestObject obj = new SerializationEventTestObject();
 4// 11
 6// Hello World!
 8// This is a nonserialized value
10// null
12string json = JsonConvert.SerializeObject(obj, Formatting.Indented);
13// {
14//   "Member1": 11,
15//   "Member2": "This value went into the data file during serialization.",
16//   "Member4": null
17// }
20// 11
22// This value was reset after serialization.
24// This is a nonserialized value
26// null
28obj = JsonConvert.DeserializeObject<SerializationEventTestObject>(json);
31// 11
33// This value went into the data file during serialization.
35// This value was set during deserialization
37// This value was set after deserialization.

To apply a JsonConverter to the items in a collection, use either JsonArrayAttribute, JsonDictionaryAttribute or JsonPropertyAttribute and set the ItemConverterType property to the converter type you want to use.

See Also