[runtime] Actually clean up context-static data segments.
[mono.git] / mcs / class / System.ServiceModel.Web / System.Runtime.Serialization.Json / DataContractJsonSerializer.cs
index 05e49e7b74df961f095ab414fe2ae2f337983d99..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;
@@ -73,29 +74,33 @@ namespace System.Runtime.Serialization.Json
                {
                }
 
-        DataContractJsonSerializer(Type type, string rootName, IEnumerable<Type> knownTypes, int maxItemsInObjectGraph, bool ignoreExtensionDataObject, bool alwaysEmitTypeInformation)
-        {
-            if (type == null)
+               DataContractJsonSerializer(Type type, string rootName, IEnumerable<Type> knownTypes, int maxItemsInObjectGraph, bool ignoreExtensionDataObject, bool alwaysEmitTypeInformation)
+               {
+                       if (type == null)
                                throw new ArgumentNullException ("type");
                        if (rootName == null)
                                throw new ArgumentNullException ("rootName");
                        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;
                        always_emit_type = alwaysEmitTypeInformation;
                }
 
-#if !NET_2_1 || MONOTOUCH
                public DataContractJsonSerializer (Type type, IEnumerable<Type> knownTypes, int maxItemsInObjectGraph, bool ignoreExtensionDataObject, IDataContractSurrogate dataContractSurrogate, bool alwaysEmitTypeInformation)
             : this (type, default_root_name, knownTypes, maxItemsInObjectGraph, ignoreExtensionDataObject, alwaysEmitTypeInformation)
                {
@@ -111,7 +116,12 @@ namespace System.Runtime.Serialization.Json
                        : this (type, rootName != null ? rootName.Value : default_root_name, knownTypes, maxItemsInObjectGraph, ignoreExtensionDataObject, dataContractSurrogate, alwaysEmitTypeInformation)
                {
                }
-#endif
+
+               public DataContractJsonSerializer (Type type, DataContractJsonSerializerSettings settings)
+                       : this (type, settings.RootName, settings.KnownTypes, settings.MaxItemsInObjectGraph, settings.IgnoreExtensionDataObject,
+                               settings.DataContractSurrogate, false)
+               {
+               }
 
         #endregion
 
@@ -121,21 +131,17 @@ namespace System.Runtime.Serialization.Json
                int max_items;
                bool ignore_extension;
                bool always_emit_type;
-#if !NET_2_1 || MONOTOUCH
                IDataContractSurrogate surrogate;
 
                [MonoTODO]
                public IDataContractSurrogate DataContractSurrogate {
                        get { return surrogate; }
                }
-#endif
 
                [MonoTODO]
                public bool IgnoreExtensionDataObject {
                        get { return ignore_extension; }
                }
-
-               [MonoTODO]
                public ReadOnlyCollection<Type> KnownTypes {
                        get { return known_types; }
                }
@@ -157,43 +163,12 @@ namespace System.Runtime.Serialization.Json
                        return IsStartObject ((XmlReader) reader);
                }
 
-#if NET_2_1
-               Stream QuoteJsonKeys (Stream stream) {
-                       TextReader r = new StreamReader (stream, Encoding.UTF8);
-                       string s = r.ReadToEnd ();
-                       r.Close ();
-
-                       int pos = s.IndexOf (":");
-                       while (pos > 0) {
-                               int i = pos-1;
-                               for (; i >=0 && Char.IsWhiteSpace (s[i]); i--) {}
-                               int end = i + 1;
-                               for (; i > 0 && !Char.IsWhiteSpace (s[i-1]) && s[i-1] != ',' && s[i-1] != '{' && s[i-1] != '}' && s[i-1] != ']'; i--) {}
-                               if (s[i] != '"') {
-                                       s = s.Insert (i, "\"");
-                                       s = s.Insert (end+1, "\"");
-                                       end += 2;
-                                       pos += 2;
-                               }
-                               pos = s.IndexOf (":", pos + 1);
-                       }
-
-                       MemoryStream ms = new MemoryStream ();
-                       StreamWriter tw = new StreamWriter (ms, Encoding.UTF8);
-                       tw.Write (s);
-                       tw.Flush ();
-                       ms.Seek (0, SeekOrigin.Begin);
-                       ms.Position = 0;
-                       return ms;
-               }
-#endif
-
                public override object ReadObject (Stream stream)
                {
 #if NET_2_1
-                       using (Stream json = QuoteJsonKeys (stream)) {
-                               return ReadObject(JsonReaderWriterFactory.CreateJsonReader(json, XmlDictionaryReaderQuotas.Max));
-                       }
+                       var r = (JsonReader) JsonReaderWriterFactory.CreateJsonReader(stream, XmlDictionaryReaderQuotas.Max);
+                       r.LameSilverlightLiteralParser = true;
+                       return ReadObject(r);
 #else
                        return ReadObject (JsonReaderWriterFactory.CreateJsonReader (stream, new XmlDictionaryReaderQuotas ()));
 #endif
@@ -225,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);
                        }
@@ -289,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 (); }
+               }
+
        }
 }