Merge pull request #1322 from StephenMcConnel/bug23532
[mono.git] / mcs / class / System.ServiceModel.Web / System.Runtime.Serialization.Json / DataContractJsonSerializer.cs
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
39 namespace System.Runtime.Serialization.Json
40 {
41         public sealed class DataContractJsonSerializer : XmlObjectSerializer
42         {
43                 const string default_root_name = "root";
44
45                 #region lengthy constructor list
46
47                 public DataContractJsonSerializer (Type type)
48                         : this (type, Type.EmptyTypes)
49                 {
50                 }
51
52                 public DataContractJsonSerializer (Type type, IEnumerable<Type> knownTypes)
53                         : this (type, default_root_name, knownTypes)
54                 {
55                 }
56
57                 public DataContractJsonSerializer (Type type, string rootName)
58                         : this (type, rootName, Type.EmptyTypes)
59                 {
60                 }
61
62                 public DataContractJsonSerializer (Type type, XmlDictionaryString rootName)
63                         : this (type, rootName != null ? rootName.Value : default_root_name, Type.EmptyTypes)
64                 {
65                 }
66
67                 public DataContractJsonSerializer (Type type, string rootName, IEnumerable<Type> knownTypes)
68                         : this (type, rootName, knownTypes, int.MaxValue, false, false)
69                 {
70                 }
71
72                 public DataContractJsonSerializer (Type type, XmlDictionaryString rootName, IEnumerable<Type> knownTypes)
73                         : this (type, rootName != null ? rootName.Value : default_root_name, knownTypes)
74                 {
75                 }
76
77                 DataContractJsonSerializer(Type type, string rootName, IEnumerable<Type> knownTypes, int maxItemsInObjectGraph, bool ignoreExtensionDataObject, bool alwaysEmitTypeInformation)
78                 {
79                         if (type == null)
80                                 throw new ArgumentNullException ("type");
81                         if (rootName == null)
82                                 throw new ArgumentNullException ("rootName");
83                         if (maxItemsInObjectGraph < 0)
84                                 throw new ArgumentOutOfRangeException ("maxItemsInObjectGraph");
85
86                         this.type = type;
87
88                         var knownTypesFromAttributes = new List<Type> ();
89
90                         foreach (var attr in type.GetCustomAttributes (typeof (KnownTypeAttribute), false))
91                                 knownTypesFromAttributes.Add ((attr as KnownTypeAttribute).Type);
92
93                         if (knownTypes != null)
94                                 knownTypesFromAttributes.AddRange (knownTypes);
95
96                         known_types = new ReadOnlyCollection<Type> (knownTypesFromAttributes);
97
98                         root = rootName;
99                         max_items = maxItemsInObjectGraph;
100                         ignore_extension = ignoreExtensionDataObject;
101                         always_emit_type = alwaysEmitTypeInformation;
102                 }
103
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)
106                 {
107         }
108
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)
111                 {
112                         surrogate = dataContractSurrogate;
113                 }
114
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)
117                 {
118                 }
119
120 #if NET_4_5
121                 public DataContractJsonSerializer (Type type, DataContractJsonSerializerSettings settings)
122                         : this (type, settings.RootName, settings.KnownTypes, settings.MaxItemsInObjectGraph, settings.IgnoreExtensionDataObject,
123                                 settings.DataContractSurrogate, false)
124                 {
125                 }
126 #endif
127
128         #endregion
129
130         Type type;
131                 string root;
132                 ReadOnlyCollection<Type> known_types;
133                 int max_items;
134                 bool ignore_extension;
135                 bool always_emit_type;
136                 IDataContractSurrogate surrogate;
137
138                 [MonoTODO]
139                 public IDataContractSurrogate DataContractSurrogate {
140                         get { return surrogate; }
141                 }
142
143                 [MonoTODO]
144                 public bool IgnoreExtensionDataObject {
145                         get { return ignore_extension; }
146                 }
147                 public ReadOnlyCollection<Type> KnownTypes {
148                         get { return known_types; }
149                 }
150
151                 public int MaxItemsInObjectGraph {
152                         get { return max_items; }
153                 }
154
155                 public override bool IsStartObject (XmlReader reader)
156                 {
157                         if (reader == null)
158                                 throw new ArgumentNullException ("reader");
159                         reader.MoveToContent ();
160                         return reader.IsStartElement (root, String.Empty);
161                 }
162
163                 public override bool IsStartObject (XmlDictionaryReader reader)
164                 {
165                         return IsStartObject ((XmlReader) reader);
166                 }
167
168                 public override object ReadObject (Stream stream)
169                 {
170 #if NET_2_1
171                         var r = (JsonReader) JsonReaderWriterFactory.CreateJsonReader(stream, XmlDictionaryReaderQuotas.Max);
172                         r.LameSilverlightLiteralParser = true;
173                         return ReadObject(r);
174 #else
175                         return ReadObject (JsonReaderWriterFactory.CreateJsonReader (stream, new XmlDictionaryReaderQuotas ()));
176 #endif
177                 }
178
179                 public override object ReadObject (XmlDictionaryReader reader)
180                 {
181                         return ReadObject (reader, true);
182                 }
183
184                 public override object ReadObject (XmlReader reader)
185                 {
186                         return ReadObject (reader, true);
187                 }
188
189                 public override object ReadObject (XmlDictionaryReader reader, bool verifyObjectName)
190                 {
191                         return ReadObject ((XmlReader) reader, verifyObjectName);
192                 }
193
194                 public override object ReadObject (XmlReader reader, bool verifyObjectName)
195                 {
196                         if (reader == null)
197                                 throw new ArgumentNullException ("reader");
198                         try {
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));
201
202                                 return new JsonSerializationReader (this, reader, type, verifyObjectName).ReadRoot ();
203                         } catch (SerializationException) {
204                                 throw;
205                         } catch (InvalidDataContractException) {
206                                 throw;
207                         } catch (System.Reflection.TargetInvocationException ex) {
208                                 throw ex.InnerException;
209                         } catch (Exception ex) {
210                                 throw new SerializationException ("Deserialization has failed", ex);
211                         }
212                 }
213
214                 public override void WriteObject (Stream stream, object graph)
215                 {
216                         using (var xw = JsonReaderWriterFactory.CreateJsonWriter (stream))
217                                 WriteObject (xw, graph);
218                 }
219
220                 public override void WriteObject (XmlWriter writer, object graph)
221                 {
222                         try {
223                                 WriteStartObject (writer, graph);
224                                 WriteObjectContent (writer, graph);
225                                 WriteEndObject (writer);
226                         } catch (NotImplementedException) {
227                                 throw;
228                         } catch (InvalidDataContractException) {
229                                 throw;
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);
232                         }
233                 }
234
235                 public override void WriteObject (XmlDictionaryWriter writer, object graph)
236                 {
237                         WriteObject ((XmlWriter) writer, graph);
238                 }
239
240                 public override void WriteStartObject (XmlDictionaryWriter writer, object graph)
241                 {
242                         WriteStartObject ((XmlWriter) writer, graph);
243                 }
244
245                 public override void WriteStartObject (XmlWriter writer, object graph)
246                 {
247                         if (writer == null)
248                                 throw new ArgumentNullException ("writer");
249                         writer.WriteStartElement (root);
250                 }
251
252                 public override void WriteObjectContent (XmlDictionaryWriter writer, object graph)
253                 {
254                         WriteObjectContent ((XmlWriter) writer, graph);
255                 }
256
257                 public override void WriteObjectContent (XmlWriter writer, object graph)
258                 {
259                         new JsonSerializationWriter (this, writer, type, always_emit_type).WriteObjectContent (graph, true, false);
260                 }
261
262                 public override void WriteEndObject (XmlDictionaryWriter writer)
263                 {
264                         WriteEndObject ((XmlWriter) writer);
265                 }
266
267                 public override void WriteEndObject (XmlWriter writer)
268                 {
269                         if (writer == null)
270                                 throw new ArgumentNullException ("writer");
271                         writer.WriteEndElement ();
272                 }
273
274 #if NET_4_5
275                 [MonoTODO]
276                 public DateTimeFormat DateTimeFormat {
277                         get { throw new NotImplementedException (); }
278                 }
279
280                 [MonoTODO]
281                 public EmitTypeInformation EmitTypeInformation {
282                         get { throw new NotImplementedException (); }
283                 }
284
285                 [MonoTODO]
286                 public bool SerializeReadOnlyTypes {
287                         get { throw new NotImplementedException (); }
288                 }
289
290                 [MonoTODO]
291                 public bool UseSimpleDictionaryFormat {
292                         get { throw new NotImplementedException (); }
293                 }
294 #endif
295
296         }
297 }