2
0

DataContractJsonSerializer_2_1.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. try {
  66. r.MoveToContent ();
  67. if (!r.IsStartElement ("root", String.Empty)) {
  68. throw new SerializationException (
  69. String.Format ("Expected element was '{0}', but the actual input element was '{1}' in namespace '{2}'",
  70. "root", r.LocalName, r.NamespaceURI));
  71. }
  72. return new JsonSerializationReader (this, r, type, true).ReadRoot ();
  73. } catch (FieldAccessException fae) {
  74. throw new SecurityException ("Deserialization has failed", fae);
  75. } catch (MethodAccessException mae) {
  76. throw new SecurityException ("Deserialization has failed", mae);
  77. } catch (SerializationException) {
  78. throw;
  79. } catch (Exception ex) {
  80. throw new SerializationException ("Deserialization has failed", ex);
  81. }
  82. }
  83. public void WriteObject (Stream stream, object graph)
  84. {
  85. using (var writer = JsonReaderWriterFactory.CreateJsonWriter (stream)) {
  86. try {
  87. writer.WriteStartElement ("root");
  88. new JsonSerializationWriter (this, writer, type, false).WriteObjectContent (graph, true, false);
  89. writer.WriteEndElement ();
  90. } catch (FieldAccessException fae) {
  91. throw new SecurityException ("Serialization has failed", fae);
  92. } catch (MethodAccessException mae) {
  93. throw new SecurityException ("Serialization has failed", mae);
  94. } catch (NotImplementedException) {
  95. throw;
  96. } catch (InvalidDataContractException) {
  97. throw;
  98. } catch (Exception ex) {
  99. throw new SerializationException (String.Format ("There was an error during serialization for object of type {0}", graph != null ? graph.GetType () : null), ex);
  100. }
  101. }
  102. }
  103. }
  104. }