[runtime] Actually clean up context-static data segments.
[mono.git] / mcs / class / System.ServiceModel.Web / System.Runtime.Serialization.Json / DataContractJsonSerializer.cs
index 9656c538a38c179fe864774f1c0b23a8df695053..8077136394dedb755b886df4689ac4d8d3f71912 100644 (file)
@@ -31,6 +31,7 @@ using System.Collections.Generic;
 using System.Collections.ObjectModel;
 using System.Globalization;
 using System.IO;
+using System.Linq;
 using System.Reflection;
 using System.Text;
 using System.Xml;
@@ -64,7 +65,7 @@ namespace System.Runtime.Serialization.Json
                }
 
                public DataContractJsonSerializer (Type type, string rootName, IEnumerable<Type> knownTypes)
-                       : this (type, rootName, knownTypes, int.MaxValue, false, null, true)
+                       : this (type, rootName, knownTypes, int.MaxValue, false, false)
                {
                }
 
@@ -73,12 +74,7 @@ namespace System.Runtime.Serialization.Json
                {
                }
 
-               public DataContractJsonSerializer (Type type, IEnumerable<Type> knownTypes, int maxItemsInObjectGraph, bool ignoreExtensionDataObject, IDataContractSurrogate dataContractSurrogate, bool alwaysEmitTypeInformation)
-                       : this (type, default_root_name, knownTypes, maxItemsInObjectGraph, ignoreExtensionDataObject, dataContractSurrogate, alwaysEmitTypeInformation)
-               {
-               }
-
-               public DataContractJsonSerializer (Type type, string rootName, IEnumerable<Type> knownTypes, int maxItemsInObjectGraph, bool ignoreExtensionDataObject, IDataContractSurrogate dataContractSurrogate, bool alwaysEmitTypeInformation)
+               DataContractJsonSerializer(Type type, string rootName, IEnumerable<Type> knownTypes, int maxItemsInObjectGraph, bool ignoreExtensionDataObject, bool alwaysEmitTypeInformation)
                {
                        if (type == null)
                                throw new ArgumentNullException ("type");
@@ -87,34 +83,55 @@ namespace System.Runtime.Serialization.Json
                        if (maxItemsInObjectGraph < 0)
                                throw new ArgumentOutOfRangeException ("maxItemsInObjectGraph");
 
-                       List<Type> types = new List<Type> ();
-                       types.Add (type);
+                       this.type = type;
+
+                       var knownTypesFromAttributes = new List<Type> ();
+
+                       foreach (var attr in type.GetCustomAttributes (typeof (KnownTypeAttribute), false))
+                               knownTypesFromAttributes.Add ((attr as KnownTypeAttribute).Type);
+
                        if (knownTypes != null)
-                               types.AddRange (knownTypes);
+                               knownTypesFromAttributes.AddRange (knownTypes);
+
+                       known_types = new ReadOnlyCollection<Type> (knownTypesFromAttributes);
 
-                       this.type = type;
-                       known_types = new ReadOnlyCollection<Type> (types);
                        root = rootName;
                        max_items = maxItemsInObjectGraph;
                        ignore_extension = ignoreExtensionDataObject;
-                       surrogate = dataContractSurrogate;
                        always_emit_type = alwaysEmitTypeInformation;
                }
 
+               public DataContractJsonSerializer (Type type, IEnumerable<Type> knownTypes, int maxItemsInObjectGraph, bool ignoreExtensionDataObject, IDataContractSurrogate dataContractSurrogate, bool alwaysEmitTypeInformation)
+            : this (type, default_root_name, knownTypes, maxItemsInObjectGraph, ignoreExtensionDataObject, alwaysEmitTypeInformation)
+               {
+       }
+
+               public DataContractJsonSerializer (Type type, string rootName, IEnumerable<Type> knownTypes, int maxItemsInObjectGraph, bool ignoreExtensionDataObject, IDataContractSurrogate dataContractSurrogate, bool alwaysEmitTypeInformation)
+                       : this (type, rootName, knownTypes, maxItemsInObjectGraph, ignoreExtensionDataObject, alwaysEmitTypeInformation)
+               {
+                       surrogate = dataContractSurrogate;
+               }
+
                public DataContractJsonSerializer (Type type, XmlDictionaryString rootName, IEnumerable<Type> knownTypes, int maxItemsInObjectGraph, bool ignoreExtensionDataObject, IDataContractSurrogate dataContractSurrogate, bool alwaysEmitTypeInformation)
