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.

116 lines
4.5 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.Collections.Generic;
  27. using System.Reflection;
  28. using Newtonsoft.Json.Utilities;
  29. using System.Collections;
  30. #if NET20
  31. using Newtonsoft.Json.Utilities.LinqBridge;
  32. #else
  33. using System.Linq;
  34. #endif
  35. namespace Newtonsoft.Json.Serialization
  36. {
  37. /// <summary>
  38. /// Contract details for a <see cref="System.Type"/> used by the <see cref="JsonSerializer"/>.
  39. /// </summary>
  40. internal class JsonContainerContract : JsonContract
  41. {
  42. private JsonContract _itemContract;
  43. private JsonContract _finalItemContract;
  44. // will be null for containers that don't have an item type (e.g. IList) or for complex objects
  45. internal JsonContract ItemContract
  46. {
  47. get => _itemContract;
  48. set
  49. {
  50. _itemContract = value;
  51. if (_itemContract != null)
  52. {
  53. _finalItemContract = (_itemContract.UnderlyingType.IsSealed()) ? _itemContract : null;
  54. }
  55. else
  56. {
  57. _finalItemContract = null;
  58. }
  59. }
  60. }
  61. // the final (i.e. can't be inherited from like a sealed class or valuetype) item contract
  62. internal JsonContract FinalItemContract => _finalItemContract;
  63. /// <summary>
  64. /// Gets or sets the default collection items <see cref="JsonConverter" />.
  65. /// </summary>
  66. /// <value>The converter.</value>
  67. public JsonConverter ItemConverter { get; set; }
  68. /// <summary>
  69. /// Gets or sets a value indicating whether the collection items preserve object references.
  70. /// </summary>
  71. /// <value><c>true</c> if collection items preserve object references; otherwise, <c>false</c>.</value>
  72. public bool? ItemIsReference { get; set; }
  73. /// <summary>
  74. /// Gets or sets the collection item reference loop handling.
  75. /// </summary>
  76. /// <value>The reference loop handling.</value>
  77. public ReferenceLoopHandling? ItemReferenceLoopHandling { get; set; }
  78. /// <summary>
  79. /// Gets or sets the collection item type name handling.
  80. /// </summary>
  81. /// <value>The type name handling.</value>
  82. public TypeNameHandling? ItemTypeNameHandling { get; set; }
  83. /// <summary>
  84. /// Initializes a new instance of the <see cref="JsonContainerContract"/> class.
  85. /// </summary>
  86. /// <param name="underlyingType">The underlying type for the contract.</param>
  87. internal JsonContainerContract(Type underlyingType)
  88. : base(underlyingType)
  89. {
  90. JsonContainerAttribute jsonContainerAttribute = JsonTypeReflector.GetCachedAttribute<JsonContainerAttribute>(underlyingType);
  91. if (jsonContainerAttribute != null)
  92. {
  93. if (jsonContainerAttribute.ItemConverterType != null)
  94. {
  95. ItemConverter = JsonTypeReflector.CreateJsonConverterInstance(
  96. jsonContainerAttribute.ItemConverterType,
  97. jsonContainerAttribute.ItemConverterParameters);
  98. }
  99. ItemIsReference = jsonContainerAttribute._itemIsReference;
  100. ItemReferenceLoopHandling = jsonContainerAttribute._itemReferenceLoopHandling;
  101. ItemTypeNameHandling = jsonContainerAttribute._itemTypeNameHandling;
  102. }
  103. }
  104. }
  105. }