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.

167 lines
6.0 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. #if !NET20
  26. using System;
  27. using System.Collections.Generic;
  28. using System.Dynamic;
  29. using System.Globalization;
  30. using System.Linq;
  31. using System.Text;
  32. using Newtonsoft.Json.Utilities;
  33. namespace Newtonsoft.Json.Converters
  34. {
  35. /// <summary>
  36. /// Converts an <see cref="ExpandoObject"/> to and from JSON.
  37. /// </summary>
  38. internal class ExpandoObjectConverter : JsonConverter
  39. {
  40. /// <summary>
  41. /// Writes the JSON representation of the object.
  42. /// </summary>
  43. /// <param name="writer">The <see cref="JsonWriter"/> to write to.</param>
  44. /// <param name="value">The value.</param>
  45. /// <param name="serializer">The calling serializer.</param>
  46. public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
  47. {
  48. // can write is set to false
  49. }
  50. /// <summary>
  51. /// Reads the JSON representation of the object.
  52. /// </summary>
  53. /// <param name="reader">The <see cref="JsonReader"/> to read from.</param>
  54. /// <param name="objectType">Type of the object.</param>
  55. /// <param name="existingValue">The existing value of object being read.</param>
  56. /// <param name="serializer">The calling serializer.</param>
  57. /// <returns>The object value.</returns>
  58. public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
  59. {
  60. return ReadValue(reader);
  61. }
  62. private object ReadValue(JsonReader reader)
  63. {
  64. if (!reader.MoveToContent())
  65. {
  66. throw JsonSerializationException.Create(reader, "Unexpected end when reading ExpandoObject.");
  67. }
  68. switch (reader.TokenType)
  69. {
  70. case JsonToken.StartObject:
  71. return ReadObject(reader);
  72. case JsonToken.StartArray:
  73. return ReadList(reader);
  74. default:
  75. if (JsonTokenUtils.IsPrimitiveToken(reader.TokenType))
  76. {
  77. return reader.Value;
  78. }
  79. throw JsonSerializationException.Create(reader, "Unexpected token when converting ExpandoObject: {0}".FormatWith(CultureInfo.InvariantCulture, reader.TokenType));
  80. }
  81. }
  82. private object ReadList(JsonReader reader)
  83. {
  84. IList<object> list = new List<object>();
  85. while (reader.Read())
  86. {
  87. switch (reader.TokenType)
  88. {
  89. case JsonToken.Comment:
  90. break;
  91. default:
  92. object v = ReadValue(reader);
  93. list.Add(v);
  94. break;
  95. case JsonToken.EndArray:
  96. return list;
  97. }
  98. }
  99. throw JsonSerializationException.Create(reader, "Unexpected end when reading ExpandoObject.");
  100. }
  101. private object ReadObject(JsonReader reader)
  102. {
  103. IDictionary<string, object> expandoObject = new ExpandoObject();
  104. while (reader.Read())
  105. {
  106. switch (reader.TokenType)
  107. {
  108. case JsonToken.PropertyName:
  109. string propertyName = reader.Value.ToString();
  110. if (!reader.Read())
  111. {
  112. throw JsonSerializationException.Create(reader, "Unexpected end when reading ExpandoObject.");
  113. }
  114. object v = ReadValue(reader);
  115. expandoObject[propertyName] = v;
  116. break;
  117. case JsonToken.Comment:
  118. break;
  119. case JsonToken.EndObject:
  120. return expandoObject;
  121. }
  122. }
  123. throw JsonSerializationException.Create(reader, "Unexpected end when reading ExpandoObject.");
  124. }
  125. /// <summary>
  126. /// Determines whether this instance can convert the specified object type.
  127. /// </summary>
  128. /// <param name="objectType">Type of the object.</param>
  129. /// <returns>
  130. /// <c>true</c> if this instance can convert the specified object type; otherwise, <c>false</c>.
  131. /// </returns>
  132. public override bool CanConvert(Type objectType)
  133. {
  134. return (objectType == typeof(ExpandoObject));
  135. }
  136. /// <summary>
  137. /// Gets a value indicating whether this <see cref="JsonConverter"/> can write JSON.
  138. /// </summary>
  139. /// <value>
  140. /// <c>true</c> if this <see cref="JsonConverter"/> can write JSON; otherwise, <c>false</c>.
  141. /// </value>
  142. public override bool CanWrite
  143. {
  144. get { return false; }
  145. }
  146. }
  147. }
  148. #endif