2010-03-09 Atsushi Enomoto <atsushi@ximian.com>
[mono.git] / mcs / class / System.ServiceModel.Web / System.Runtime.Serialization.Json / DataContractJsonSerializer_2_1.cs
1 //
2 // DataContractJsonSerializer.cs (for Moonlight profile)
3 //
4 // Authors:
5 //      Atsushi Enomoto  <atsushi@ximian.com>
6 //      Sebastien Pouliot  <sebastien@ximian.com>
7 //
8 // Copyright (C) 2007-2008, 2010 Novell, Inc (http://www.novell.com)
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29
30 using System;
31 using System.Collections.Generic;
32 using System.Collections.ObjectModel;
33 using System.IO;
34 using System.Security;
35 using System.Xml;
36
37 namespace System.Runtime.Serialization.Json {
38
39         public sealed class DataContractJsonSerializer {
40
41                 private Type type;
42                 private ReadOnlyCollection<Type> known_types;
43
44                 public DataContractJsonSerializer (Type type) :
45                         this (type, null)
46                 {
47                 }
48
49                 [MonoTODO ("The 'knownTypes' parameter is unused (except as a property)")]
50                 public DataContractJsonSerializer (Type type, IEnumerable<Type> knownTypes)
51                 {
52                         if (type == null)
53                                 throw new ArgumentNullException ("type");
54
55                         this.type = type;
56                         List<Type> types = new List<Type> ();
57                         if (knownTypes != null)
58                                 types.AddRange (knownTypes);
59                         known_types = new ReadOnlyCollection<Type> (types);
60                 }
61
62                 public ReadOnlyCollection<Type> KnownTypes {
63                         get { return known_types; }
64                 }
65
66                 // used by JsonSerializationReader.cs
67                 internal int MaxItemsInObjectGraph {
68                         get { return Int32.MaxValue; }
69                 }
70
71                 public object ReadObject (Stream stream)
72                 {
73                         var r = (JsonReader) JsonReaderWriterFactory.CreateJsonReader (stream, XmlDictionaryReaderQuotas.Max);
74                         r.LameSilverlightLiteralParser = true;
75
76                         try {
77                                 r.MoveToContent ();
78                                 if (!r.IsStartElement ("root", String.Empty)) {
79                                         throw new SerializationException (
80                                                 String.Format ("Expected element was '{0}', but the actual input element was '{1}' in namespace '{2}'", 
81                                                 "root", r.LocalName, r.NamespaceURI));
82                                 }
83                                 return new JsonSerializationReader (this, r, type, true).ReadRoot ();
84                         } catch (FieldAccessException fae) {
85                                 throw new SecurityException ("Deserialization has failed", fae);
86                         } catch (MethodAccessException mae) {
87                                 throw new SecurityException ("Deserialization has failed", mae);
88                         } catch (SerializationException) {
89                                 throw;
90                         } catch (Exception ex) {
91                                 throw new SerializationException ("Deserialization has failed", ex);
92                         }
93                 }
94
95                 public void WriteObject (Stream stream, object graph)
96                 {
97                         using (var writer = JsonReaderWriterFactory.CreateJsonWriter (stream)) {
98                                 try {
99                                         writer.WriteStartElement ("root");
100                                         new JsonSerializationWriter (this, writer, type, false).WriteObjectContent (graph, true, false);
101                                         writer.WriteEndElement ();
102                                 } catch (FieldAccessException fae) {
103                                         throw new SecurityException ("Serialization has failed", fae);
104                                 } catch (MethodAccessException mae) {
105                                         throw new SecurityException ("Serialization has failed", mae);
106                                 } catch (NotImplementedException) {
107                                         throw;
108                                 } catch (InvalidDataContractException) {
109                                         throw;
110                                 } catch (Exception ex) {
111                                         throw new SerializationException (String.Format ("There was an error during serialization for object of type {0}", graph != null ? graph.GetType () : null), ex);
112                                 }
113                         }
114                 }
115         }
116 }
117