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.

146 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.Utilities;
  27. using System.Globalization;
  28. namespace Newtonsoft.Json
  29. {
  30. /// <summary>
  31. /// Converts an object to and from JSON.
  32. /// </summary>
  33. internal abstract class JsonConverter
  34. {
  35. /// <summary>
  36. /// Writes the JSON representation of the object.
  37. /// </summary>
  38. /// <param name="writer">The <see cref="JsonWriter"/> to write to.</param>
  39. /// <param name="value">The value.</param>
  40. /// <param name="serializer">The calling serializer.</param>
  41. public abstract void WriteJson(JsonWriter writer, object value, JsonSerializer serializer);
  42. /// <summary>
  43. /// Reads the JSON representation of the object.
  44. /// </summary>
  45. /// <param name="reader">The <see cref="JsonReader"/> to read from.</param>
  46. /// <param name="objectType">Type of the object.</param>
  47. /// <param name="existingValue">The existing value of object being read.</param>
  48. /// <param name="serializer">The calling serializer.</param>
  49. /// <returns>The object value.</returns>
  50. public abstract object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer);
  51. /// <summary>
  52. /// Determines whether this instance can convert the specified object type.
  53. /// </summary>
  54. /// <param name="objectType">Type of the object.</param>
  55. /// <returns>
  56. /// <c>true</c> if this instance can convert the specified object type; otherwise, <c>false</c>.
  57. /// </returns>
  58. public abstract bool CanConvert(Type objectType);
  59. /// <summary>
  60. /// Gets a value indicating whether this <see cref="JsonConverter"/> can read JSON.
  61. /// </summary>
  62. /// <value><c>true</c> if this <see cref="JsonConverter"/> can read JSON; otherwise, <c>false</c>.</value>
  63. public virtual bool CanRead => true;
  64. /// <summary>
  65. /// Gets a value indicating whether this <see cref="JsonConverter"/> can write JSON.
  66. /// </summary>
  67. /// <value><c>true</c> if this <see cref="JsonConverter"/> can write JSON; otherwise, <c>false</c>.</value>
  68. public virtual bool CanWrite => true;
  69. }
  70. /// <summary>
  71. /// Converts an object to and from JSON.
  72. /// </summary>
  73. /// <typeparam name="T">The object type to convert.</typeparam>
  74. internal abstract class JsonConverter<T> : JsonConverter
  75. {
  76. /// <summary>
  77. /// Writes the JSON representation of the object.
  78. /// </summary>
  79. /// <param name="writer">The <see cref="JsonWriter"/> to write to.</param>
  80. /// <param name="value">The value.</param>
  81. /// <param name="serializer">The calling serializer.</param>
  82. public sealed override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
  83. {
  84. if (!(value != null ? value is T : ReflectionUtils.IsNullable(typeof(T))))
  85. {
  86. throw new JsonSerializationException("Converter cannot write specified value to JSON. {0} is required.".FormatWith(CultureInfo.InvariantCulture, typeof(T)));
  87. }
  88. WriteJson(writer, (T)value, serializer);
  89. }
  90. /// <summary>
  91. /// Writes the JSON representation of the object.
  92. /// </summary>
  93. /// <param name="writer">The <see cref="JsonWriter"/> to write to.</param>
  94. /// <param name="value">The value.</param>
  95. /// <param name="serializer">The calling serializer.</param>
  96. public abstract void WriteJson(JsonWriter writer, T value, JsonSerializer serializer);
  97. /// <summary>
  98. /// Reads the JSON representation of the object.
  99. /// </summary>
  100. /// <param name="reader">The <see cref="JsonReader"/> to read from.</param>
  101. /// <param name="objectType">Type of the object.</param>
  102. /// <param name="existingValue">The existing value of object being read.</param>
  103. /// <param name="serializer">The calling serializer.</param>
  104. /// <returns>The object value.</returns>
  105. public sealed override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
  106. {
  107. bool existingIsNull = existingValue == null;
  108. if (!(existingIsNull || existingValue is T))
  109. {
  110. throw new JsonSerializationException("Converter cannot read JSON with the specified existing value. {0} is required.".FormatWith(CultureInfo.InvariantCulture, typeof(T)));
  111. }
  112. return ReadJson(reader, objectType, existingIsNull ? default(T) : (T)existingValue, !existingIsNull, serializer);
  113. }
  114. /// <summary>
  115. /// Reads the JSON representation of the object.
  116. /// </summary>
  117. /// <param name="reader">The <see cref="JsonReader"/> to read from.</param>
  118. /// <param name="objectType">Type of the object.</param>
  119. /// <param name="existingValue">The existing value of object being read. If there is no existing value then <c>null</c> will be used.</param>
  120. /// <param name="hasExistingValue">The existing value has a value.</param>
  121. /// <param name="serializer">The calling serializer.</param>
  122. /// <returns>The object value.</returns>
  123. public abstract T ReadJson(JsonReader reader, Type objectType, T existingValue, bool hasExistingValue, JsonSerializer serializer);
  124. /// <summary>
  125. /// Determines whether this instance can convert the specified object type.
  126. /// </summary>
  127. /// <param name="objectType">Type of the object.</param>
  128. /// <returns>
  129. /// <c>true</c> if this instance can convert the specified object type; otherwise, <c>false</c>.
  130. /// </returns>
  131. public sealed override bool CanConvert(Type objectType)
  132. {
  133. return typeof(T).IsAssignableFrom(objectType);
  134. }
  135. }
  136. }