-Changed netstandard1.1 to netstandard1.3

-Added support for ISerializable interface to netstandard1.3+
-Added support for SerializableAttribute and NonSerializedAttribute to netstandard1.3+
This commit is contained in:
James Newton-King
2017-03-05 14:29:24 +13:00
parent b9ef3adbc2
commit febdb8188b
31 changed files with 502 additions and 268 deletions
@@ -239,7 +239,6 @@ namespace Newtonsoft.Json.Tests.Linq
Assert.AreEqual(5, (int)a[1].Previous);
Assert.AreEqual(2, a[2].BeforeSelf().Count());
//Assert.AreEqual(2, a[2].AfterSelf().Count());
}
[Test]
@@ -316,6 +315,8 @@ namespace Newtonsoft.Json.Tests.Linq
Assert.AreEqual(true, (bool)(new JValue(1.0)));
Assert.AreEqual(true, (bool)(new JValue("true")));
Assert.AreEqual(true, (bool)(new JValue(true)));
Assert.AreEqual(true, (bool)(new JValue(2)));
Assert.AreEqual(false, (bool)(new JValue(0)));
Assert.AreEqual(1, (int)(new JValue(1)));
Assert.AreEqual(1, (int)(new JValue(1.0)));
Assert.AreEqual(1, (int)(new JValue("1")));
@@ -97,15 +97,16 @@
<DefineConstants>NET20;$(AdditionalConstants)</DefineConstants>
</PropertyGroup>
<ItemGroup Condition="'$(TargetFramework)'=='netcoreapp1.0'">
<PackageReference Include="FSharp.Core" Version="4.0.1.7-alpha" />
<PackageReference Include="System.Runtime.Serialization.Primitives" Version="4.1.1" />
<PackageReference Include="System.Runtime.Serialization.Xml" Version="4.1.1" />
<PackageReference Include="System.Xml.XmlSerializer" Version="4.0.11" />
<PackageReference Include="FSharp.Core" Version="4.1.0" />
<PackageReference Include="System.Runtime.Serialization.Primitives" Version="4.3.0" />
<PackageReference Include="System.Runtime.Serialization.Xml" Version="4.3.0" />
<PackageReference Include="System.Runtime.Serialization.Formatters" Version="4.3.0" />
<PackageReference Include="System.Xml.XmlSerializer" Version="4.3.0" />
<PackageReference Include="System.ValueTuple" Version="4.3.0" />
<PackageReference Include="xunit" Version="2.2.0-*" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.2.0-beta5-build1225" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.0.0-*" />
<PackageReference Include="xunit" Version="2.2.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.2.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.0.0" />
</ItemGroup>
<ItemGroup>
<!-- Tracking for removal https://github.com/Microsoft/vstest/issues/472 -->
@@ -49,8 +49,8 @@ using Extensions = Newtonsoft.Json.Schema.Extensions;
using Newtonsoft.Json.Utilities.LinqBridge;
#else
using System.Linq;
#endif
using Newtonsoft.Json.Tests.Serialization;
namespace Newtonsoft.Json.Tests.Schema
{
@@ -312,14 +312,14 @@ namespace Newtonsoft.Json.Tests.Schema
Assert.IsTrue(v.IsValid(schema));
}
#if !(PORTABLE || DNXCORE50 || PORTABLE40)
#if !(PORTABLE || DNXCORE50 || PORTABLE40) || NETSTANDARD1_1
[Test]
public void GenerateSchemaForISerializable()
{
JsonSchemaGenerator generator = new JsonSchemaGenerator();
generator.UndefinedSchemaIdHandling = UndefinedSchemaIdHandling.UseTypeName;
JsonSchema schema = generator.Generate(typeof(Exception));
JsonSchema schema = generator.Generate(typeof(ISerializableTestObject));
Assert.AreEqual(JsonSchemaType.Object, schema.Type);
Assert.AreEqual(true, schema.AllowAdditionalProperties);
@@ -338,7 +338,9 @@ namespace Newtonsoft.Json.Tests.Schema
Assert.AreEqual(JsonSchemaType.Null, schema.Type);
}
#endif
#if !(PORTABLE || DNXCORE50 || PORTABLE40) || NETSTANDARD1_1
public class CustomDirectoryInfoMapper : DefaultContractResolver
{
public CustomDirectoryInfoMapper()
@@ -477,7 +479,7 @@ namespace Newtonsoft.Json.Tests.Schema
generator.UndefinedSchemaIdHandling = UndefinedSchemaIdHandling.UseTypeName;
generator.ContractResolver = new CamelCasePropertyNamesContractResolver()
{
#if !(PORTABLE || DNXCORE50 || PORTABLE40)
#if !(PORTABLE || DNXCORE50 || PORTABLE40) || NETSTANDARD1_1
IgnoreSerializableAttribute = true
#endif
};
@@ -522,55 +524,50 @@ namespace Newtonsoft.Json.Tests.Schema
}", json);
}
#if !(PORTABLE || DNXCORE50 || PORTABLE40)
#if !(PORTABLE || DNXCORE50 || PORTABLE40) || NETSTANDARD1_1
[Test]
public void GenerateSchemaSerializable()
{
JsonSchemaGenerator generator = new JsonSchemaGenerator();
generator.ContractResolver = new DefaultContractResolver
DefaultContractResolver contractResolver = new DefaultContractResolver
{
IgnoreSerializableAttribute = false
};
generator.ContractResolver = contractResolver;
generator.UndefinedSchemaIdHandling = UndefinedSchemaIdHandling.UseTypeName;
JsonSchema schema = generator.Generate(typeof(Version), true);
JsonSchema schema = generator.Generate(typeof(SerializableTestObject), true);
string json = schema.ToString();
StringAssert.AreEqual(@"{
""id"": ""System.Version"",
""id"": ""Newtonsoft.Json.Tests.Schema.SerializableTestObject"",
""type"": [
""object"",
""null""
],
""additionalProperties"": false,
""properties"": {
""_Major"": {
""_name"": {
""required"": true,
""type"": ""integer""
},
""_Minor"": {
""required"": true,
""type"": ""integer""
},
""_Build"": {
""required"": true,
""type"": ""integer""
},
""_Revision"": {
""required"": true,
""type"": ""integer""
""type"": [
""string"",
""null""
]
}
}
}", json);
JTokenWriter jsonWriter = new JTokenWriter();
JsonSerializer serializer = new JsonSerializer();
serializer.ContractResolver = new DefaultContractResolver
serializer.ContractResolver = contractResolver;
serializer.Serialize(jsonWriter, new SerializableTestObject
{
IgnoreSerializableAttribute = false
};
serializer.Serialize(jsonWriter, new Version(1, 2, 3, 4));
Name = "Name!"
});
List<string> errors = new List<string>();
jsonWriter.Token.Validate(schema, (sender, args) => errors.Add(args.Message));
@@ -578,17 +575,11 @@ namespace Newtonsoft.Json.Tests.Schema
Assert.AreEqual(0, errors.Count);
StringAssert.AreEqual(@"{
""_Major"": 1,
""_Minor"": 2,
""_Build"": 3,
""_Revision"": 4
""_name"": ""Name!""
}", jsonWriter.Token.ToString());
Version version = jsonWriter.Token.ToObject<Version>(serializer);
Assert.AreEqual(1, version.Major);
Assert.AreEqual(2, version.Minor);
Assert.AreEqual(3, version.Build);
Assert.AreEqual(4, version.Revision);
SerializableTestObject c = jsonWriter.Token.ToObject<SerializableTestObject>(serializer);
Assert.AreEqual("Name!", c.Name);
}
#endif
@@ -845,6 +836,20 @@ namespace Newtonsoft.Json.Tests.Schema
public class BulkInsertTask_DSL
{
}
#if !(PORTABLE || DNXCORE50 || PORTABLE40) || NETSTANDARD1_1
[Serializable]
public sealed class SerializableTestObject
{
private string _name;
public string Name
{
get { return _name; }
set { _name = value; }
}
}
#endif
}
#pragma warning restore 618
@@ -109,12 +109,35 @@ namespace Newtonsoft.Json.Tests.Serialization
#if !NET20
[DataMember(Name = "CustomerAddress1")]
#endif
public string AddressLine1 { get; set; }
public string AddressLine1 { get; set; }
}
[TestFixture]
public class ContractResolverTests : TestFixtureBase
{
#if !(PORTABLE) || NETSTANDARD1_1
[Test]
public void ResolveSerializableContract()
{
DefaultContractResolver contractResolver = new DefaultContractResolver();
JsonContract contract = contractResolver.ResolveContract(typeof(ISerializableTestObject));
Assert.AreEqual(JsonContractType.Serializable, contract.ContractType);
}
[Test]
public void ResolveObjectContractWithFieldsSerialization()
{
DefaultContractResolver contractResolver = new DefaultContractResolver
{
IgnoreSerializableAttribute = false
};
JsonObjectContract contract = (JsonObjectContract)contractResolver.ResolveContract(typeof(AnswerFilterModel));
Assert.AreEqual(MemberSerialization.Fields, contract.MemberSerialization);
}
#endif
[Test]
public void JsonPropertyDefaultValue()
{
@@ -4158,7 +4158,7 @@ Path '', line 1, position 1.");
{
ContractResolver = new DefaultContractResolver
{
#if !(PORTABLE || DNXCORE50 || PORTABLE40)
#if !(PORTABLE || DNXCORE50 || PORTABLE40) || NETSTANDARD1_1
IgnoreSerializableAttribute = true
#endif
}
@@ -4175,7 +4175,7 @@ Path '', line 1, position 1.");
{
ContractResolver = new DefaultContractResolver
{
#if !(PORTABLE || DNXCORE50 || PORTABLE40)
#if !(PORTABLE || DNXCORE50 || PORTABLE40) || NETSTANDARD1_1
IgnoreSerializableAttribute = true
#endif
}
@@ -4192,7 +4192,7 @@ Path '', line 1, position 1.");
{
ContractResolver = new DefaultContractResolver
{
#if !(PORTABLE || DNXCORE50 || PORTABLE40)
#if !(PORTABLE || DNXCORE50 || PORTABLE40) || NETSTANDARD1_1
IgnoreSerializableAttribute = true
#endif
}
@@ -4984,84 +4984,7 @@ Path '', line 1, position 1.");
Assert.AreEqual("value", newModelStateDictionary["key"]);
}
#if !(PORTABLE || DNXCORE50 || PORTABLE40)
public class ISerializableTestObject : ISerializable
{
internal string _stringValue;
internal int _intValue;
internal DateTimeOffset _dateTimeOffsetValue;
internal Person _personValue;
internal Person _nullPersonValue;
internal int? _nullableInt;
internal bool _booleanValue;
internal byte _byteValue;
internal char _charValue;
internal DateTime _dateTimeValue;
internal decimal _decimalValue;
internal short _shortValue;
internal long _longValue;
internal sbyte _sbyteValue;
internal float _floatValue;
internal ushort _ushortValue;
internal uint _uintValue;
internal ulong _ulongValue;
public ISerializableTestObject(string stringValue, int intValue, DateTimeOffset dateTimeOffset, Person personValue)
{
_stringValue = stringValue;
_intValue = intValue;
_dateTimeOffsetValue = dateTimeOffset;
_personValue = personValue;
_dateTimeValue = new DateTime(0, DateTimeKind.Utc);
}
protected ISerializableTestObject(SerializationInfo info, StreamingContext context)
{
_stringValue = info.GetString("stringValue");
_intValue = info.GetInt32("intValue");
_dateTimeOffsetValue = (DateTimeOffset)info.GetValue("dateTimeOffsetValue", typeof(DateTimeOffset));
_personValue = (Person)info.GetValue("personValue", typeof(Person));
_nullPersonValue = (Person)info.GetValue("nullPersonValue", typeof(Person));
_nullableInt = (int?)info.GetValue("nullableInt", typeof(int?));
_booleanValue = info.GetBoolean("booleanValue");
_byteValue = info.GetByte("byteValue");
_charValue = info.GetChar("charValue");
_dateTimeValue = info.GetDateTime("dateTimeValue");
_decimalValue = info.GetDecimal("decimalValue");
_shortValue = info.GetInt16("shortValue");
_longValue = info.GetInt64("longValue");
_sbyteValue = info.GetSByte("sbyteValue");
_floatValue = info.GetSingle("floatValue");
_ushortValue = info.GetUInt16("ushortValue");
_uintValue = info.GetUInt32("uintValue");
_ulongValue = info.GetUInt64("ulongValue");
}
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue("stringValue", _stringValue);
info.AddValue("intValue", _intValue);
info.AddValue("dateTimeOffsetValue", _dateTimeOffsetValue);
info.AddValue("personValue", _personValue);
info.AddValue("nullPersonValue", _nullPersonValue);
info.AddValue("nullableInt", null);
info.AddValue("booleanValue", _booleanValue);
info.AddValue("byteValue", _byteValue);
info.AddValue("charValue", _charValue);
info.AddValue("dateTimeValue", _dateTimeValue);
info.AddValue("decimalValue", _decimalValue);
info.AddValue("shortValue", _shortValue);
info.AddValue("longValue", _longValue);
info.AddValue("sbyteValue", _sbyteValue);
info.AddValue("floatValue", _floatValue);
info.AddValue("ushortValue", _ushortValue);
info.AddValue("uintValue", _uintValue);
info.AddValue("ulongValue", _ulongValue);
}
}
#if !(PORTABLE || DNXCORE50 || PORTABLE40) || NETSTANDARD1_1
#if DEBUG
[Test]
public void SerializeISerializableInPartialTrustWithIgnoreInterface()
@@ -5094,7 +5017,7 @@ Path '', line 1, position 1.");
}
finally
{
JsonTypeReflector.SetFullyTrusted(true);
JsonTypeReflector.SetFullyTrusted(null);
}
}
@@ -5114,7 +5037,7 @@ Path '', line 1, position 1.");
}
finally
{
JsonTypeReflector.SetFullyTrusted(true);
JsonTypeReflector.SetFullyTrusted(null);
}
}
@@ -5135,7 +5058,7 @@ Path '', line 1, position 1.");
}
finally
{
JsonTypeReflector.SetFullyTrusted(true);
JsonTypeReflector.SetFullyTrusted(null);
}
}
#endif
@@ -8091,24 +8014,19 @@ Path '', line 1, position 1.");
Assert.AreEqual("", s);
}
#if !(PORTABLE || DNXCORE50 || PORTABLE40)
#if !(PORTABLE || DNXCORE50 || PORTABLE40) || NETSTANDARD1_1
[Test]
public void SerializeAndDeserializeWithAttributes()
{
var testObj = new PersonSerializable() { Name = "John Doe", Age = 28 };
var objDeserialized = SerializeAndDeserialize<PersonSerializable>(testObj);
var json = Serialize(testObj);
var objDeserialized = Deserialize<PersonSerializable>(json);
Assert.AreEqual(testObj.Name, objDeserialized.Name);
Assert.AreEqual(0, objDeserialized.Age);
}
private T SerializeAndDeserialize<T>(T obj)
where T : class
{
var json = Serialize(obj);
return Deserialize<T>(json);
}
private string Serialize<T>(T obj)
where T : class
{
@@ -8622,15 +8540,18 @@ This is just junk, though.";
public void SerializeTupleWithSerializableAttribute()
{
var tuple = Tuple.Create(500);
SerializableContractResolver contractResolver = new SerializableContractResolver();
var json = JsonConvert.SerializeObject(tuple, new JsonSerializerSettings
{
ContractResolver = new SerializableContractResolver()
ContractResolver = contractResolver
});
Assert.AreEqual(@"{""m_Item1"":500}", json);
var obj = JsonConvert.DeserializeObject<Tuple<int>>(json, new JsonSerializerSettings
{
ContractResolver = new SerializableContractResolver()
ContractResolver = contractResolver
});
Assert.AreEqual(500, obj.Item1);
}
@@ -8891,7 +8812,7 @@ This is just junk, though.";
Assert.AreEqual(@"{""First"":""One"",""Second"":2}", json);
#if !(PORTABLE || DNXCORE50 || PORTABLE40)
#if !(PORTABLE || DNXCORE50 || PORTABLE40) || NETSTANDARD1_1
DefaultContractResolver r = new DefaultContractResolver();
r.IgnoreSerializableAttribute = false;
@@ -9007,7 +8928,7 @@ This is just junk, though.";
Assert.AreEqual(1234567890.123456m, d);
}
#if !(PORTABLE || DNXCORE50 || PORTABLE40)
#if !(PORTABLE || DNXCORE50 || PORTABLE40) || NETSTANDARD1_1
[Test]
public void DontSerializeStaticFields()
{
@@ -10334,4 +10255,83 @@ This is just junk, though.";
public string DataSource { get; }
public string Project { get; }
}
#if !(PORTABLE || DNXCORE50 || PORTABLE40) || NETSTANDARD1_1
public class ISerializableTestObject : ISerializable
{
internal string _stringValue;
internal int _intValue;
internal DateTimeOffset _dateTimeOffsetValue;
internal Person _personValue;
internal Person _nullPersonValue;
internal int? _nullableInt;
internal bool _booleanValue;
internal byte _byteValue;
internal char _charValue;
internal DateTime _dateTimeValue;
internal decimal _decimalValue;
internal short _shortValue;
internal long _longValue;
internal sbyte _sbyteValue;
internal float _floatValue;
internal ushort _ushortValue;
internal uint _uintValue;
internal ulong _ulongValue;
public ISerializableTestObject(string stringValue, int intValue, DateTimeOffset dateTimeOffset, Person personValue)
{
_stringValue = stringValue;
_intValue = intValue;
_dateTimeOffsetValue = dateTimeOffset;
_personValue = personValue;
_dateTimeValue = new DateTime(0, DateTimeKind.Utc);
}
protected ISerializableTestObject(SerializationInfo info, StreamingContext context)
{
_stringValue = info.GetString("stringValue");
_intValue = info.GetInt32("intValue");
_dateTimeOffsetValue = (DateTimeOffset)info.GetValue("dateTimeOffsetValue", typeof(DateTimeOffset));
_personValue = (Person)info.GetValue("personValue", typeof(Person));
_nullPersonValue = (Person)info.GetValue("nullPersonValue", typeof(Person));
_nullableInt = (int?)info.GetValue("nullableInt", typeof(int?));
_booleanValue = info.GetBoolean("booleanValue");
_byteValue = info.GetByte("byteValue");
_charValue = info.GetChar("charValue");
_dateTimeValue = info.GetDateTime("dateTimeValue");
_decimalValue = info.GetDecimal("decimalValue");
_shortValue = info.GetInt16("shortValue");
_longValue = info.GetInt64("longValue");
_sbyteValue = info.GetSByte("sbyteValue");
_floatValue = info.GetSingle("floatValue");
_ushortValue = info.GetUInt16("ushortValue");
_uintValue = info.GetUInt32("uintValue");
_ulongValue = info.GetUInt64("ulongValue");
}
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue("stringValue", _stringValue);
info.AddValue("intValue", _intValue);
info.AddValue("dateTimeOffsetValue", _dateTimeOffsetValue);
info.AddValue("personValue", _personValue);
info.AddValue("nullPersonValue", _nullPersonValue);
info.AddValue("nullableInt", null);
info.AddValue("booleanValue", _booleanValue);
info.AddValue("byteValue", _byteValue);
info.AddValue("charValue", _charValue);
info.AddValue("dateTimeValue", _dateTimeValue);
info.AddValue("decimalValue", _decimalValue);
info.AddValue("shortValue", _shortValue);
info.AddValue("longValue", _longValue);
info.AddValue("sbyteValue", _sbyteValue);
info.AddValue("floatValue", _floatValue);
info.AddValue("ushortValue", _ushortValue);
info.AddValue("uintValue", _uintValue);
info.AddValue("ulongValue", _ulongValue);
}
}
#endif
}
@@ -75,7 +75,7 @@ namespace Newtonsoft.Json.Tests.Serialization
{
ContractResolver = new DefaultContractResolver
{
#if !(PORTABLE || DNXCORE50 || PORTABLE40)
#if !(PORTABLE || DNXCORE50 || PORTABLE40) || NETSTANDARD1_1
IgnoreSerializableAttribute = false
#endif
}
@@ -84,7 +84,7 @@ namespace Newtonsoft.Json.Tests.Serialization
Assert.AreEqual(expected, json);
}
#if !(PORTABLE || DNXCORE50 || PORTABLE40)
#if !(PORTABLE || DNXCORE50 || PORTABLE40) || NETSTANDARD1_1
[Test]
public void SerializeInheritedType()
{
@@ -114,7 +114,7 @@ namespace Newtonsoft.Json.Tests.Serialization
}
}
#if !(PORTABLE || DNXCORE50 || PORTABLE40)
#if !(PORTABLE || DNXCORE50 || PORTABLE40) || NETSTANDARD1_1
[Serializable]
#else
[JsonObject(MemberSerialization.Fields)]
@@ -143,7 +143,7 @@ namespace Newtonsoft.Json.Tests.Serialization
#else
[JsonIgnore]
#endif
public string nonSerializedField;
public string nonSerializedField;
public bool Equals(SerializableType other)
{
@@ -34,7 +34,7 @@ using System.Linq;
namespace Newtonsoft.Json.Tests.TestObjects
{
#if !(PORTABLE || DNXCORE50)
#if !(PORTABLE || DNXCORE50) || NETSTANDARD1_1
[Serializable]
public class AnswerFilterModel
{
@@ -27,7 +27,7 @@ using System;
namespace Newtonsoft.Json.Tests.TestObjects
{
#if !(PORTABLE || DNXCORE50)
#if !(PORTABLE || DNXCORE50) || NETSTANDARD1_1
[Serializable]
public class PersonSerializable
{