Imported Upstream version 3.12.0

Former-commit-id: cf92446697332992ec36726e78eb8703e1f259d7
This commit is contained in:
Jo Shields
2015-01-13 10:44:36 +00:00
parent 8b9b85e7f5
commit 181b81b4a4
659 changed files with 12743 additions and 16300 deletions

View File

@ -84,7 +84,17 @@ namespace System.Runtime.Serialization.Json
throw new ArgumentOutOfRangeException ("maxItemsInObjectGraph");
this.type = type;
known_types = new ReadOnlyCollection<Type> (knownTypes != null ? knownTypes.ToArray () : Type.EmptyTypes);
var knownTypesFromAttributes = new List<Type> ();
foreach (var attr in type.GetCustomAttributes (typeof (KnownTypeAttribute), false))
knownTypesFromAttributes.Add ((attr as KnownTypeAttribute).Type);
if (knownTypes != null)
knownTypesFromAttributes.AddRange (knownTypes);
known_types = new ReadOnlyCollection<Type> (knownTypesFromAttributes);
root = rootName;
max_items = maxItemsInObjectGraph;
ignore_extension = ignoreExtensionDataObject;
@ -134,8 +144,6 @@ namespace System.Runtime.Serialization.Json
public bool IgnoreExtensionDataObject {
get { return ignore_extension; }
}
[MonoTODO]
public ReadOnlyCollection<Type> KnownTypes {
get { return known_types; }
}

View File

@ -182,8 +182,18 @@ namespace System.Runtime.Serialization.Json
if (ct != null) {
return DeserializeGenericCollection (type, ct, instance);
} else {
TypeMap map = GetTypeMap (type);
return map.Deserialize (this, instance);
string typeHint = reader.GetAttribute ("__type");
if (typeHint != null) {
// this might be a derived & known type. We allow it when it's both.
Type exactType = GetRuntimeType (typeHint, type);
if (exactType == null)
throw SerializationError (String.Format ("Cannot load type '{0}'", typeHint));
TypeMap map = GetTypeMap (exactType);
return map.Deserialize (this, instance);
} else { // no type hint
TypeMap map = GetTypeMap (type);
return map.Deserialize (this, instance);
}
}
}
else
@ -198,24 +208,22 @@ namespace System.Runtime.Serialization.Json
}
Type GetRuntimeType (string name)
Type GetRuntimeType (string name, Type baseType)
{
name = ToRuntimeTypeName (name);
string properName = ToRuntimeTypeName (name);
if (baseType != null && baseType.FullName.Equals (properName))
return baseType;
if (serializer.KnownTypes != null)
foreach (Type t in serializer.KnownTypes)
if (t.FullName == name)
if (t.FullName.Equals (properName))
return t;
var ret = root_type.Assembly.GetType (name, false) ?? Type.GetType (name, false);
if (ret != null)
return ret;
// We probably have to iterate all the existing
// assemblies that are loaded in current domain.
foreach (var ass in AppDomain.CurrentDomain.GetAssemblies ()) {
ret = ass.GetType (name, false);
if (ret != null)
return ret;
}
if (baseType != null)
foreach (var attr in baseType.GetCustomAttributes (typeof (KnownTypeAttribute), false))
if ((attr as KnownTypeAttribute).Type.FullName.Equals (properName))
return (attr as KnownTypeAttribute).Type;
return null;
}
@ -230,7 +238,7 @@ namespace System.Runtime.Serialization.Json
case "object":
string runtimeType = reader.GetAttribute ("__type");
if (runtimeType != null) {
Type t = GetRuntimeType (runtimeType);
Type t = GetRuntimeType (runtimeType, null);
if (t == null)
throw SerializationError (String.Format ("Cannot load type '{0}'", runtimeType));
return ReadObject (t);
@ -264,7 +272,7 @@ namespace System.Runtime.Serialization.Json
if (double.TryParse (v, NumberStyles.None, CultureInfo.InvariantCulture, out dbl))
return dbl;
decimal dec;
if (decimal.TryParse (v, NumberStyles.None, CultureInfo.InvariantCulture, out dec))
if (decimal.TryParse (v, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out dec))
return dec;
throw SerializationError (String.Format ("Invalid JSON input: {0}", v));
default:

View File

@ -203,10 +203,9 @@ namespace System.ServiceModel.Syndication
Item.Links.Add (l);
continue;
case "guid":
Item.Id = reader.ReadElementContentAsString ();
if (reader.GetAttribute ("isPermaLink") == "true")
Item.AddPermalink (CreateUri (reader.ReadElementContentAsString ()));
else
Item.Id = reader.ReadElementContentAsString ();
Item.AddPermalink (CreateUri (Item.Id));
continue;
case "pubDate":
Item.PublishDate = FromRFC822DateString (reader.ReadElementContentAsString ());

View File

@ -1191,6 +1191,15 @@ namespace MonoTests.System.Runtime.Serialization.Json
return ser.ReadObject (xr);
}
public T Deserialize<T>(string json)
{
var bytes = Encoding.Unicode.GetBytes (json);
using (MemoryStream stream = new MemoryStream (bytes)) {
var serializer = new DataContractJsonSerializer (typeof(T));
return (T)serializer.ReadObject (stream);
}
}
[Test]
public void IsStartObject ()
{
@ -1819,6 +1828,50 @@ namespace MonoTests.System.Runtime.Serialization.Json
serializer.WriteObject (stream, o);
}
// properly deserialize object with a polymorphic property (known derived type)
[Test]
public void Bug23058()
{
string serializedObj = @"{""PolymorphicProperty"":{""__type"":""KnownDerivedType:#MonoTests.System.Runtime.Serialization.Json"",""BaseTypeProperty"":""Base"",""DerivedProperty"":""Derived 1""},""Name"":""Parent2""}";
ParentType deserializedObj = Deserialize<ParentType> (serializedObj);
Assert.AreEqual (deserializedObj.PolymorphicProperty.GetType ().FullName, "MonoTests.System.Runtime.Serialization.Json.KnownDerivedType");
Assert.AreEqual (deserializedObj.PolymorphicProperty.BaseTypeProperty, "Base");
Assert.AreEqual ((deserializedObj.PolymorphicProperty as KnownDerivedType).DerivedProperty, "Derived 1");
Assert.AreEqual (deserializedObj.Name, "Parent2");
}
// properly deserialize object with a polymorphic property (base type with __type hint)
[Test]
public void DeserializeBaseTypePropHint()
{
string serializedObj = @"{""PolymorphicProperty"":{""__type"":""BaseType:#MonoTests.System.Runtime.Serialization.Json"",""BaseTypeProperty"":""Base""},""Name"":""Parent2""}";
ParentType deserializedObj = Deserialize<ParentType> (serializedObj);
Assert.AreEqual (deserializedObj.PolymorphicProperty.GetType ().FullName, "MonoTests.System.Runtime.Serialization.Json.BaseType");
Assert.AreEqual (deserializedObj.PolymorphicProperty.BaseTypeProperty, "Base");
}
// properly deserialize object with a polymorphic property (base type with __type hint)
[Test]
public void DeserializeBaseTypePropNoHint()
{
string serializedObj = @"{""PolymorphicProperty"":{""BaseTypeProperty"":""Base""},""Name"":""Parent2""}";
ParentType deserializedObj = Deserialize<ParentType> (serializedObj);
Assert.AreEqual (deserializedObj.PolymorphicProperty.GetType ().FullName, "MonoTests.System.Runtime.Serialization.Json.BaseType");
Assert.AreEqual (deserializedObj.PolymorphicProperty.BaseTypeProperty, "Base");
}
// properly fail deserializing object with a polymorphic property (unknown derived type)
[ExpectedException (typeof (SerializationException))]
[Test]
public void FailDeserializingUnknownTypeProp()
{
string serializedObj = @"{""PolymorphicProperty"":{""__type"":""UnknownDerivedType:#MonoTests.System.Runtime.Serialization.Json"",""BaseTypeProperty"":""Base"",""DerivedProperty"":""Derived 1""},""Name"":""Parent2""}";
ParentType deserializedObj = Deserialize<ParentType> (serializedObj);
}
#endregion
}
@ -2037,6 +2090,42 @@ namespace MonoTests.System.Runtime.Serialization.Json
public long CodedServerTimeUTC { get; set; }
public DateTime ServerTimeUTC { get; set; }
}
#region polymorphism test helper classes
[DataContract]
[KnownType (typeof (KnownDerivedType))]
public class ParentType
{
[DataMember]
public string Name { get; set; }
[DataMember]
public BaseType PolymorphicProperty { get; set; }
}
[DataContract]
public class BaseType
{
[DataMember]
public string BaseTypeProperty { get; set; }
}
[DataContract]
public class KnownDerivedType : BaseType
{
[DataMemberAttribute]
public string DerivedProperty { get; set; }
}
[DataContract]
public class UnknownDerivedType : BaseType
{
[DataMember]
public string DerivedProperty { get; set; }
}
#endregion
}
[DataContract]

