2 // DataContractJsonSerializer.cs
5 // Atsushi Enomoto <atsushi@ximian.com>
7 // Copyright (C) 2007-2008 Novell, Inc (http://www.novell.com)
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
29 using System.Collections;
30 using System.Collections.Generic;
31 using System.Collections.ObjectModel;
32 using System.Globalization;
34 using System.Reflection;
38 namespace System.Runtime.Serialization.Json
40 public sealed class DataContractJsonSerializer : XmlObjectSerializer
42 const string default_root_name = "root";
44 #region lengthy constructor list
46 public DataContractJsonSerializer (Type type)
47 : this (type, Type.EmptyTypes)
51 public DataContractJsonSerializer (Type type, IEnumerable<Type> knownTypes)
52 : this (type, default_root_name, knownTypes)
56 public DataContractJsonSerializer (Type type, string rootName)
57 : this (type, rootName, Type.EmptyTypes)
61 public DataContractJsonSerializer (Type type, XmlDictionaryString rootName)
62 : this (type, rootName != null ? rootName.Value : default_root_name, Type.EmptyTypes)
66 public DataContractJsonSerializer (Type type, string rootName, IEnumerable<Type> knownTypes)
67 : this (type, rootName, knownTypes, int.MaxValue, false, null, true)
71 public DataContractJsonSerializer (Type type, XmlDictionaryString rootName, IEnumerable<Type> knownTypes)
72 : this (type, rootName != null ? rootName.Value : default_root_name, knownTypes)
76 public DataContractJsonSerializer (Type type, IEnumerable<Type> knownTypes, int maxItemsInObjectGraph, bool ignoreExtensionDataObject, IDataContractSurrogate dataContractSurrogate, bool alwaysEmitTypeInformation)
77 : this (type, default_root_name, knownTypes, maxItemsInObjectGraph, ignoreExtensionDataObject, dataContractSurrogate, alwaysEmitTypeInformation)
81 public DataContractJsonSerializer (Type type, string rootName, IEnumerable<Type> knownTypes, int maxItemsInObjectGraph, bool ignoreExtensionDataObject, IDataContractSurrogate dataContractSurrogate, bool alwaysEmitTypeInformation)
84 throw new ArgumentNullException ("type");
86 throw new ArgumentNullException ("rootName");
87 if (maxItemsInObjectGraph < 0)
88 throw new ArgumentOutOfRangeException ("maxItemsInObjectGraph");
90 List<Type> types = new List<Type> ();
92 if (knownTypes != null)
93 types.AddRange (knownTypes);
96 known_types = new ReadOnlyCollection<Type> (types);
98 max_items = maxItemsInObjectGraph;
99 ignore_extension = ignoreExtensionDataObject;
100 surrogate = dataContractSurrogate;
101 always_emit_type = alwaysEmitTypeInformation;
104 public DataContractJsonSerializer (Type type, XmlDictionaryString rootName, IEnumerable<Type> knownTypes, int maxItemsInObjectGraph, bool ignoreExtensionDataObject, IDataContractSurrogate dataContractSurrogate, bool alwaysEmitTypeInformation)
105 : this (type, rootName != null ? rootName.Value : null, knownTypes, maxItemsInObjectGraph, ignoreExtensionDataObject, dataContractSurrogate, alwaysEmitTypeInformation)
113 ReadOnlyCollection<Type> known_types;
115 bool ignore_extension;
116 IDataContractSurrogate surrogate;
117 bool always_emit_type;
120 public IDataContractSurrogate DataContractSurrogate {
121 get { return surrogate; }
125 public bool IgnoreExtensionDataObject {
126 get { return ignore_extension; }
130 public ReadOnlyCollection<Type> KnownTypes {
131 get { return known_types; }
134 public int MaxItemsInObjectGraph {
135 get { return max_items; }
138 public override bool IsStartObject (XmlReader reader)
141 throw new ArgumentNullException ("reader");
142 reader.MoveToContent ();
143 return reader.IsStartElement (root, String.Empty);
146 public override bool IsStartObject (XmlDictionaryReader reader)
148 return IsStartObject ((XmlReader) reader);
151 public override object ReadObject (Stream stream)
153 return ReadObject (JsonReaderWriterFactory.CreateJsonReader (stream, new XmlDictionaryReaderQuotas ()));
156 public override object ReadObject (XmlDictionaryReader reader)
158 return ReadObject (reader, true);
161 public override object ReadObject (XmlReader reader)
163 return ReadObject (reader, true);
166 public override object ReadObject (XmlDictionaryReader reader, bool verifyObjectName)
168 return ReadObject ((XmlReader) reader, verifyObjectName);
171 public override object ReadObject (XmlReader reader, bool verifyObjectName)
174 throw new ArgumentNullException ("reader");
176 if (verifyObjectName && !IsStartObject (reader))
177 throw new SerializationException (String.Format ("Expected element was '{0}', but the actual input element was '{1}' in namespace '{2}'", root, reader.LocalName, reader.NamespaceURI));
179 return new JsonSerializationReader (this, reader, type, verifyObjectName).ReadRoot ();
180 } catch (SerializationException) {
182 } catch (Exception ex) {
183 throw new SerializationException ("Deserialization has failed", ex);
187 public override void WriteObject (Stream stream, object graph)
189 WriteObject (JsonReaderWriterFactory.CreateJsonWriter (stream), graph);
192 public override void WriteObject (XmlWriter writer, object graph)
195 WriteStartObject (writer, graph);
196 WriteObjectContent (writer, graph);
197 WriteEndObject (writer);
198 } catch (NotImplementedException) {
200 } catch (InvalidDataContractException) {
202 } catch (Exception ex) {
203 throw new SerializationException (String.Format ("There was an error during serialization for object of type {0}", graph != null ? graph.GetType () : null), ex);
207 public override void WriteObject (XmlDictionaryWriter writer, object graph)
209 WriteObject ((XmlWriter) writer, graph);
212 public override void WriteStartObject (XmlDictionaryWriter writer, object graph)
214 WriteStartObject ((XmlWriter) writer, graph);
217 public override void WriteStartObject (XmlWriter writer, object graph)
220 throw new ArgumentNullException ("writer");
221 writer.WriteStartElement (root);
224 public override void WriteObjectContent (XmlDictionaryWriter writer, object graph)
226 WriteObjectContent ((XmlWriter) writer, graph);
229 public override void WriteObjectContent (XmlWriter writer, object graph)
231 new JsonSerializationWriter (this, writer, type, always_emit_type).WriteObjectContent (graph, true, false);
234 public override void WriteEndObject (XmlDictionaryWriter writer)
236 WriteEndObject ((XmlWriter) writer);
239 public override void WriteEndObject (XmlWriter writer)
242 throw new ArgumentNullException ("writer");
243 writer.WriteEndElement ();