NetDataContractSerializer.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. //
  2. // NetDataContractSerializer.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. //
  7. // Copyright (C) 2005 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. #if NET_2_0
  29. using System;
  30. using System.Collections;
  31. using System.IO;
  32. using System.Reflection;
  33. using System.Runtime.Serialization.Formatters;
  34. using System.Runtime.Serialization.Formatters.Binary;
  35. using System.Xml;
  36. using System.Xml.Schema;
  37. using QName = System.Xml.XmlQualifiedName;
  38. namespace System.Runtime.Serialization
  39. {
  40. public sealed class NetDataContractSerializer
  41. : XmlObjectSerializer, IFormatter
  42. {
  43. const string xmlns = "http://www.w3.org/2000/xmlns/";
  44. const string default_ns = "http://schemas.datacontract.org/2004/07/";
  45. // This is only for compatible mode.
  46. StreamingContext context;
  47. // KnownTypeCollection known_types;
  48. // IDataContractSurrogate surrogate;
  49. SerializationBinder binder;
  50. ISurrogateSelector selector;
  51. int max_items = 0x10000; // FIXME: could be from config.
  52. bool ignore_extensions;
  53. FormatterAssemblyStyle ass_style;
  54. XmlDictionaryString root_name, root_ns;
  55. public NetDataContractSerializer ()
  56. {
  57. }
  58. public NetDataContractSerializer (StreamingContext context)
  59. {
  60. this.context = context;
  61. }
  62. public NetDataContractSerializer (string rootName,
  63. string rootNamespace)
  64. {
  65. FillDictionaryString (rootName, rootNamespace);
  66. }
  67. public NetDataContractSerializer (XmlDictionaryString rootName,
  68. XmlDictionaryString rootNamespace)
  69. {
  70. if (rootName == null)
  71. throw new ArgumentNullException ("rootName");
  72. if (rootNamespace == null)
  73. throw new ArgumentNullException ("rootNamespace");
  74. root_name = rootName;
  75. root_ns = rootNamespace;
  76. }
  77. public NetDataContractSerializer (StreamingContext context,
  78. int maxItemsInObjectGraph,
  79. bool ignoreExtensibleDataObject,
  80. FormatterAssemblyStyle assemblyFormat,
  81. ISurrogateSelector surrogateSelector)
  82. {
  83. this.context = context;
  84. max_items = maxItemsInObjectGraph;
  85. ignore_extensions = ignoreExtensibleDataObject;
  86. ass_style = assemblyFormat;
  87. selector = surrogateSelector;
  88. }
  89. public NetDataContractSerializer (
  90. string rootName, string rootNamespace,
  91. StreamingContext context,
  92. int maxItemsInObjectGraph,
  93. bool ignoreExtensibleDataObject,
  94. FormatterAssemblyStyle assemblyFormat,
  95. ISurrogateSelector surrogateSelector)
  96. : this (context, maxItemsInObjectGraph,
  97. ignoreExtensibleDataObject, assemblyFormat,
  98. surrogateSelector)
  99. {
  100. FillDictionaryString (rootName, rootNamespace);
  101. }
  102. public NetDataContractSerializer (
  103. XmlDictionaryString rootName,
  104. XmlDictionaryString rootNamespace,
  105. StreamingContext context,
  106. int maxItemsInObjectGraph,
  107. bool ignoreExtensibleDataObject,
  108. FormatterAssemblyStyle assemblyFormat,
  109. ISurrogateSelector surrogateSelector)
  110. : this (context, maxItemsInObjectGraph,
  111. ignoreExtensibleDataObject, assemblyFormat,
  112. surrogateSelector)
  113. {
  114. if (rootName == null)
  115. throw new ArgumentNullException ("rootName");
  116. if (rootNamespace == null)
  117. throw new ArgumentNullException ("rootNamespace");
  118. root_name = rootName;
  119. root_ns = rootNamespace;
  120. }
  121. void FillDictionaryString (string rootName, string rootNamespace)
  122. {
  123. if (rootName == null)
  124. throw new ArgumentNullException ("rootName");
  125. if (rootNamespace == null)
  126. throw new ArgumentNullException ("rootNamespace");
  127. XmlDictionary d = new XmlDictionary ();
  128. root_name = d.Add (rootName);
  129. root_ns = d.Add (rootNamespace);
  130. }
  131. public FormatterAssemblyStyle AssemblyFormat {
  132. get { return ass_style; }
  133. set { ass_style = value; }
  134. }
  135. public SerializationBinder Binder {
  136. get { return binder; }
  137. set { binder = value; }
  138. }
  139. public bool IgnoreExtensionDataObject {
  140. get { return ignore_extensions; }
  141. }
  142. public ISurrogateSelector SurrogateSelector {
  143. get { return selector; }
  144. set { selector = value; }
  145. }
  146. public StreamingContext Context {
  147. get { return context; }
  148. set { context = value; }
  149. }
  150. public int MaxItemsInObjectGraph {
  151. get { return max_items; }
  152. }
  153. public object Deserialize (Stream stream)
  154. {
  155. return ReadObject (stream);
  156. }
  157. [MonoTODO]
  158. public override bool IsStartObject (XmlDictionaryReader reader)
  159. {
  160. throw new NotImplementedException ();
  161. }
  162. public override object ReadObject (XmlDictionaryReader reader, bool readContentOnly)
  163. {
  164. /*
  165. int startTypeCount = known_types.Count;
  166. object ret = XmlFormatterDeserializer.Deserialize (
  167. // FIXME: remove this second param.
  168. reader, null, known_types, surrogate, readContentOnly);
  169. if (!readContentOnly && reader.NodeType == XmlNodeType.EndElement)
  170. reader.Read ();
  171. // remove temporarily-added known types for
  172. // rootType and object graph type.
  173. while (known_types.Count > startTypeCount)
  174. known_types.RemoveAt (startTypeCount);
  175. return ret;
  176. */
  177. throw new NotImplementedException ();
  178. }
  179. public void Serialize (Stream stream, Object graph)
  180. {
  181. using (XmlWriter w = XmlWriter.Create (stream)) {
  182. WriteObject (w, graph);
  183. }
  184. }
  185. [MonoTODO ("support arrays; support Serializable; support SharedType; use DataContractSurrogate")]
  186. /*
  187. when writeContentOnly is true, then the input XmlWriter
  188. must be at element state. This is to write possible
  189. xsi:nil.
  190. rootType determines the top-level element QName (thus
  191. it is ignored when writeContentOnly is true).
  192. preserveObjectReferences indicates that whether the
  193. output should contain ms:Id or not.
  194. (http://schemas.microsoft.com/2003/10/Serialization/)
  195. */
  196. public override void WriteObjectContent (
  197. XmlDictionaryWriter writer, object graph)
  198. {
  199. /*
  200. int startTypeCount = known_types.Count;
  201. string ns = default_ns;
  202. //writer.WriteAttributeString ("xmlns", "i", xmlns, XmlSchema.InstanceNamespace);
  203. //writer.WriteAttributeString ("xmlns", "x", xmlns, XmlSchema.Namespace);
  204. if (ns != null)
  205. writer.WriteAttributeString ("xmlns", xmlns, ns);
  206. XmlFormatterSerializer.Serialize (writer, graph,
  207. known_types,
  208. ignore_extensions, max_items);
  209. // remove temporarily-added known types for
  210. // rootType and object graph type.
  211. while (known_types.Count > startTypeCount)
  212. known_types.RemoveAt (startTypeCount);
  213. */
  214. throw new NotImplementedException ();
  215. }
  216. public override void WriteStartObject (
  217. XmlDictionaryWriter writer, object graph)
  218. {
  219. /*
  220. Type rootType = graph.GetType ();
  221. known_types.Add (rootType);
  222. SerializationMap map =
  223. known_types.FindUserMap (rootType);
  224. QName name = map != null ? map.XmlName :
  225. KnownTypeCollection.GetPredefinedTypeName (rootType);
  226. writer.WriteStartElement (
  227. name.Name, name.Namespace);
  228. */
  229. throw new NotImplementedException ();
  230. }
  231. public override void WriteEndObject (XmlDictionaryWriter writer)
  232. {
  233. writer.WriteEndElement ();
  234. }
  235. }
  236. }
  237. #endif