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.

312 lines
11 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 System.Reflection;
  27. using Newtonsoft.Json.Utilities;
  28. #if NET20
  29. using Newtonsoft.Json.Utilities.LinqBridge;
  30. #endif
  31. namespace Newtonsoft.Json.Serialization
  32. {
  33. /// <summary>
  34. /// Maps a JSON property to a .NET member or constructor parameter.
  35. /// </summary>
  36. internal class JsonProperty
  37. {
  38. internal Required? _required;
  39. internal bool _hasExplicitDefaultValue;
  40. private object _defaultValue;
  41. private bool _hasGeneratedDefaultValue;
  42. private string _propertyName;
  43. internal bool _skipPropertyNameEscape;
  44. private Type _propertyType;
  45. // use to cache contract during deserialization
  46. internal JsonContract PropertyContract { get; set; }
  47. /// <summary>
  48. /// Gets or sets the name of the property.
  49. /// </summary>
  50. /// <value>The name of the property.</value>
  51. public string PropertyName
  52. {
  53. get => _propertyName;
  54. set
  55. {
  56. _propertyName = value;
  57. _skipPropertyNameEscape = !JavaScriptUtils.ShouldEscapeJavaScriptString(_propertyName, JavaScriptUtils.HtmlCharEscapeFlags);
  58. }
  59. }
  60. /// <summary>
  61. /// Gets or sets the type that declared this property.
  62. /// </summary>
  63. /// <value>The type that declared this property.</value>
  64. public Type DeclaringType { get; set; }
  65. /// <summary>
  66. /// Gets or sets the order of serialization of a member.
  67. /// </summary>
  68. /// <value>The numeric order of serialization.</value>
  69. public int? Order { get; set; }
  70. /// <summary>
  71. /// Gets or sets the name of the underlying member or parameter.
  72. /// </summary>
  73. /// <value>The name of the underlying member or parameter.</value>
  74. public string UnderlyingName { get; set; }
  75. /// <summary>
  76. /// Gets the <see cref="IValueProvider"/> that will get and set the <see cref="JsonProperty"/> during serialization.
  77. /// </summary>
  78. /// <value>The <see cref="IValueProvider"/> that will get and set the <see cref="JsonProperty"/> during serialization.</value>
  79. public IValueProvider ValueProvider { get; set; }
  80. /// <summary>
  81. /// Gets or sets the <see cref="IAttributeProvider"/> for this property.
  82. /// </summary>
  83. /// <value>The <see cref="IAttributeProvider"/> for this property.</value>
  84. public IAttributeProvider AttributeProvider { get; set; }
  85. /// <summary>
  86. /// Gets or sets the type of the property.
  87. /// </summary>
  88. /// <value>The type of the property.</value>
  89. public Type PropertyType
  90. {
  91. get => _propertyType;
  92. set
  93. {
  94. if (_propertyType != value)
  95. {
  96. _propertyType = value;
  97. _hasGeneratedDefaultValue = false;
  98. }
  99. }
  100. }
  101. /// <summary>
  102. /// Gets or sets the <see cref="JsonConverter" /> for the property.
  103. /// If set this converter takes precedence over the contract converter for the property type.
  104. /// </summary>
  105. /// <value>The converter.</value>
  106. public JsonConverter Converter { get; set; }
  107. /// <summary>
  108. /// Gets or sets the member converter.
  109. /// </summary>
  110. /// <value>The member converter.</value>
  111. [Obsolete("MemberConverter is obsolete. Use Converter instead.")]
  112. public JsonConverter MemberConverter
  113. {
  114. get => Converter;
  115. set => Converter = value;
  116. }
  117. /// <summary>
  118. /// Gets or sets a value indicating whether this <see cref="JsonProperty"/> is ignored.
  119. /// </summary>
  120. /// <value><c>true</c> if ignored; otherwise, <c>false</c>.</value>
  121. public bool Ignored { get; set; }
  122. /// <summary>
  123. /// Gets or sets a value indicating whether this <see cref="JsonProperty"/> is readable.
  124. /// </summary>
  125. /// <value><c>true</c> if readable; otherwise, <c>false</c>.</value>
  126. public bool Readable { get; set; }
  127. /// <summary>
  128. /// Gets or sets a value indicating whether this <see cref="JsonProperty"/> is writable.
  129. /// </summary>
  130. /// <value><c>true</c> if writable; otherwise, <c>false</c>.</value>
  131. public bool Writable { get; set; }
  132. /// <summary>
  133. /// Gets or sets a value indicating whether this <see cref="JsonProperty"/> has a member attribute.
  134. /// </summary>
  135. /// <value><c>true</c> if has a member attribute; otherwise, <c>false</c>.</value>
  136. public bool HasMemberAttribute { get; set; }
  137. /// <summary>
  138. /// Gets the default value.
  139. /// </summary>
  140. /// <value>The default value.</value>
  141. public object DefaultValue
  142. {
  143. get
  144. {
  145. if (!_hasExplicitDefaultValue)
  146. {
  147. return null;
  148. }
  149. return _defaultValue;
  150. }
  151. set
  152. {
  153. _hasExplicitDefaultValue = true;
  154. _defaultValue = value;
  155. }
  156. }
  157. internal object GetResolvedDefaultValue()
  158. {
  159. if (_propertyType == null)
  160. {
  161. return null;
  162. }
  163. if (!_hasExplicitDefaultValue && !_hasGeneratedDefaultValue)
  164. {
  165. _defaultValue = ReflectionUtils.GetDefaultValue(PropertyType);
  166. _hasGeneratedDefaultValue = true;
  167. }
  168. return _defaultValue;
  169. }
  170. /// <summary>
  171. /// Gets or sets a value indicating whether this <see cref="JsonProperty"/> is required.
  172. /// </summary>
  173. /// <value>A value indicating whether this <see cref="JsonProperty"/> is required.</value>
  174. public Required Required
  175. {
  176. get => _required ?? Required.Default;
  177. set => _required = value;
  178. }
  179. /// <summary>
  180. /// Gets or sets a value indicating whether this property preserves object references.
  181. /// </summary>
  182. /// <value>
  183. /// <c>true</c> if this instance is reference; otherwise, <c>false</c>.
  184. /// </value>
  185. public bool? IsReference { get; set; }
  186. /// <summary>
  187. /// Gets or sets the property null value handling.
  188. /// </summary>
  189. /// <value>The null value handling.</value>
  190. public NullValueHandling? NullValueHandling { get; set; }
  191. /// <summary>
  192. /// Gets or sets the property default value handling.
  193. /// </summary>
  194. /// <value>The default value handling.</value>
  195. public DefaultValueHandling? DefaultValueHandling { get; set; }
  196. /// <summary>
  197. /// Gets or sets the property reference loop handling.
  198. /// </summary>
  199. /// <value>The reference loop handling.</value>
  200. public ReferenceLoopHandling? ReferenceLoopHandling { get; set; }
  201. /// <summary>
  202. /// Gets or sets the property object creation handling.
  203. /// </summary>
  204. /// <value>The object creation handling.</value>
  205. public ObjectCreationHandling? ObjectCreationHandling { get; set; }
  206. /// <summary>
  207. /// Gets or sets or sets the type name handling.
  208. /// </summary>
  209. /// <value>The type name handling.</value>
  210. public TypeNameHandling? TypeNameHandling { get; set; }
  211. /// <summary>
  212. /// Gets or sets a predicate used to determine whether the property should be serialized.
  213. /// </summary>
  214. /// <value>A predicate used to determine whether the property should be serialized.</value>
  215. public Predicate<object> ShouldSerialize { get; set; }
  216. /// <summary>
  217. /// Gets or sets a predicate used to determine whether the property should be deserialized.
  218. /// </summary>
  219. /// <value>A predicate used to determine whether the property should be deserialized.</value>
  220. public Predicate<object> ShouldDeserialize { get; set; }
  221. /// <summary>
  222. /// Gets or sets a predicate used to determine whether the property should be serialized.
  223. /// </summary>
  224. /// <value>A predicate used to determine whether the property should be serialized.</value>
  225. public Predicate<object> GetIsSpecified { get; set; }
  226. /// <summary>
  227. /// Gets or sets an action used to set whether the property has been deserialized.
  228. /// </summary>
  229. /// <value>An action used to set whether the property has been deserialized.</value>
  230. public Action<object, object> SetIsSpecified { get; set; }
  231. /// <summary>
  232. /// Returns a <see cref="String"/> that represents this instance.
  233. /// </summary>
  234. /// <returns>
  235. /// A <see cref="String"/> that represents this instance.
  236. /// </returns>
  237. public override string ToString()
  238. {
  239. return PropertyName;
  240. }
  241. /// <summary>
  242. /// Gets or sets the converter used when serializing the property's collection items.
  243. /// </summary>
  244. /// <value>The collection's items converter.</value>
  245. public JsonConverter ItemConverter { get; set; }
  246. /// <summary>
  247. /// Gets or sets whether this property's collection items are serialized as a reference.
  248. /// </summary>
  249. /// <value>Whether this property's collection items are serialized as a reference.</value>
  250. public bool? ItemIsReference { get; set; }
  251. /// <summary>
  252. /// Gets or sets the type name handling used when serializing the property's collection items.
  253. /// </summary>
  254. /// <value>The collection's items type name handling.</value>
  255. public TypeNameHandling? ItemTypeNameHandling { get; set; }
  256. /// <summary>
  257. /// Gets or sets the reference loop handling used when serializing the property's collection items.
  258. /// </summary>
  259. /// <value>The collection's items reference loop handling.</value>
  260. public ReferenceLoopHandling? ItemReferenceLoopHandling { get; set; }
  261. internal void WritePropertyName(JsonWriter writer)
  262. {
  263. if (_skipPropertyNameEscape)
  264. {
  265. writer.WritePropertyName(PropertyName, false);
  266. }
  267. else
  268. {
  269. writer.WritePropertyName(PropertyName);
  270. }
  271. }
  272. }
  273. }