mirror of
https://github.com/BepInEx/mono.git
synced 2026-11-16 17:10:20 -10:00
Collection support in JSON serializer was actually for any IEnumerable.
This commit is contained in:
1 parent
c58d9d6431
commit
382a385a8f
3 files changed
+89
-57
No files matched your search
+51
-38
@@ -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;
|
||||
@@ -145,7 +146,7 @@ namespace System.Runtime.Serialization.Json
|
||||
return null;
|
||||
}
|
||||
|
||||
Type ct = GetCollectionType (type);
|
||||
Type ct = GetCollectionElementType (type);
|
||||
if (ct != null) {
|
||||
return DeserializeGenericCollection (type, ct);
|
||||
} else {
|
||||
@@ -183,7 +184,11 @@ namespace System.Runtime.Serialization.Json
|
||||
object ReadInstanceDrivenObject ()
|
||||
{
|
||||
string type = reader.GetAttribute ("type");
|
||||
if (type == "object") {
|
||||
switch (type) {
|
||||
case "null":
|
||||
reader.Skip ();
|
||||
return null;
|
||||
case "object":
|
||||
string runtimeType = reader.GetAttribute ("__type");
|
||||
if (runtimeType != null) {
|
||||
Type t = GetRuntimeType (runtimeType);
|
||||
@@ -191,6 +196,7 @@ namespace System.Runtime.Serialization.Json
|
||||
throw SerializationError (String.Format ("Cannot load type '{0}'", runtimeType));
|
||||
return ReadObject (t);
|
||||
}
|
||||
break;
|
||||
}
|
||||
string v = reader.ReadElementContentAsString ();
|
||||
switch (type) {
|
||||
@@ -205,10 +211,6 @@ namespace System.Runtime.Serialization.Json
|
||||
}
|
||||
case "string":
|
||||
return v;
|
||||
case "null":
|
||||
if (v != "null")
|
||||
throw SerializationError (String.Format ("Invalid JSON null value: {0}", v));
|
||||
return null;
|
||||
case "number":
|
||||
int i;
|
||||
if (int.TryParse (v, NumberStyles.None, CultureInfo.InvariantCulture, out i))
|
||||
@@ -242,18 +244,25 @@ namespace System.Runtime.Serialization.Json
|
||||
return idx < 0 ? s : String.Concat (s.Substring (idx + 2), ".", s.Substring (0, idx));
|
||||
}
|
||||
|
||||
Type GetCollectionType (Type type)
|
||||
IEnumerable<Type> GetInterfaces2 (Type type)
|
||||
{
|
||||
if (type.IsInterface)
|
||||
yield return type;
|
||||
foreach (var t in type.GetInterfaces ())
|
||||
yield return t;
|
||||
}
|
||||
|
||||
Type GetCollectionElementType (Type type)
|
||||
{
|
||||
if (type.IsArray)
|
||||
return type.GetElementType ();
|
||||
if (type.IsGenericType) {
|
||||
// returns T for ICollection<T>
|
||||
Type [] ifaces = type.GetInterfaces ();
|
||||
foreach (Type i in ifaces)
|
||||
if (i.IsGenericType && i.GetGenericTypeDefinition ().Equals (typeof (ICollection<>)))
|
||||
// returns T for IEnumerable<T>
|
||||
foreach (Type i in GetInterfaces2 (type))
|
||||
if (i.IsGenericType && i.GetGenericTypeDefinition ().Equals (typeof (IEnumerable<>)))
|
||||
return i.GetGenericArguments () [0];
|
||||
}
|
||||
if (typeof (IList).IsAssignableFrom (type))
|
||||
if (typeof (IEnumerable).IsAssignableFrom (type))
|
||||
// return typeof(object) for mere collection.
|
||||
return typeof (object);
|
||||
else
|
||||
@@ -266,32 +275,8 @@ namespace System.Runtime.Serialization.Json
|
||||
object ret;
|
||||
if (collectionType.IsInterface)
|
||||
collectionType = typeof (List<>).MakeGenericType (elementType);
|
||||
if (typeof (IList).IsAssignableFrom (collectionType)) {
|
||||
#if NET_2_1
|
||||
Type listType = collectionType.IsArray ? typeof (List<>).MakeGenericType (elementType) : null;
|
||||
#else
|
||||
Type listType = collectionType.IsArray ? typeof (ArrayList) : null;
|
||||
#endif
|
||||
IList c = (IList) Activator.CreateInstance (listType ?? collectionType);
|
||||
for (reader.MoveToContent (); reader.NodeType != XmlNodeType.EndElement; reader.MoveToContent ()) {
|
||||
if (!reader.IsStartElement ("item"))
|
||||
throw SerializationError (String.Format ("Expected element 'item', but found '{0}' in namespace '{1}'", reader.LocalName, reader.NamespaceURI));
|
||||
Type et = elementType == typeof (object) || elementType.IsAbstract ? null : elementType;
|
||||
object elem = ReadObject (et ?? typeof (object));
|
||||
c.Add (elem);
|
||||
}
|
||||
#if NET_2_1
|
||||
if (collectionType.IsArray) {
|
||||
Array array = Array.CreateInstance (elementType, c.Count);
|
||||
c.CopyTo (array, 0);
|
||||
ret = array;
|
||||
}
|
||||
else
|
||||
ret = c;
|
||||
#else
|
||||
ret = collectionType.IsArray ? ((ArrayList) c).ToArray (elementType) : c;
|
||||
#endif
|
||||
} else if (TypeMap.IsDictionary (collectionType)) {
|
||||
|
||||
if (TypeMap.IsDictionary (collectionType)) {
|
||||
object dic = Activator.CreateInstance (collectionType);
|
||||
var itemSetter = dic.GetType ().GetProperty ("Item");
|
||||
var keyarr = new object [1];
|
||||
@@ -311,6 +296,34 @@ namespace System.Runtime.Serialization.Json
|
||||
itemSetter.SetValue (dic, val, keyarr);
|
||||
}
|
||||
ret = dic;
|
||||
} else if (typeof (IEnumerable).IsAssignableFrom (collectionType)) {
|
||||
#if NET_2_1
|
||||
Type listType = collectionType.IsArray ? typeof (List<>).MakeGenericType (elementType) : null;
|
||||
#else
|
||||
Type listType = collectionType.IsArray ? typeof (ArrayList) : null;
|
||||
#endif
|
||||
var c = (IEnumerable) TypeMap.CreateInstance (listType ?? collectionType);
|
||||
var addMethod = c.GetType ().GetMethods ().First (m => m.Name == "Add" && m.GetParameters ().Length == 1);
|
||||
var argarr = new object [1];
|
||||
for (reader.MoveToContent (); reader.NodeType != XmlNodeType.EndElement; reader.MoveToContent ()) {
|
||||
if (!reader.IsStartElement ("item"))
|
||||
throw SerializationError (String.Format ("Expected element 'item', but found '{0}' in namespace '{1}'", reader.LocalName, reader.NamespaceURI));
|
||||
Type et = elementType == typeof (object) || elementType.IsAbstract ? null : elementType;
|
||||
object elem = ReadObject (et ?? typeof (object));
|
||||
argarr [0] = elem;
|
||||
addMethod.Invoke (c, argarr);
|
||||
}
|
||||
#if NET_2_1
|
||||
if (collectionType.IsArray) {
|
||||
Array array = Array.CreateInstance (elementType, c.Count);
|
||||
c.CopyTo (array, 0);
|
||||
ret = array;
|
||||
}
|
||||
else
|
||||
ret = c;
|
||||
#else
|
||||
ret = collectionType.IsArray ? ((ArrayList) c).ToArray (elementType) : c;
|
||||
#endif
|
||||
} else {
|
||||
object c = Activator.CreateInstance (collectionType);
|
||||
MethodInfo add = collectionType.GetMethod ("Add", new Type [] {elementType});
|
||||
|
||||
+17
-13
@@ -71,7 +71,11 @@ namespace System.Runtime.Serialization.Json
|
||||
if (serialized_object_count ++ == serializer.MaxItemsInObjectGraph)
|
||||
throw new SerializationException (String.Format ("The object graph exceeded the maximum object count '{0}' specified in the serializer", serializer.MaxItemsInObjectGraph));
|
||||
|
||||
switch (Type.GetTypeCode (graph.GetType ())) {
|
||||
var type = graph.GetType ();
|
||||
if (type.IsGenericType && type.GetGenericTypeDefinition () == typeof (Nullable<>))
|
||||
type = type.GetGenericArguments () [0];
|
||||
|
||||
switch (Type.GetTypeCode (type)) {
|
||||
case TypeCode.Char:
|
||||
case TypeCode.String:
|
||||
writer.WriteString (graph.ToString ());
|
||||
@@ -91,8 +95,8 @@ namespace System.Runtime.Serialization.Json
|
||||
case TypeCode.UInt64:
|
||||
case TypeCode.Decimal:
|
||||
writer.WriteAttributeString ("type", "number");
|
||||
if (graph.GetType ().IsEnum)
|
||||
graph = ((IConvertible) graph).ToType (Enum.GetUnderlyingType (graph.GetType ()), CultureInfo.InvariantCulture);
|
||||
if (type.IsEnum)
|
||||
graph = ((IConvertible) graph).ToType (Enum.GetUnderlyingType (type), CultureInfo.InvariantCulture);
|
||||
writer.WriteString (((IFormattable) graph).ToString ("G", CultureInfo.InvariantCulture));
|
||||
break;
|
||||
case TypeCode.Boolean:
|
||||
@@ -115,44 +119,44 @@ namespace System.Runtime.Serialization.Json
|
||||
writer.WriteString (qn.Name);
|
||||
writer.WriteString (":");
|
||||
writer.WriteString (qn.Namespace);
|
||||
} else if (TypeMap.IsDictionary (graph.GetType ())) {
|
||||
} else if (TypeMap.IsDictionary (type)) {
|
||||
writer.WriteAttributeString ("type", "array");
|
||||
var itemGetter = graph.GetType ().GetProperty ("Item");
|
||||
var keysGetter = graph.GetType ().GetProperty ("Keys");
|
||||
var itemGetter = type.GetProperty ("Item");
|
||||
var keysGetter = type.GetProperty ("Keys");
|
||||
var argarr = new object [1];
|
||||
foreach (object o in (IEnumerable) keysGetter.GetValue (graph, null)) {
|
||||
writer.WriteStartElement ("item");
|
||||
writer.WriteAttributeString ("type", "object");
|
||||
// outputting a KeyValuePair as <Key .. /><Value ... />
|
||||
writer.WriteStartElement ("Key");
|
||||
WriteObjectContent (o, false, !(graph is Array && graph.GetType ().GetElementType () != typeof (object)));
|
||||
WriteObjectContent (o, false, !(graph is Array && type.GetElementType () != typeof (object)));
|
||||
writer.WriteEndElement ();
|
||||
writer.WriteStartElement ("Value");
|
||||
argarr [0] = o;
|
||||
WriteObjectContent (itemGetter.GetValue (graph, argarr), false, !(graph is Array && graph.GetType ().GetElementType () != typeof (object)));
|
||||
WriteObjectContent (itemGetter.GetValue (graph, argarr), false, !(graph is Array && type.GetElementType () != typeof (object)));
|
||||
writer.WriteEndElement ();
|
||||
writer.WriteEndElement ();
|
||||
}
|
||||
} else if (TypeMap.IsCollection (graph.GetType ())) { // array
|
||||
} else if (TypeMap.IsCollection (type)) { // array
|
||||
writer.WriteAttributeString ("type", "array");
|
||||
foreach (object o in (ICollection) graph) {
|
||||
writer.WriteStartElement ("item");
|
||||
// when it is typed, then no need to output "__type"
|
||||
WriteObjectContent (o, false, !(graph is Array && graph.GetType ().GetElementType () != typeof (object)));
|
||||
WriteObjectContent (o, false, !(graph is Array && type.GetElementType () != typeof (object)));
|
||||
writer.WriteEndElement ();
|
||||
}
|
||||
} else { // object
|
||||
TypeMap tm = GetTypeMap (graph.GetType ());
|
||||
TypeMap tm = GetTypeMap (type);
|
||||
if (tm != null) {
|
||||
// FIXME: I'm not sure how it is determined whether __type is written or not...
|
||||
if (outputTypeName || always_emit_type)
|
||||
writer.WriteAttributeString ("__type", FormatTypeName (graph.GetType ()));
|
||||
writer.WriteAttributeString ("__type", FormatTypeName (type));
|
||||
tm.Serialize (this, graph, "object");
|
||||
}
|
||||
else
|
||||
// it does not emit type="object" (as the graph is regarded as a string)
|
||||
// writer.WriteString (graph.ToString ());
|
||||
throw new InvalidDataContractException (String.Format ("Type {0} cannot be serialized by this JSON serializer", graph.GetType ()));
|
||||
throw new InvalidDataContractException (String.Format ("Type {0} cannot be serialized by this JSON serializer", type));
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -106,11 +106,9 @@ namespace System.Runtime.Serialization.Json
|
||||
|
||||
internal static bool IsCollection (Type type)
|
||||
{
|
||||
if (type.GetInterface ("System.Collections.IList", false) != null)
|
||||
return true;
|
||||
if (type.GetInterface ("System.Collections.Generic.IList`1", false) != null)
|
||||
return true;
|
||||
if (type.GetInterface ("System.Collections.Generic.ICollection`1", false) != null)
|
||||
if (IsPrimitiveType (type) || IsDictionary (type))
|
||||
return false;
|
||||
if (type.GetInterface ("System.Collections.IEnumerable", false) != null)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
@@ -194,12 +192,29 @@ namespace System.Runtime.Serialization.Json
|
||||
}
|
||||
}
|
||||
|
||||
internal static object CreateInstance (Type type)
|
||||
{
|
||||
if (TypeMap.IsDictionary (type)) {
|
||||
if (type.IsGenericType)
|
||||
return Activator.CreateInstance (typeof (Dictionary<,>).MakeGenericType (type.GetGenericArguments ()));
|
||||
else
|
||||
return new Hashtable ();
|
||||
} else if (TypeMap.IsCollection (type)) {
|
||||
if (type.IsGenericType)
|
||||
return Activator.CreateInstance (typeof (List<>).MakeGenericType (type.GetGenericArguments ()));
|
||||
else
|
||||
return new ArrayList ();
|
||||
}
|
||||
else
|
||||
return FormatterServices.GetUninitializedObject (type);
|
||||
}
|
||||
|
||||
public virtual object Deserialize (JsonSerializationReader jsr)
|
||||
{
|
||||
XmlReader reader = jsr.Reader;
|
||||
bool isNull = reader.GetAttribute ("type") == "null";
|
||||
|
||||
object ret = isNull ? null : FormatterServices.GetUninitializedObject (type);
|
||||
object ret = isNull ? null : CreateInstance (type);
|
||||
if (ret != null && OnDeserializing != null)
|
||||
OnDeserializing.Invoke (ret, new object [] {new StreamingContext (StreamingContextStates.All)});
|
||||
Dictionary<TypeMapMember,bool> filled = new Dictionary<TypeMapMember,bool> ();
|
||||
|
||||
Reference in new issue
Block a user