2 // DataContractJsonSerializer.cs (for Moonlight profile)
5 // Atsushi Enomoto <atsushi@ximian.com>
6 // Sebastien Pouliot <sebastien@ximian.com>
8 // Copyright (C) 2007-2008, 2010 Novell, Inc (http://www.novell.com)
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:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
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.
31 using System.Collections.Generic;
32 using System.Collections.ObjectModel;
34 using System.Security;
37 namespace System.Runtime.Serialization.Json {
39 public sealed class DataContractJsonSerializer {
42 private ReadOnlyCollection<Type> known_types;
44 public DataContractJsonSerializer (Type type) :
49 [MonoTODO ("The 'knownTypes' parameter is unused (except as a property)")]
50 public DataContractJsonSerializer (Type type, IEnumerable<Type> knownTypes)
53 throw new ArgumentNullException ("type");
56 List<Type> types = new List<Type> ();
57 if (knownTypes != null)
58 types.AddRange (knownTypes);
59 known_types = new ReadOnlyCollection<Type> (types);
62 public ReadOnlyCollection<Type> KnownTypes {
63 get { return known_types; }
66 // used by JsonSerializationReader.cs
67 internal int MaxItemsInObjectGraph {
68 get { return Int32.MaxValue; }
71 public object ReadObject (Stream stream)
73 var r = (JsonReader) JsonReaderWriterFactory.CreateJsonReader (stream, XmlDictionaryReaderQuotas.Max);
74 r.LameSilverlightLiteralParser = true;
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));
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) {
90 } catch (Exception ex) {
91 throw new SerializationException ("Deserialization has failed", ex);
95 public void WriteObject (Stream stream, object graph)
97 using (var writer = JsonReaderWriterFactory.CreateJsonWriter (stream)) {
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) {
108 } catch (InvalidDataContractException) {
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);