using System.Collections.ObjectModel;
using System.Globalization;
using System.IO;
+using System.Linq;
using System.Reflection;
using System.Text;
using System.Xml;
{
}
- 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)
{
: 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
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; }
}
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
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);
}
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 (); }
+ }
+
}
}