You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

222 lines
9.0 KiB

  1. #region License
  2. // Copyright (c) 2007 James Newton-King
  3. //
  4. // Permission is hereby granted, free of charge, to any person
  5. // obtaining a copy of this software and associated documentation
  6. // files (the "Software"), to deal in the Software without
  7. // restriction, including without limitation the rights to use,
  8. // copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the
  10. // Software is furnished to do so, subject to the following
  11. // conditions:
  12. //
  13. // The above copyright notice and this permission notice shall be
  14. // included in all copies or substantial portions of the Software.
  15. //
  16. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  17. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  18. // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  19. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  20. // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  21. // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  22. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  23. // OTHER DEALINGS IN THE SOFTWARE.
  24. #endregion
  25. using System;
  26. using Newtonsoft.Json.Serialization;
  27. namespace Newtonsoft.Json
  28. {
  29. /// <summary>
  30. /// Instructs the <see cref="JsonSerializer"/> to always serialize the member with the specified name.
  31. /// </summary>
  32. [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property | AttributeTargets.Parameter, AllowMultiple = false)]
  33. internal sealed class JsonPropertyAttribute : Attribute
  34. {
  35. // yuck. can't set nullable properties on an attribute in C#
  36. // have to use this approach to get an unset default state
  37. internal NullValueHandling? _nullValueHandling;
  38. internal DefaultValueHandling? _defaultValueHandling;
  39. internal ReferenceLoopHandling? _referenceLoopHandling;
  40. internal ObjectCreationHandling? _objectCreationHandling;
  41. internal TypeNameHandling? _typeNameHandling;
  42. internal bool? _isReference;
  43. internal int? _order;
  44. internal Required? _required;
  45. internal bool? _itemIsReference;
  46. internal ReferenceLoopHandling? _itemReferenceLoopHandling;
  47. internal TypeNameHandling? _itemTypeNameHandling;
  48. /// <summary>
  49. /// Gets or sets the <see cref="JsonConverter"/> used when serializing the property's collection items.
  50. /// </summary>
  51. /// <value>The collection's items <see cref="JsonConverter"/>.</value>
  52. public Type ItemConverterType { get; set; }
  53. /// <summary>
  54. /// The parameter list to use when constructing the <see cref="JsonConverter"/> described by <see cref="ItemConverterType"/>.
  55. /// If <c>null</c>, the default constructor is used.
  56. /// When non-<c>null</c>, there must be a constructor defined in the <see cref="JsonConverter"/> that exactly matches the number,
  57. /// order, and type of these parameters.
  58. /// </summary>
  59. /// <example>
  60. /// <code>
  61. /// [JsonProperty(ItemConverterType = typeof(MyContainerConverter), ItemConverterParameters = new object[] { 123, "Four" })]
  62. /// </code>
  63. /// </example>
  64. public object[] ItemConverterParameters { get; set; }
  65. /// <summary>
  66. /// Gets or sets the <see cref="Type"/> of the <see cref="NamingStrategy"/>.
  67. /// </summary>
  68. /// <value>The <see cref="Type"/> of the <see cref="NamingStrategy"/>.</value>
  69. public Type NamingStrategyType { get; set; }
  70. /// <summary>
  71. /// The parameter list to use when constructing the <see cref="NamingStrategy"/> described by <see cref="JsonPropertyAttribute.NamingStrategyType"/>.
  72. /// If <c>null</c>, the default constructor is used.
  73. /// When non-<c>null</c>, there must be a constructor defined in the <see cref="NamingStrategy"/> that exactly matches the number,
  74. /// order, and type of these parameters.
  75. /// </summary>
  76. /// <example>
  77. /// <code>
  78. /// [JsonProperty(NamingStrategyType = typeof(MyNamingStrategy), NamingStrategyParameters = new object[] { 123, "Four" })]
  79. /// </code>
  80. /// </example>
  81. public object[] NamingStrategyParameters { get; set; }
  82. /// <summary>
  83. /// Gets or sets the null value handling used when serializing this property.
  84. /// </summary>
  85. /// <value>The null value handling.</value>
  86. public NullValueHandling NullValueHandling
  87. {
  88. get => _nullValueHandling ?? default(NullValueHandling);
  89. set => _nullValueHandling = value;
  90. }
  91. /// <summary>
  92. /// Gets or sets the default value handling used when serializing this property.
  93. /// </summary>
  94. /// <value>The default value handling.</value>
  95. public DefaultValueHandling DefaultValueHandling
  96. {
  97. get => _defaultValueHandling ?? default(DefaultValueHandling);
  98. set => _defaultValueHandling = value;
  99. }
  100. /// <summary>
  101. /// Gets or sets the reference loop handling used when serializing this property.
  102. /// </summary>
  103. /// <value>The reference loop handling.</value>
  104. public ReferenceLoopHandling ReferenceLoopHandling
  105. {
  106. get => _referenceLoopHandling ?? default(ReferenceLoopHandling);
  107. set => _referenceLoopHandling = value;
  108. }
  109. /// <summary>
  110. /// Gets or sets the object creation handling used when deserializing this property.
  111. /// </summary>
  112. /// <value>The object creation handling.</value>
  113. public ObjectCreationHandling ObjectCreationHandling
  114. {
  115. get => _objectCreationHandling ?? default(ObjectCreationHandling);
  116. set => _objectCreationHandling = value;
  117. }
  118. /// <summary>
  119. /// Gets or sets the type name handling used when serializing this property.
  120. /// </summary>
  121. /// <value>The type name handling.</value>
  122. public TypeNameHandling TypeNameHandling
  123. {
  124. get => _typeNameHandling ?? default(TypeNameHandling);
  125. set => _typeNameHandling = value;
  126. }
  127. /// <summary>
  128. /// Gets or sets whether this property's value is serialized as a reference.
  129. /// </summary>
  130. /// <value>Whether this property's value is serialized as a reference.</value>
  131. public bool IsReference
  132. {
  133. get => _isReference ?? default(bool);
  134. set => _isReference = value;
  135. }
  136. /// <summary>
  137. /// Gets or sets the order of serialization of a member.
  138. /// </summary>
  139. /// <value>The numeric order of serialization.</value>
  140. public int Order
  141. {
  142. get => _order ?? default(int);
  143. set => _order = value;
  144. }
  145. /// <summary>
  146. /// Gets or sets a value indicating whether this property is required.
  147. /// </summary>
  148. /// <value>
  149. /// A value indicating whether this property is required.
  150. /// </value>
  151. public Required Required
  152. {
  153. get => _required ?? Required.Default;
  154. set => _required = value;
  155. }
  156. /// <summary>
  157. /// Gets or sets the name of the property.
  158. /// </summary>
  159. /// <value>The name of the property.</value>
  160. public string PropertyName { get; set; }
  161. /// <summary>
  162. /// Gets or sets the reference loop handling used when serializing the property's collection items.
  163. /// </summary>
  164. /// <value>The collection's items reference loop handling.</value>
  165. public ReferenceLoopHandling ItemReferenceLoopHandling
  166. {
  167. get => _itemReferenceLoopHandling ?? default(ReferenceLoopHandling);
  168. set => _itemReferenceLoopHandling = value;
  169. }
  170. /// <summary>
  171. /// Gets or sets the type name handling used when serializing the property's collection items.
  172. /// </summary>
  173. /// <value>The collection's items type name handling.</value>
  174. public TypeNameHandling ItemTypeNameHandling
  175. {
  176. get => _itemTypeNameHandling ?? default(TypeNameHandling);
  177. set => _itemTypeNameHandling = value;
  178. }
  179. /// <summary>
  180. /// Gets or sets whether this property's collection items are serialized as a reference.
  181. /// </summary>
  182. /// <value>Whether this property's collection items are serialized as a reference.</value>
  183. public bool ItemIsReference
  184. {
  185. get => _itemIsReference ?? default(bool);
  186. set => _itemIsReference = value;
  187. }
  188. /// <summary>
  189. /// Initializes a new instance of the <see cref="JsonPropertyAttribute"/> class.
  190. /// </summary>
  191. public JsonPropertyAttribute()
  192. {
  193. }
  194. /// <summary>
  195. /// Initializes a new instance of the <see cref="JsonPropertyAttribute"/> class with the specified name.
  196. /// </summary>
  197. /// <param name="propertyName">Name of the property.</param>
  198. public JsonPropertyAttribute(string propertyName)
  199. {
  200. PropertyName = propertyName;
  201. }
  202. }
  203. }