DataContractJsonSerializer.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. //
  2. // DataContractJsonSerializer.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  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.Reflection;
  35. using System.Text;
  36. using System.Xml;
  37. namespace System.Runtime.Serialization.Json
  38. {
  39. public sealed class DataContractJsonSerializer : XmlObjectSerializer
  40. {
  41. const string default_root_name = "root";
  42. #region lengthy constructor list
  43. public DataContractJsonSerializer (Type type)
  44. : this (type, Type.EmptyTypes)
  45. {
  46. }
  47. public DataContractJsonSerializer (Type type, IEnumerable<Type> knownTypes)
  48. : this (type, default_root_name, knownTypes)
  49. {
  50. }
  51. public DataContractJsonSerializer (Type type, string rootName)
  52. : this (type, rootName, Type.EmptyTypes)
  53. {
  54. }
  55. public DataContractJsonSerializer (Type type, XmlDictionaryString rootName)
  56. : this (type, rootName != null ? rootName.Value : default_root_name, Type.EmptyTypes)
  57. {
  58. }
  59. public DataContractJsonSerializer (Type type, string rootName, IEnumerable<Type> knownTypes)
  60. : this (type, rootName, knownTypes, int.MaxValue, false, null, true)
  61. {
  62. }
  63. public DataContractJsonSerializer (Type type, XmlDictionaryString rootName, IEnumerable<Type> knownTypes)
  64. : this (type, rootName != null ? rootName.Value : default_root_name, knownTypes)
  65. {
  66. }
  67. public DataContractJsonSerializer (Type type, IEnumerable<Type> knownTypes, int maxItemsInObjectGraph, bool ignoreExtensionDataObject, IDataContractSurrogate dataContractSurrogate, bool alwaysEmitTypeInformation)
  68. : this (type, default_root_name, knownTypes, maxItemsInObjectGraph, ignoreExtensionDataObject, dataContractSurrogate, alwaysEmitTypeInformation)
  69. {
  70. }
  71. public DataContractJsonSerializer (Type type, string rootName, IEnumerable<Type> knownTypes, int maxItemsInObjectGraph, bool ignoreExtensionDataObject, IDataContractSurrogate dataContractSurrogate, bool alwaysEmitTypeInformation)
  72. {
  73. if (type == null)
  74. throw new ArgumentNullException ("type");
  75. if (rootName == null)
  76. throw new ArgumentNullException ("rootName");
  77. if (maxItemsInObjectGraph < 0)
  78. throw new ArgumentOutOfRangeException ("maxItemsInObjectGraph");
  79. List<Type> types = new List<Type> ();
  80. types.Add (type);
  81. if (knownTypes != null)
  82. types.AddRange (knownTypes);
  83. this.type = type;
  84. known_types = new ReadOnlyCollection<Type> (types);
  85. root = rootName;
  86. max_items = maxItemsInObjectGraph;
  87. ignore_extension = ignoreExtensionDataObject;
  88. surrogate = dataContractSurrogate;
  89. always_emit_type = alwaysEmitTypeInformation;
  90. }
  91. public DataContractJsonSerializer (Type type, XmlDictionaryString rootName, IEnumerable<Type> knownTypes, int maxItemsInObjectGraph, bool ignoreExtensionDataObject, IDataContractSurrogate dataContractSurrogate, bool alwaysEmitTypeInformation)
  92. : this (type, rootName != null ? rootName.Value : null, knownTypes, maxItemsInObjectGraph, ignoreExtensionDataObject, dataContractSurrogate, alwaysEmitTypeInformation)
  93. {
  94. }
  95. #endregion
  96. Type type;
  97. string root;
  98. ReadOnlyCollection<Type> known_types;
  99. int max_items;
  100. bool ignore_extension;
  101. IDataContractSurrogate surrogate;
  102. bool always_emit_type;
  103. [MonoTODO]
  104. public IDataContractSurrogate DataContractSurrogate {
  105. get { return surrogate; }
  106. }
  107. [MonoTODO]
  108. public bool IgnoreExtensionDataObject {
  109. get { return ignore_extension; }
  110. }
  111. [MonoTODO]
  112. public ReadOnlyCollection<Type> KnownTypes {
  113. get { return known_types; }
  114. }
  115. public int MaxItemsInObjectGraph {
  116. get { return max_items; }
  117. }
  118. public override bool IsStartObject (XmlReader reader)
  119. {
  120. if (reader == null)
  121. throw new ArgumentNullException ("reader");
  122. reader.MoveToContent ();
  123. return reader.IsStartElement (root, String.Empty);
  124. }
  125. public override bool IsStartObject (XmlDictionaryReader reader)
  126. {
  127. return IsStartObject ((XmlReader) reader);
  128. }
  129. public override object ReadObject (Stream stream)
  130. {
  131. return ReadObject (JsonReaderWriterFactory.CreateJsonReader (stream, new XmlDictionaryReaderQuotas ()));
  132. }
  133. public override object ReadObject (XmlDictionaryReader reader)
  134. {
  135. return ReadObject (reader, true);
  136. }
  137. public override object ReadObject (XmlReader reader)
  138. {
  139. return ReadObject (reader, true);
  140. }
  141. public override object ReadObject (XmlDictionaryReader reader, bool verifyObjectName)
  142. {
  143. return ReadObject ((XmlReader) reader, verifyObjectName);
  144. }
  145. public override object ReadObject (XmlReader reader, bool verifyObjectName)
  146. {
  147. if (reader == null)
  148. throw new ArgumentNullException ("reader");
  149. try {
  150. if (verifyObjectName && !IsStartObject (reader))
  151. throw new SerializationException (String.Format ("Expected element was '{0}', but the actual input element was '{1}' in namespace '{2}'", root, reader.LocalName, reader.NamespaceURI));
  152. return new JsonSerializationReader (this, reader, type, verifyObjectName).ReadRoot ();
  153. } catch (SerializationException) {
  154. throw;
  155. } catch (Exception ex) {
  156. throw new SerializationException ("Deserialization has failed", ex);
  157. }
  158. }
  159. public override void WriteObject (Stream stream, object graph)
  160. {
  161. using (var xw = JsonReaderWriterFactory.CreateJsonWriter (stream))
  162. WriteObject (xw, graph);
  163. }
  164. public override void WriteObject (XmlWriter writer, object graph)
  165. {
  166. try {
  167. WriteStartObject (writer, graph);
  168. WriteObjectContent (writer, graph);
  169. WriteEndObject (writer);
  170. } catch (NotImplementedException) {
  171. throw;
  172. } catch (InvalidDataContractException) {
  173. throw;
  174. } catch (Exception ex) {
  175. throw new SerializationException (String.Format ("There was an error during serialization for object of type {0}", graph != null ? graph.GetType () : null), ex);
  176. }
  177. }
  178. public override void WriteObject (XmlDictionaryWriter writer, object graph)
  179. {
  180. WriteObject ((XmlWriter) writer, graph);
  181. }
  182. public override void WriteStartObject (XmlDictionaryWriter writer, object graph)
  183. {
  184. WriteStartObject ((XmlWriter) writer, graph);
  185. }
  186. public override void WriteStartObject (XmlWriter writer, object graph)
  187. {
  188. if (writer == null)
  189. throw new ArgumentNullException ("writer");
  190. writer.WriteStartElement (root);
  191. }
  192. public override void WriteObjectContent (XmlDictionaryWriter writer, object graph)
  193. {
  194. WriteObjectContent ((XmlWriter) writer, graph);
  195. }
  196. public override void WriteObjectContent (XmlWriter writer, object graph)
  197. {
  198. new JsonSerializationWriter (this, writer, type, always_emit_type).WriteObjectContent (graph, true, false);
  199. }
  200. public override void WriteEndObject (XmlDictionaryWriter writer)
  201. {
  202. WriteEndObject ((XmlWriter) writer);
  203. }
  204. public override void WriteEndObject (XmlWriter writer)
  205. {
  206. if (writer == null)
  207. throw new ArgumentNullException ("writer");
  208. writer.WriteEndElement ();
  209. }
  210. }
  211. }