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.

  • JsonExtensionDataAttribute - Placed on a collection field or property to deserialize properties with no matching class member into the specified collection and write values during serialization.

  • JsonConstructorAttribute - Placed on a constructor to specify that it should be used to create the class during deserialization.

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

This section contains the following subsections:


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).

The NamingStrategy setting on this attributes can be set to a NamingStrategy type that specifies how property names are serialized.

Json.NET serializes .NET classes that implement IEnumerable as a JSON array populated with the IEnumerable values. Placing the JsonObjectAttribute 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 JsonDictionaryAttribute 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 NamingStrategy of the serialized property name.

  • 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 a substitute for JsonIgnoreAttribute.


The JsonConverterAttribute specifies which JsonConverter 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.

JsonConverterAttribute Property Example
 1public enum UserStatus
 3    NotConfirmed,
 4    Active,
 5    Deleted
 8public class User
10    public string UserName { get; set; }
12    [JsonConverter(typeof(StringEnumConverter))]
13    public UserStatus Status { get; set; }

This example shows the JsonConverterAttribute being applied to a property.

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.


The JsonExtensionDataAttribute instructs the JsonSerializer to deserialize properties with no matching field or property on the type into the specified collection. During serialization the values in this collection are written back to the instance's JSON object.

Note Note

All extension data values will be written during serialization, even if a property the same name has already been written.

This example shows the JsonExtensionDataAttribute being applied to a field, unmatched JSON properties being added to the field's collection during deserialization.

 1public class DirectoryAccount
 3    // normal deserialization
 4    public string DisplayName { get; set; }
 6    // these properties are set in OnDeserialized
 7    public string UserName { get; set; }
 8    public string Domain { get; set; }
10    [JsonExtensionData]
11    private IDictionary<string, JToken> _additionalData;
13    [OnDeserialized]
14    private void OnDeserialized(StreamingContext context)
15    {
16        // SAMAccountName is not deserialized to any property
17        // and so it is added to the extension data dictionary
18        string samAccountName = (string)_additionalData["SAMAccountName"];
20        Domain = samAccountName.Split('\\')[0];
21        UserName = samAccountName.Split('\\')[1];
22    }
24    public DirectoryAccount()
25    {
26        _additionalData = new Dictionary<string, JToken>();
27    }
 1string json = @"{
 2  'DisplayName': 'John Smith',
 3  'SAMAccountName': 'contoso\\johns'
 6DirectoryAccount account = JsonConvert.DeserializeObject<DirectoryAccount>(json);
 9// John Smith
12// contoso
15// johns


The JsonConstructorAttribute instructs the JsonSerializer to use a specific constructor when deserializing a class. It can be used to create a class using a parameterized constructor instead of the default constructor, or to pick which specific parameterized constructor to use if there are multiple.

 1public class User
 3    public string UserName { get; private set; }
 4    public bool Enabled { get; private set; }
 6    public User()
 7    {
 8    }
10    [JsonConstructor]
11    public User(string userName, bool enabled)
12    {
13        UserName = userName;
14        Enabled = enabled;
15    }
1string json = @"{
2  ""UserName"": ""domain\\username"",
3  ""Enabled"": true
6User user = JsonConvert.DeserializeObject<User>(json);
9// domain\username
See Also