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;
35 using System.Reflection;
39 namespace System.Runtime.Serialization.Json
41 public sealed class DataContractJsonSerializer : XmlObjectSerializer
43 const string default_root_name = "root";
45 #region lengthy constructor list
47 public DataContractJsonSerializer (Type type)
48 : this (type, Type.EmptyTypes)
52 public DataContractJsonSerializer (Type type, IEnumerable<Type> knownTypes)
53 : this (type, default_root_name, knownTypes)
57 public DataContractJsonSerializer (Type type, string rootName)
58 : this (type, rootName, Type.EmptyTypes)
62 public DataContractJsonSerializer (Type type, XmlDictionaryString rootName)
63 : this (type, rootName != null ? rootName.Value : default_root_name, Type.EmptyTypes)
67 public DataContractJsonSerializer (Type type, string rootName, IEnumerable<Type> knownTypes)
68 : this (type, rootName, knownTypes, int.MaxValue, false, false)
72 public DataContractJsonSerializer (Type type, XmlDictionaryString rootName, IEnumerable<Type> knownTypes)
73 : this (type, rootName != null ? rootName.Value : default_root_name, knownTypes)
77 DataContractJsonSerializer(Type type, string rootName, IEnumerable<Type> knownTypes, int maxItemsInObjectGraph, bool ignoreExtensionDataObject, bool alwaysEmitTypeInformation)
80 throw new ArgumentNullException ("type");
82 throw new ArgumentNullException ("rootName");
83 if (maxItemsInObjectGraph < 0)
84 throw new ArgumentOutOfRangeException ("maxItemsInObjectGraph");
88 var knownTypesFromAttributes = new List<Type> ();
90 foreach (var attr in type.GetCustomAttributes (typeof (KnownTypeAttribute), false))
91 knownTypesFromAttributes.Add ((attr as KnownTypeAttribute).Type);
93 if (knownTypes != null)
94 knownTypesFromAttributes.AddRange (knownTypes);
96 known_types = new ReadOnlyCollection<Type> (knownTypesFromAttributes);
99 max_items = maxItemsInObjectGraph;
100 ignore_extension = ignoreExtensionDataObject;
101 always_emit_type = alwaysEmitTypeInformation;
104 public DataContractJsonSerializer (Type type, IEnumerable<Type> knownTypes, int maxItemsInObjectGraph, bool ignoreExtensionDataObject, IDataContractSurrogate dataContractSurrogate, bool alwaysEmitTypeInformation)
105 : this (type, default_root_name, knownTypes, maxItemsInObjectGraph, ignoreExtensionDataObject, alwaysEmitTypeInformation)
109 public DataContractJsonSerializer (Type type, string rootName, IEnumerable<Type> knownTypes, int maxItemsInObjectGraph, bool ignoreExtensionDataObject, IDataContractSurrogate dataContractSurrogate, bool alwaysEmitTypeInformation)
110 : this (type, rootName, knownTypes, maxItemsInObjectGraph, ignoreExtensionDataObject, alwaysEmitTypeInformation)
112 surrogate = dataContractSurrogate;
115 public DataContractJsonSerializer (Type type, XmlDictionaryString rootName, IEnumerable<Type> knownTypes, int maxItemsInObjectGraph, bool ignoreExtensionDataObject, IDataContractSurrogate dataContractSurrogate, bool alwaysEmitTypeInformation)
116 : this (type, rootName != null ? rootName.Value : default_root_name, knownTypes, maxItemsInObjectGraph, ignoreExtensionDataObject, dataContractSurrogate, alwaysEmitTypeInformation)
121 public DataContractJsonSerializer (Type type, DataContractJsonSerializerSettings settings)
122 : this (type, settings.RootName, settings.KnownTypes, settings.MaxItemsInObjectGraph, settings.IgnoreExtensionDataObject,
123 settings.DataContractSurrogate, false)
132 ReadOnlyCollection<Type> known_types;
134 bool ignore_extension;
135 bool always_emit_type;
136 IDataContractSurrogate surrogate;
139 public IDataContractSurrogate DataContractSurrogate {
140 get { return surrogate; }
144 public bool IgnoreExtensionDataObject {
145 get { return ignore_extension; }
147 public ReadOnlyCollection<Type> KnownTypes {
148 get { return known_types; }
151 public int MaxItemsInObjectGraph {
152 get { return max_items; }
155 public override bool IsStartObject (XmlReader reader)
158 throw new ArgumentNullException ("reader");
159 reader.MoveToContent ();
160 return reader.IsStartElement (root, String.Empty);
163 public override bool IsStartObject (XmlDictionaryReader reader)
165 return IsStartObject ((XmlReader) reader);
168 public override object ReadObject (Stream stream)
171 var r = (JsonReader) JsonReaderWriterFactory.CreateJsonReader(stream, XmlDictionaryReaderQuotas.Max);
172 r.LameSilverlightLiteralParser = true;
173 return ReadObject(r);
175 return ReadObject (JsonReaderWriterFactory.CreateJsonReader (stream, new XmlDictionaryReaderQuotas ()));
179 public override object ReadObject (XmlDictionaryReader reader)
181 return ReadObject (reader, true);
184 public override object ReadObject (XmlReader reader)
186 return ReadObject (reader, true);
189 public override object ReadObject (XmlDictionaryReader reader, bool verifyObjectName)
191 return ReadObject ((XmlReader) reader, verifyObjectName);
194 public override object ReadObject (XmlReader reader, bool verifyObjectName)
197 throw new ArgumentNullException ("reader");
199 if (verifyObjectName && !IsStartObject (reader))
200 throw new SerializationException (String.Format ("Expected element was '{0}', but the actual input element was '{1}' in namespace '{2}'", root, reader.LocalName, reader.NamespaceURI));
202 return new JsonSerializationReader (this, reader, type, verifyObjectName).ReadRoot ();
203 } catch (SerializationException) {
205 } catch (InvalidDataContractException) {
207 } catch (System.Reflection.TargetInvocationException ex) {
208 throw ex.InnerException;
209 } catch (Exception ex) {
210 throw new SerializationException ("Deserialization has failed", ex);
214 public override void WriteObject (Stream stream, object graph)
216 using (var xw = JsonReaderWriterFactory.CreateJsonWriter (stream))
217 WriteObject (xw, graph);
220 public override void WriteObject (XmlWriter writer, object graph)
223 WriteStartObject (writer, graph);
224 WriteObjectContent (writer, graph);
225 WriteEndObject (writer);
226 } catch (NotImplementedException) {
228 } catch (InvalidDataContractException) {
230 } catch (Exception ex) {
231 throw new SerializationException (String.Format ("There was an error during serialization for object of type {0}", graph != null ? graph.GetType () : null), ex);
235 public override void WriteObject (XmlDictionaryWriter writer, object graph)
237 WriteObject ((XmlWriter) writer, graph);
240 public override void WriteStartObject (XmlDictionaryWriter writer, object graph)
242 WriteStartObject ((XmlWriter) writer, graph);
245 public override void WriteStartObject (XmlWriter writer, object graph)
248 throw new ArgumentNullException ("writer");
249 writer.WriteStartElement (root);
252 public override void WriteObjectContent (XmlDictionaryWriter writer, object graph)
254 WriteObjectContent ((XmlWriter) writer, graph);
257 public override void WriteObjectContent (XmlWriter writer, object graph)
259 new JsonSerializationWriter (this, writer, type, always_emit_type).WriteObjectContent (graph, true, false);
262 public override void WriteEndObject (XmlDictionaryWriter writer)
264 WriteEndObject ((XmlWriter) writer);
267 public override void WriteEndObject (XmlWriter writer)
270 throw new ArgumentNullException ("writer");
271 writer.WriteEndElement ();
276 public DateTimeFormat DateTimeFormat {
277 get { throw new NotImplementedException (); }
281 public EmitTypeInformation EmitTypeInformation {
282 get { throw new NotImplementedException (); }
286 public bool SerializeReadOnlyTypes {
287 get { throw new NotImplementedException (); }
291 public bool UseSimpleDictionaryFormat {
292 get { throw new NotImplementedException (); }