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.

192 lines
7.3 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.Globalization;
  27. using System.Reflection;
  28. using System.Runtime.Serialization;
  29. using System.Security;
  30. using Newtonsoft.Json.Linq;
  31. using Newtonsoft.Json.Utilities;
  32. namespace Newtonsoft.Json.Serialization
  33. {
  34. /// <summary>
  35. /// Contract details for a <see cref="System.Type"/> used by the <see cref="JsonSerializer"/>.
  36. /// </summary>
  37. internal class JsonObjectContract : JsonContainerContract
  38. {
  39. /// <summary>
  40. /// Gets or sets the object member serialization.
  41. /// </summary>
  42. /// <value>The member object serialization.</value>
  43. public MemberSerialization MemberSerialization { get; set; }
  44. /// <summary>
  45. /// Gets or sets a value that indicates whether the object's properties are required.
  46. /// </summary>
  47. /// <value>
  48. /// A value indicating whether the object's properties are required.
  49. /// </value>
  50. public Required? ItemRequired { get; set; }
  51. /// <summary>
  52. /// Gets or sets how the object's properties with null values are handled during serialization and deserialization.
  53. /// </summary>
  54. /// <value>How the object's properties with null values are handled during serialization and deserialization.</value>
  55. public NullValueHandling? ItemNullValueHandling { get; set; }
  56. /// <summary>
  57. /// Gets the object's properties.
  58. /// </summary>
  59. /// <value>The object's properties.</value>
  60. public JsonPropertyCollection Properties { get; }
  61. /// <summary>
  62. /// Gets a collection of <see cref="JsonProperty"/> instances that define the parameters used with <see cref="JsonObjectContract.OverrideCreator"/>.
  63. /// </summary>
  64. public JsonPropertyCollection CreatorParameters
  65. {
  66. get
  67. {
  68. if (_creatorParameters == null)
  69. {
  70. _creatorParameters = new JsonPropertyCollection(UnderlyingType);
  71. }
  72. return _creatorParameters;
  73. }
  74. }
  75. /// <summary>
  76. /// Gets or sets the function used to create the object. When set this function will override <see cref="JsonContract.DefaultCreator"/>.
  77. /// This function is called with a collection of arguments which are defined by the <see cref="JsonObjectContract.CreatorParameters"/> collection.
  78. /// </summary>
  79. /// <value>The function used to create the object.</value>
  80. public ObjectConstructor<object> OverrideCreator
  81. {
  82. get => _overrideCreator;
  83. set => _overrideCreator = value;
  84. }
  85. internal ObjectConstructor<object> ParameterizedCreator
  86. {
  87. get => _parameterizedCreator;
  88. set => _parameterizedCreator = value;
  89. }
  90. /// <summary>
  91. /// Gets or sets the extension data setter.
  92. /// </summary>
  93. public ExtensionDataSetter ExtensionDataSetter { get; set; }
  94. /// <summary>
  95. /// Gets or sets the extension data getter.
  96. /// </summary>
  97. public ExtensionDataGetter ExtensionDataGetter { get; set; }
  98. /// <summary>
  99. /// Gets or sets the extension data value type.
  100. /// </summary>
  101. public Type ExtensionDataValueType
  102. {
  103. get => _extensionDataValueType;
  104. set
  105. {
  106. _extensionDataValueType = value;
  107. ExtensionDataIsJToken = (value != null && typeof(JToken).IsAssignableFrom(value));
  108. }
  109. }
  110. /// <summary>
  111. /// Gets or sets the extension data name resolver.
  112. /// </summary>
  113. /// <value>The extension data name resolver.</value>
  114. public Func<string, string> ExtensionDataNameResolver { get; set; }
  115. internal bool ExtensionDataIsJToken;
  116. private bool? _hasRequiredOrDefaultValueProperties;
  117. private ObjectConstructor<object> _overrideCreator;
  118. private ObjectConstructor<object> _parameterizedCreator;
  119. private JsonPropertyCollection _creatorParameters;
  120. private Type _extensionDataValueType;
  121. internal bool HasRequiredOrDefaultValueProperties
  122. {
  123. get
  124. {
  125. if (_hasRequiredOrDefaultValueProperties == null)
  126. {
  127. _hasRequiredOrDefaultValueProperties = false;
  128. if (ItemRequired.GetValueOrDefault(Required.Default) != Required.Default)
  129. {
  130. _hasRequiredOrDefaultValueProperties = true;
  131. }
  132. else
  133. {
  134. foreach (JsonProperty property in Properties)
  135. {
  136. if (property.Required != Required.Default || (property.DefaultValueHandling & DefaultValueHandling.Populate) == DefaultValueHandling.Populate)
  137. {
  138. _hasRequiredOrDefaultValueProperties = true;
  139. break;
  140. }
  141. }
  142. }
  143. }
  144. return _hasRequiredOrDefaultValueProperties.GetValueOrDefault();
  145. }
  146. }
  147. /// <summary>
  148. /// Initializes a new instance of the <see cref="JsonObjectContract"/> class.
  149. /// </summary>
  150. /// <param name="underlyingType">The underlying type for the contract.</param>
  151. public JsonObjectContract(Type underlyingType)
  152. : base(underlyingType)
  153. {
  154. ContractType = JsonContractType.Object;
  155. Properties = new JsonPropertyCollection(UnderlyingType);
  156. }
  157. #if HAVE_BINARY_FORMATTER
  158. #if !NET20
  159. [SecuritySafeCritical]
  160. #endif
  161. internal object GetUninitializedObject()
  162. {
  163. // we should never get here if the environment is not fully trusted, check just in case
  164. if (!JsonTypeReflector.FullyTrusted)
  165. {
  166. throw new JsonException("Insufficient permissions. Creating an uninitialized '{0}' type requires full trust.".FormatWith(CultureInfo.InvariantCulture, NonNullableUnderlyingType));
  167. }
  168. return FormatterServices.GetUninitializedObject(NonNullableUnderlyingType);
  169. }
  170. #endif
  171. }
  172. }