PageRenderTime 45ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/Newtonsoft.Json/Modules/Converters/ExpandoObjectConverter.cs

https://bitbucket.org/VirtualReality/3rdparty-addon-modules
C# | 165 lines | 92 code | 20 blank | 53 comment | 14 complexity | fd3dadce013bc66dfc1bf934d1964098 MD5 | raw file
  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 !(NET35 || NET20 || WINDOWS_PHONE || PORTABLE)
  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 ExpandoObject to and from JSON.
  37. /// </summary>
  38. public 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. while (reader.TokenType == JsonToken.Comment)
  65. {
  66. if (!reader.Read())
  67. throw JsonSerializationException.Create(reader, "Unexpected end when reading ExpandoObject.");
  68. }
  69. switch (reader.TokenType)
  70. {
  71. case JsonToken.StartObject:
  72. return ReadObject(reader);
  73. case JsonToken.StartArray:
  74. return ReadList(reader);
  75. default:
  76. if (JsonReader.IsPrimitiveToken(reader.TokenType))
  77. return reader.Value;
  78. throw JsonSerializationException.Create(reader, "Unexpected token when converting ExpandoObject: {0}".FormatWith(CultureInfo.InvariantCulture, reader.TokenType));
  79. }
  80. }
  81. private object ReadList(JsonReader reader)
  82. {
  83. IList<object> list = new List<object>();
  84. while (reader.Read())
  85. {
  86. switch (reader.TokenType)
  87. {
  88. case JsonToken.Comment:
  89. break;
  90. default:
  91. object v = ReadValue(reader);
  92. list.Add(v);
  93. break;
  94. case JsonToken.EndArray:
  95. return list;
  96. }
  97. }
  98. throw JsonSerializationException.Create(reader, "Unexpected end when reading ExpandoObject.");
  99. }
  100. private object ReadObject(JsonReader reader)
  101. {
  102. IDictionary<string, object> expandoObject = new ExpandoObject();
  103. while (reader.Read())
  104. {
  105. switch (reader.TokenType)
  106. {
  107. case JsonToken.PropertyName:
  108. string propertyName = reader.Value.ToString();
  109. if (!reader.Read())
  110. throw JsonSerializationException.Create(reader, "Unexpected end when reading ExpandoObject.");
  111. object v = ReadValue(reader);
  112. expandoObject[propertyName] = v;
  113. break;
  114. case JsonToken.Comment:
  115. break;
  116. case JsonToken.EndObject:
  117. return expandoObject;
  118. }
  119. }
  120. throw JsonSerializationException.Create(reader, "Unexpected end when reading ExpandoObject.");
  121. }
  122. /// <summary>
  123. /// Determines whether this instance can convert the specified object type.
  124. /// </summary>
  125. /// <param name="objectType">Type of the object.</param>
  126. /// <returns>
  127. /// <c>true</c> if this instance can convert the specified object type; otherwise, <c>false</c>.
  128. /// </returns>
  129. public override bool CanConvert(Type objectType)
  130. {
  131. return (objectType == typeof (ExpandoObject));
  132. }
  133. /// <summary>
  134. /// Gets a value indicating whether this <see cref="JsonConverter"/> can write JSON.
  135. /// </summary>
  136. /// <value>
  137. /// <c>true</c> if this <see cref="JsonConverter"/> can write JSON; otherwise, <c>false</c>.
  138. /// </value>
  139. public override bool CanWrite
  140. {
  141. get { return false; }
  142. }
  143. }
  144. }
  145. #endif