using System.Collections.ObjectModel;
using System.Globalization;
using System.IO;
+using System.Linq;
using System.Reflection;
using System.Text;
using System.Xml;
}
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)
{
}
{
}
- 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");
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 {
public bool IgnoreExtensionDataObject {
get { return ignore_extension; }
}
-
- [MonoTODO]
public ReadOnlyCollection<Type> KnownTypes {
get { return known_types; }
}
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)
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);
}
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)
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 (); }
+ }
+
}
}