/mcs/class/System.ServiceModel.Web/System.Runtime.Serialization.Json/DataContractJsonSerializer.cs

https://github.com/catlion/mono · C# · 254 lines · 191 code · 36 blank · 27 comment · 20 complexity · eaaedcdae05423abd5442e0caf1d2c48 MD5 · raw file

  1. //
  2. // DataContractJsonSerializer.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <atsushi@ximian.com>
  6. //
  7. // Copyright (C) 2007-2008 Novell, Inc (http://www.novell.com)
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. using System.Collections;
  30. using System.Collections.Generic;
  31. using System.Collections.ObjectModel;
  32. using System.Globalization;
  33. using System.IO;
  34. using System.Linq;
  35. using System.Reflection;
  36. using System.Text;
  37. using System.Xml;
  38. namespace System.Runtime.Serialization.Json
  39. {
  40. public sealed class DataContractJsonSerializer : XmlObjectSerializer
  41. {
  42. const string default_root_name = "root";
  43. #region lengthy constructor list
  44. public DataContractJsonSerializer (Type type)
  45. : this (type, Type.EmptyTypes)
  46. {
  47. }
  48. public DataContractJsonSerializer (Type type, IEnumerable<Type> knownTypes)
  49. : this (type, default_root_name, knownTypes)
  50. {
  51. }
  52. public DataContractJsonSerializer (Type type, string rootName)
  53. : this (type, rootName, Type.EmptyTypes)
  54. {
  55. }
  56. public DataContractJsonSerializer (Type type, XmlDictionaryString rootName)
  57. : this (type, rootName != null ? rootName.Value : default_root_name, Type.EmptyTypes)
  58. {
  59. }
  60. public DataContractJsonSerializer (Type type, string rootName, IEnumerable<Type> knownTypes)
  61. : this (type, rootName, knownTypes, int.MaxValue, false, false)
  62. {
  63. }
  64. public DataContractJsonSerializer (Type type, XmlDictionaryString rootName, IEnumerable<Type> knownTypes)
  65. : this (type, rootName != null ? rootName.Value : default_root_name, knownTypes)
  66. {
  67. }
  68. DataContractJsonSerializer(Type type, string rootName, IEnumerable<Type> knownTypes, int maxItemsInObjectGraph, bool ignoreExtensionDataObject, bool alwaysEmitTypeInformation)
  69. {
  70. if (type == null)
  71. throw new ArgumentNullException ("type");
  72. if (rootName == null)
  73. throw new ArgumentNullException ("rootName");
  74. if (maxItemsInObjectGraph < 0)
  75. throw new ArgumentOutOfRangeException ("maxItemsInObjectGraph");
  76. this.type = type;
  77. known_types = new ReadOnlyCollection<Type> (knownTypes != null ? knownTypes.ToArray () : Type.EmptyTypes);
  78. root = rootName;
  79. max_items = maxItemsInObjectGraph;
  80. ignore_extension = ignoreExtensionDataObject;
  81. always_emit_type = alwaysEmitTypeInformation;
  82. }
  83. public DataContractJsonSerializer (Type type, IEnumerable<Type> knownTypes, int maxItemsInObjectGraph, bool ignoreExtensionDataObject, IDataContractSurrogate dataContractSurrogate, bool alwaysEmitTypeInformation)
  84. : this (type, default_root_name, knownTypes, maxItemsInObjectGraph, ignoreExtensionDataObject, alwaysEmitTypeInformation)
  85. {
  86. }
  87. public DataContractJsonSerializer (Type type, string rootName, IEnumerable<Type> knownTypes, int maxItemsInObjectGraph, bool ignoreExtensionDataObject, IDataContractSurrogate dataContractSurrogate, bool alwaysEmitTypeInformation)
  88. : this (type, rootName, knownTypes, maxItemsInObjectGraph, ignoreExtensionDataObject, alwaysEmitTypeInformation)
  89. {
  90. surrogate = dataContractSurrogate;
  91. }
  92. public DataContractJsonSerializer (Type type, XmlDictionaryString rootName, IEnumerable<Type> knownTypes, int maxItemsInObjectGraph, bool ignoreExtensionDataObject, IDataContractSurrogate dataContractSurrogate, bool alwaysEmitTypeInformation)
  93. : this (type, rootName != null ? rootName.Value : default_root_name, knownTypes, maxItemsInObjectGraph, ignoreExtensionDataObject, dataContractSurrogate, alwaysEmitTypeInformation)
  94. {
  95. }
  96. #endregion
  97. Type type;
  98. string root;
  99. ReadOnlyCollection<Type> known_types;
  100. int max_items;
  101. bool ignore_extension;
  102. bool always_emit_type;
  103. IDataContractSurrogate surrogate;
  104. [MonoTODO]
  105. public IDataContractSurrogate DataContractSurrogate {
  106. get { return surrogate; }
  107. }
  108. [MonoTODO]
  109. public bool IgnoreExtensionDataObject {
  110. get { return ignore_extension; }
  111. }
  112. [MonoTODO]
  113. public ReadOnlyCollection<Type> KnownTypes {
  114. get { return known_types; }
  115. }
  116. public int MaxItemsInObjectGraph {
  117. get { return max_items; }
  118. }
  119. public override bool IsStartObject (XmlReader reader)
  120. {
  121. if (reader == null)
  122. throw new ArgumentNullException ("reader");
  123. reader.MoveToContent ();
  124. return reader.IsStartElement (root, String.Empty);
  125. }
  126. public override bool IsStartObject (XmlDictionaryReader reader)
  127. {
  128. return IsStartObject ((XmlReader) reader);
  129. }
  130. public override object ReadObject (Stream stream)
  131. {
  132. #if NET_2_1
  133. var r = (JsonReader) JsonReaderWriterFactory.CreateJsonReader(stream, XmlDictionaryReaderQuotas.Max);
  134. r.LameSilverlightLiteralParser = true;
  135. return ReadObject(r);
  136. #else
  137. return ReadObject (JsonReaderWriterFactory.CreateJsonReader (stream, new XmlDictionaryReaderQuotas ()));
  138. #endif
  139. }
  140. public override object ReadObject (XmlDictionaryReader reader)
  141. {
  142. return ReadObject (reader, true);
  143. }
  144. public override object ReadObject (XmlReader reader)
  145. {
  146. return ReadObject (reader, true);
  147. }
  148. public override object ReadObject (XmlDictionaryReader reader, bool verifyObjectName)
  149. {
  150. return ReadObject ((XmlReader) reader, verifyObjectName);
  151. }
  152. public override object ReadObject (XmlReader reader, bool verifyObjectName)
  153. {
  154. if (reader == null)
  155. throw new ArgumentNullException ("reader");
  156. try {
  157. if (verifyObjectName && !IsStartObject (reader))
  158. throw new SerializationException (String.Format ("Expected element was '{0}', but the actual input element was '{1}' in namespace '{2}'", root, reader.LocalName, reader.NamespaceURI));
  159. return new JsonSerializationReader (this, reader, type, verifyObjectName).ReadRoot ();
  160. } catch (SerializationException) {
  161. throw;
  162. } catch (Exception ex) {
  163. throw new SerializationException ("Deserialization has failed", ex);
  164. }
  165. }
  166. public override void WriteObject (Stream stream, object graph)
  167. {
  168. using (var xw = JsonReaderWriterFactory.CreateJsonWriter (stream))
  169. WriteObject (xw, graph);
  170. }
  171. public override void WriteObject (XmlWriter writer, object graph)
  172. {
  173. try {
  174. WriteStartObject (writer, graph);
  175. WriteObjectContent (writer, graph);
  176. WriteEndObject (writer);
  177. } catch (NotImplementedException) {
  178. throw;
  179. } catch (InvalidDataContractException) {
  180. throw;
  181. } catch (Exception ex) {
  182. throw new SerializationException (String.Format ("There was an error during serialization for object of type {0}", graph != null ? graph.GetType () : null), ex);
  183. }
  184. }
  185. public override void WriteObject (XmlDictionaryWriter writer, object graph)
  186. {
  187. WriteObject ((XmlWriter) writer, graph);
  188. }
  189. public override void WriteStartObject (XmlDictionaryWriter writer, object graph)
  190. {
  191. WriteStartObject ((XmlWriter) writer, graph);
  192. }
  193. public override void WriteStartObject (XmlWriter writer, object graph)
  194. {
  195. if (writer == null)
  196. throw new ArgumentNullException ("writer");
  197. writer.WriteStartElement (root);
  198. }
  199. public override void WriteObjectContent (XmlDictionaryWriter writer, object graph)
  200. {
  201. WriteObjectContent ((XmlWriter) writer, graph);
  202. }
  203. public override void WriteObjectContent (XmlWriter writer, object graph)
  204. {
  205. new JsonSerializationWriter (this, writer, type, always_emit_type).WriteObjectContent (graph, true, false);
  206. }
  207. public override void WriteEndObject (XmlDictionaryWriter writer)
  208. {
  209. WriteEndObject ((XmlWriter) writer);
  210. }
  211. public override void WriteEndObject (XmlWriter writer)
  212. {
  213. if (writer == null)
  214. throw new ArgumentNullException ("writer");
  215. writer.WriteEndElement ();
  216. }
  217. }
  218. }