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.

179 lines
6.9 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"/> how to serialize the object.
  31. /// </summary>
  32. [AttributeUsage(AttributeTargets.Class | AttributeTargets.Interface, AllowMultiple = false)]
  33. internal abstract class JsonContainerAttribute : Attribute
  34. {
  35. /// <summary>
  36. /// Gets or sets the id.
  37. /// </summary>
  38. /// <value>The id.</value>
  39. public string Id { get; set; }
  40. /// <summary>
  41. /// Gets or sets the title.
  42. /// </summary>
  43. /// <value>The title.</value>
  44. public string Title { get; set; }
  45. /// <summary>
  46. /// Gets or sets the description.
  47. /// </summary>
  48. /// <value>The description.</value>
  49. public string Description { get; set; }
  50. /// <summary>
  51. /// Gets or sets the collection's items converter.
  52. /// </summary>
  53. /// <value>The collection's items converter.</value>
  54. public Type ItemConverterType { get; set; }
  55. /// <summary>
  56. /// The parameter list to use when constructing the <see cref="JsonConverter"/> described by <see cref="ItemConverterType"/>.
  57. /// If <c>null</c>, the default constructor is used.
  58. /// When non-<c>null</c>, there must be a constructor defined in the <see cref="JsonConverter"/> that exactly matches the number,
  59. /// order, and type of these parameters.
  60. /// </summary>
  61. /// <example>
  62. /// <code>
  63. /// [JsonContainer(ItemConverterType = typeof(MyContainerConverter), ItemConverterParameters = new object[] { 123, "Four" })]
  64. /// </code>
  65. /// </example>
  66. public object[] ItemConverterParameters { get; set; }
  67. /// <summary>
  68. /// Gets or sets the <see cref="Type"/> of the <see cref="NamingStrategy"/>.
  69. /// </summary>
  70. /// <value>The <see cref="Type"/> of the <see cref="NamingStrategy"/>.</value>
  71. public Type NamingStrategyType
  72. {
  73. get => _namingStrategyType;
  74. set
  75. {
  76. _namingStrategyType = value;
  77. NamingStrategyInstance = null;
  78. }
  79. }
  80. /// <summary>
  81. /// The parameter list to use when constructing the <see cref="NamingStrategy"/> described by <see cref="NamingStrategyType"/>.
  82. /// If <c>null</c>, the default constructor is used.
  83. /// When non-<c>null</c>, there must be a constructor defined in the <see cref="NamingStrategy"/> that exactly matches the number,
  84. /// order, and type of these parameters.
  85. /// </summary>
  86. /// <example>
  87. /// <code>
  88. /// [JsonContainer(NamingStrategyType = typeof(MyNamingStrategy), NamingStrategyParameters = new object[] { 123, "Four" })]
  89. /// </code>
  90. /// </example>
  91. public object[] NamingStrategyParameters
  92. {
  93. get => _namingStrategyParameters;
  94. set
  95. {
  96. _namingStrategyParameters = value;
  97. NamingStrategyInstance = null;
  98. }
  99. }
  100. internal NamingStrategy NamingStrategyInstance { get; set; }
  101. // yuck. can't set nullable properties on an attribute in C#
  102. // have to use this approach to get an unset default state
  103. internal bool? _isReference;
  104. internal bool? _itemIsReference;
  105. internal ReferenceLoopHandling? _itemReferenceLoopHandling;
  106. internal TypeNameHandling? _itemTypeNameHandling;
  107. private Type _namingStrategyType;
  108. private object[] _namingStrategyParameters;
  109. /// <summary>
  110. /// Gets or sets a value that indicates whether to preserve object references.
  111. /// </summary>
  112. /// <value>
  113. /// <c>true</c> to keep object reference; otherwise, <c>false</c>. The default is <c>false</c>.
  114. /// </value>
  115. public bool IsReference
  116. {
  117. get => _isReference ?? default(bool);
  118. set => _isReference = value;
  119. }
  120. /// <summary>
  121. /// Gets or sets a value that indicates whether to preserve collection's items references.
  122. /// </summary>
  123. /// <value>
  124. /// <c>true</c> to keep collection's items object references; otherwise, <c>false</c>. The default is <c>false</c>.
  125. /// </value>
  126. public bool ItemIsReference
  127. {
  128. get => _itemIsReference ?? default(bool);
  129. set => _itemIsReference = value;
  130. }
  131. /// <summary>
  132. /// Gets or sets the reference loop handling used when serializing the collection's items.
  133. /// </summary>
  134. /// <value>The reference loop handling.</value>
  135. public ReferenceLoopHandling ItemReferenceLoopHandling
  136. {
  137. get => _itemReferenceLoopHandling ?? default(ReferenceLoopHandling);
  138. set => _itemReferenceLoopHandling = value;
  139. }
  140. /// <summary>
  141. /// Gets or sets the type name handling used when serializing the collection's items.
  142. /// </summary>
  143. /// <value>The type name handling.</value>
  144. public TypeNameHandling ItemTypeNameHandling
  145. {
  146. get => _itemTypeNameHandling ?? default(TypeNameHandling);
  147. set => _itemTypeNameHandling = value;
  148. }
  149. /// <summary>
  150. /// Initializes a new instance of the <see cref="JsonContainerAttribute"/> class.
  151. /// </summary>
  152. protected JsonContainerAttribute()
  153. {
  154. }
  155. /// <summary>
  156. /// Initializes a new instance of the <see cref="JsonContainerAttribute"/> class with the specified container Id.
  157. /// </summary>
  158. /// <param name="id">The container Id.</param>
  159. protected JsonContainerAttribute(string id)
  160. {
  161. Id = id;
  162. }
  163. }
  164. }