-                       : this (type, rootName != null ? rootName.Value : null, knownTypes, maxItemsInObjectGraph, ignoreExtensionDataObject, dataContractSurrogate, alwaysEmitTypeInformation)
+                       : this (type, rootName != null ? rootName.Value : default_root_name, knownTypes, maxItemsInObjectGraph, ignoreExtensionDataObject, dataContractSurrogate, alwaysEmitTypeInformation)
                {
                }
 
-               #endregion
+               public DataContractJsonSerializer (Type type, DataContractJsonSerializerSettings settings)
+                       : this (type, settings.RootName, settings.KnownTypes, settings.MaxItemsInObjectGraph, settings.IgnoreExtensionDataObject,
+                               settings.DataContractSurrogate, false)
+               {
+               }
+
+        #endregion
 
-               Type type;
+        Type type;
                string root;
                ReadOnlyCollection<Type> known_types;
                int max_items;
                bool ignore_extension;
-               IDataContractSurrogate surrogate;
                bool always_emit_type;
+               IDataContractSurrogate surrogate;
 
                [MonoTODO]
                public IDataContractSurrogate DataContractSurrogate {
@@ -125,8 +142,6 @@ namespace System.Runtime.Serialization.Json
                public bool IgnoreExtensionDataObject {
                        get { return ignore_extension; }
                }
-
-               [MonoTODO]
                public ReadOnlyCollection<Type> KnownTypes {
                        get { return known_types; }
                }
@@ -150,7 +165,13 @@ namespace System.Runtime.Serialization.Json
 
                public override object ReadObject (Stream stream)
                {
+#if NET_2_1
+                       var r = (JsonReader) JsonReaderWriterFactory.CreateJsonReader(stream, XmlDictionaryReaderQuotas.Max);
+                       r.LameSilverlightLiteralParser = true;
+                       return ReadObject(r);
+#else
                        return ReadObject (JsonReaderWriterFactory.CreateJsonReader (stream, new XmlDictionaryReaderQuotas ()));
+#endif
                }
 
                public override object ReadObject (XmlDictionaryReader reader)
@@ -179,6 +200,10 @@ namespace System.Runtime.Serialization.Json
                                return new JsonSerializationReader (this, reader, type, verifyObjectName).ReadRoot ();
                        } catch (SerializationException) {
                                throw;
+                       } catch (InvalidDataContractException) {
+                               throw;
+                       } catch (System.Reflection.TargetInvocationException ex) {
+                               throw ex.InnerException;
                        } catch (Exception ex) {
                                throw new SerializationException ("Deserialization has failed", ex);
                        }
@@ -186,7 +211,8 @@ namespace System.Runtime.Serialization.Json
 
                public override void WriteObject (Stream stream, object graph)
                {
-                       WriteObject (JsonReaderWriterFactory.CreateJsonWriter (stream), graph);
+                       using (var xw = JsonReaderWriterFactory.CreateJsonWriter (stream))
+                               WriteObject (xw, graph);
                }
 
                public override void WriteObject (XmlWriter writer, object graph)
@@ -242,5 +268,26 @@ namespace System.Runtime.Serialization.Json
                                throw new ArgumentNullException ("writer");
                        writer.WriteEndElement ();
                }
+
+               [MonoTODO]
+               public DateTimeFormat DateTimeFormat {
+                       get { throw new NotImplementedException (); }
+               }
+
+               [MonoTODO]
+               public EmitTypeInformation EmitTypeInformation {
+                       get { throw new NotImplementedException (); }
+               }
+
+               [MonoTODO]
+               public bool SerializeReadOnlyTypes {
+                       get { throw new NotImplementedException (); }
+               }
+
+               [MonoTODO]
+               public bool UseSimpleDictionaryFormat {
+                       get { throw new NotImplementedException (); }
+               }
+
        }
 }