View File

@ -855,5 +855,16 @@ namespace MonoTests.System.Runtime.Serialization.Json
r.ReadStartElement ();
r.Read ();
}
[Test]
public void ReadNumberAsObject ()
{
const double testValue = 42.42D;
var serializer = new DataContractJsonSerializer (typeof (object));
var serializedStream = GetInput (testValue.ToString (CultureInfo.InvariantCulture));
var deserializedValue = serializer.ReadObject (serializedStream);
Assert.AreEqual (typeof (decimal), deserializedValue.GetType ());
Assert.AreEqual (testValue, (decimal) deserializedValue);
}
}
}

View File

@ -351,6 +351,24 @@ namespace MonoTests.System.ServiceModel.Syndication
{
Assert.IsNull (((IXmlSerializable) new Rss20ItemFormatter ()).GetSchema ());
}
[Test]
public void ReadFromGuidPermaLink ()
{
const string xml1 = "<item><guid isPermaLink=\"false\">urn:myid</guid><description /></item>";
using (XmlReader r = CreateReader (xml1)) {
var rss = new Rss20ItemFormatter ();
rss.ReadFrom (r);
Assert.AreEqual ("urn:myid", rss.Item.Id);
}
const string xml2 = "<item><guid isPermaLink=\"true\">urn:myid</guid><description /></item>";
using (XmlReader r = CreateReader (xml2)) {
var rss = new Rss20ItemFormatter ();
rss.ReadFrom (r);
Assert.AreEqual ("urn:myid", rss.Item.Id);
}
}
}
}
#endif