DataContractJsonSerializer_2_1.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. //
  2. // DataContractJsonSerializer.cs (for Moonlight profile)
  3. //
  4. // Authors:
  5. // Atsushi Enomoto <[email protected]>
  6. // Sebastien Pouliot <[email protected]>
  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. using System;
  30. using System.Collections.Generic;
  31. using System.Collections.ObjectModel;
  32. using System.IO;
  33. using System.Security;
  34. using System.Xml;
  35. namespace System.Runtime.Serialization.Json {
  36. public sealed class DataContractJsonSerializer {
  37. private Type type;
  38. private ReadOnlyCollection<Type> known_types;
  39. public DataContractJsonSerializer (Type type) :
  40. this (type, null)
  41. {
  42. }
  43. [MonoTODO ("The 'knownTypes' parameter is unused (except as a property)")]
  44. public DataContractJsonSerializer (Type type, IEnumerable<Type> knownTypes)
  45. {
  46. if (type == null)
  47. throw new ArgumentNullException ("type");
  48. this.type = type;
  49. List<Type> types = new List<Type> ();
  50. if (knownTypes != null)
  51. types.AddRange (knownTypes);
  52. known_types = new ReadOnlyCollection<Type> (types);
  53. }
  54. public ReadOnlyCollection<Type> KnownTypes {
  55. get { return known_types; }
  56. }
  57. // used by JsonSerializationReader.cs
  58. internal int MaxItemsInObjectGraph {
  59. get { return Int32.MaxValue; }
  60. }
  61. public object ReadObject (Stream stream)
  62. {
  63. var r = (JsonReader) JsonReaderWriterFactory.CreateJsonReader (stream, XmlDictionaryReaderQuotas.Max);
  64. r.LameSilverlightLiteralParser = true;
  65. return ReadObject (r);
  66. }
  67. // FIXME: should be internal
  68. public object ReadObject (XmlReader r)
  69. {
  70. return ReadObject (XmlDictionaryReader.CreateDictionaryReader (r));
  71. }
  72. // FIXME: should be internal
  73. public object ReadObject (XmlDictionaryReader r)
  74. {
  75. try {
  76. r.MoveToContent ();
  77. if (!r.IsStartElement ("root", String.Empty)) {
  78. throw new SerializationException (
  79. String.Format ("Expected element was '{0}', but the actual input element was '{1}' in namespace '{2}'",
  80. "root", r.LocalName, r.NamespaceURI));
  81. }
  82. return new JsonSerializationReader (this, r, type, true).ReadRoot ();
  83. } catch (FieldAccessException fae) {
  84. throw new SecurityException ("Deserialization has failed", fae);
  85. } catch (MethodAccessException mae) {
  86. throw new SecurityException ("Deserialization has failed", mae);
  87. } catch (SerializationException) {
  88. throw;
  89. } catch (Exception ex) {
  90. throw new SerializationException ("Deserialization has failed", ex);
  91. }
  92. }
  93. public void WriteObject (Stream stream, object graph)
  94. {
  95. using (var writer = JsonReaderWriterFactory.CreateJsonWriter (stream)) {
  96. WriteObject (writer, graph);
  97. }
  98. }
  99. // FIXME: should be internal
  100. public void WriteObject (XmlWriter writer, object graph)
  101. {
  102. WriteObject (XmlDictionaryWriter.CreateDictionaryWriter (writer), graph);
  103. }
  104. // FIXME: should be internal
  105. public void WriteObject (XmlDictionaryWriter writer, object graph)
  106. {
  107. try {
  108. writer.WriteStartElement ("root");
  109. new JsonSerializationWriter (this, writer, type, false).WriteObjectContent (graph, true, false);
  110. writer.WriteEndElement ();
  111. } catch (FieldAccessException fae) {
  112. throw new SecurityException ("Serialization has failed", fae);
  113. } catch (MethodAccessException mae) {
  114. throw new SecurityException ("Serialization has failed", mae);
  115. } catch (NotImplementedException) {
  116. throw;
  117. } catch (InvalidDataContractException) {
  118. throw;
  119. } catch (Exception ex) {
  120. throw new SerializationException (String.Format ("There was an error during serialization for object of type {0}", graph != null ? graph.GetType () : null), ex);
  121. }
  122. }
  123. }
  124. }