3 namespace Newtonsoft.Json
6 /// Instructs the <see cref="JsonSerializer"/> to always serialize the member with the specified name.
8 [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false)]
9 public sealed class JsonPropertyAttribute : Attribute
11 // yuck. can't set nullable properties on an attribute in C#
12 // have to use this approach to get an unset default state
13 internal NullValueHandling? _nullValueHandling;
14 internal DefaultValueHandling? _defaultValueHandling;
15 internal ReferenceLoopHandling? _referenceLoopHandling;
16 internal ObjectCreationHandling? _objectCreationHandling;
17 internal TypeNameHandling? _typeNameHandling;
18 internal bool? _isReference;
21 /// Gets or sets the null value handling used when serializing this property.
23 /// <value>The null value handling.</value>
24 public NullValueHandling NullValueHandling
26 get { return _nullValueHandling ?? default(NullValueHandling); }
27 set { _nullValueHandling = value; }
31 /// Gets or sets the default value handling used when serializing this property.
33 /// <value>The default value handling.</value>
34 public DefaultValueHandling DefaultValueHandling
36 get { return _defaultValueHandling ?? default(DefaultValueHandling); }
37 set { _defaultValueHandling = value; }
41 /// Gets or sets the reference loop handling used when serializing this property.
43 /// <value>The reference loop handling.</value>
44 public ReferenceLoopHandling ReferenceLoopHandling
46 get { return _referenceLoopHandling ?? default(ReferenceLoopHandling); }
47 set { _referenceLoopHandling = value; }
51 /// Gets or sets the object creation handling used when deserializing this property.
53 /// <value>The object creation handling.</value>
54 public ObjectCreationHandling ObjectCreationHandling
56 get { return _objectCreationHandling ?? default(ObjectCreationHandling); }
57 set { _objectCreationHandling = value; }
61 /// Gets or sets the type name handling used when serializing this property.
63 /// <value>The type name handling.</value>
64 public TypeNameHandling TypeNameHandling
66 get { return _typeNameHandling ?? default(TypeNameHandling); }
67 set { _typeNameHandling = value; }
71 /// Gets or sets whether this property's value is serialized as a reference.
73 /// <value>Whether this property's value is serialized as a reference.</value>
74 public bool IsReference
76 get { return _isReference ?? default(bool); }
77 set { _isReference = value; }
81 /// Gets or sets the name of the property.
83 /// <value>The name of the property.</value>
84 public string PropertyName { get; set; }
87 /// Gets or sets a value indicating whether this property is required.
90 /// A value indicating whether this property is required.
92 public Required Required { get; set; }
95 /// Initializes a new instance of the <see cref="JsonPropertyAttribute"/> class.
97 public JsonPropertyAttribute()
102 /// Initializes a new instance of the <see cref="JsonPropertyAttribute"/> class with the specified name.
104 /// <param name="propertyName">Name of the property.</param>
105 public JsonPropertyAttribute(string propertyName)
107 PropertyName = propertyName;