-More object creation refactoring
This commit is contained in:
@@ -23,6 +23,7 @@
|
||||
// OTHER DEALINGS IN THE SOFTWARE.
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.Reflection;
|
||||
using Newtonsoft.Json.Tests.TestObjects;
|
||||
#if !NETFX_CORE
|
||||
@@ -66,7 +67,7 @@ namespace Newtonsoft.Json.Tests.Serialization
|
||||
[Test]
|
||||
public void FailWithPrivateConstructorPlusParametizedAndDefault()
|
||||
{
|
||||
ExceptionAssert.Throws<TargetInvocationException>(
|
||||
ExceptionAssert.Throws<Exception>(
|
||||
null,
|
||||
() =>
|
||||
{
|
||||
|
||||
@@ -150,6 +150,74 @@ namespace Newtonsoft.Json.Tests.Serialization
|
||||
Assert.IsFalse(contract.Properties.Any(c => c.PropertyName == "InvariantInfo"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ParametrizedCreator()
|
||||
{
|
||||
var resolver = new DefaultContractResolver();
|
||||
var contract = (JsonObjectContract)resolver.ResolveContract(typeof(PublicParametizedConstructorWithPropertyNameConflictWithAttribute));
|
||||
|
||||
Assert.IsNull(contract.DefaultCreator);
|
||||
Assert.IsNotNull(contract.ParametrizedCreator);
|
||||
#pragma warning disable 618
|
||||
Assert.AreEqual(contract.ParametrizedConstructor, typeof(PublicParametizedConstructorWithPropertyNameConflictWithAttribute).GetConstructor(new[] { typeof(string) }));
|
||||
#pragma warning restore 618
|
||||
Assert.AreEqual(1, contract.CreatorParameters.Count);
|
||||
Assert.AreEqual("name", contract.CreatorParameters[0].PropertyName);
|
||||
|
||||
#pragma warning disable 618
|
||||
contract.ParametrizedConstructor = null;
|
||||
#pragma warning restore 618
|
||||
Assert.IsNull(contract.ParametrizedCreator);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void OverrideCreator()
|
||||
{
|
||||
var resolver = new DefaultContractResolver();
|
||||
var contract = (JsonObjectContract)resolver.ResolveContract(typeof(MultipleParamatrizedConstructorsJsonConstructor));
|
||||
|
||||
Assert.IsNull(contract.DefaultCreator);
|
||||
Assert.IsNotNull(contract.OverrideCreator);
|
||||
#pragma warning disable 618
|
||||
Assert.AreEqual(contract.OverrideConstructor, typeof(MultipleParamatrizedConstructorsJsonConstructor).GetConstructor(new[] { typeof(string), typeof(int) }));
|
||||
#pragma warning restore 618
|
||||
Assert.AreEqual(2, contract.CreatorParameters.Count);
|
||||
Assert.AreEqual("Value", contract.CreatorParameters[0].PropertyName);
|
||||
Assert.AreEqual("Age", contract.CreatorParameters[1].PropertyName);
|
||||
|
||||
#pragma warning disable 618
|
||||
contract.OverrideConstructor = null;
|
||||
#pragma warning restore 618
|
||||
Assert.IsNull(contract.OverrideCreator);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void CustomOverrideCreator()
|
||||
{
|
||||
var resolver = new DefaultContractResolver();
|
||||
var contract = (JsonObjectContract)resolver.ResolveContract(typeof(MultipleParamatrizedConstructorsJsonConstructor));
|
||||
|
||||
bool ensureCustomCreatorCalled = false;
|
||||
|
||||
contract.OverrideCreator = args =>
|
||||
{
|
||||
ensureCustomCreatorCalled = true;
|
||||
return new MultipleParamatrizedConstructorsJsonConstructor((string) args[0], (int) args[1]);
|
||||
};
|
||||
#pragma warning disable 618
|
||||
Assert.IsNull(contract.OverrideConstructor);
|
||||
#pragma warning restore 618
|
||||
|
||||
var o = JsonConvert.DeserializeObject<MultipleParamatrizedConstructorsJsonConstructor>("{Value:'value!', Age:1}", new JsonSerializerSettings
|
||||
{
|
||||
ContractResolver = resolver
|
||||
});
|
||||
|
||||
Assert.AreEqual("value!", o.Value);
|
||||
Assert.AreEqual(1, o.Age);
|
||||
Assert.IsTrue(ensureCustomCreatorCalled);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void SerializeInterface()
|
||||
{
|
||||
|
||||
@@ -4952,27 +4952,6 @@ To fix this error either change the environment to be fully trusted, change the
|
||||
Assert.IsTrue(deserializedResponse.Data.DeepEquals(response.Data));
|
||||
}
|
||||
|
||||
public abstract class Test<T>
|
||||
{
|
||||
public abstract T Value { get; set; }
|
||||
}
|
||||
|
||||
[JsonObject(MemberSerialization.OptIn)]
|
||||
public class DecimalTest : Test<decimal>
|
||||
{
|
||||
protected DecimalTest()
|
||||
{
|
||||
}
|
||||
|
||||
public DecimalTest(decimal val)
|
||||
{
|
||||
Value = val;
|
||||
}
|
||||
|
||||
[JsonProperty]
|
||||
public override decimal Value { get; set; }
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeserializeMinValueDecimal()
|
||||
{
|
||||
@@ -4983,24 +4962,6 @@ To fix this error either change the environment to be fully trusted, change the
|
||||
Assert.AreEqual(decimal.MinValue, obj.Value);
|
||||
}
|
||||
|
||||
public class NonPublicConstructorWithJsonConstructor
|
||||
{
|
||||
public string Value { get; private set; }
|
||||
public string Constructor { get; private set; }
|
||||
|
||||
[JsonConstructor]
|
||||
private NonPublicConstructorWithJsonConstructor()
|
||||
{
|
||||
Constructor = "NonPublic";
|
||||
}
|
||||
|
||||
public NonPublicConstructorWithJsonConstructor(string value)
|
||||
{
|
||||
Value = value;
|
||||
Constructor = "Public Paramatized";
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void NonPublicConstructorWithJsonConstructorTest()
|
||||
{
|
||||
@@ -5008,24 +4969,6 @@ To fix this error either change the environment to be fully trusted, change the
|
||||
Assert.AreEqual("NonPublic", c.Constructor);
|
||||
}
|
||||
|
||||
public class PublicConstructorOverridenByJsonConstructor
|
||||
{
|
||||
public string Value { get; private set; }
|
||||
public string Constructor { get; private set; }
|
||||
|
||||
public PublicConstructorOverridenByJsonConstructor()
|
||||
{
|
||||
Constructor = "NonPublic";
|
||||
}
|
||||
|
||||
[JsonConstructor]
|
||||
public PublicConstructorOverridenByJsonConstructor(string value)
|
||||
{
|
||||
Value = value;
|
||||
Constructor = "Public Paramatized";
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void PublicConstructorOverridenByJsonConstructorTest()
|
||||
{
|
||||
@@ -5034,27 +4977,6 @@ To fix this error either change the environment to be fully trusted, change the
|
||||
Assert.AreEqual("value!", c.Value);
|
||||
}
|
||||
|
||||
public class MultipleParamatrizedConstructorsJsonConstructor
|
||||
{
|
||||
public string Value { get; private set; }
|
||||
public int Age { get; private set; }
|
||||
public string Constructor { get; private set; }
|
||||
|
||||
public MultipleParamatrizedConstructorsJsonConstructor(string value)
|
||||
{
|
||||
Value = value;
|
||||
Constructor = "Public Paramatized 1";
|
||||
}
|
||||
|
||||
[JsonConstructor]
|
||||
public MultipleParamatrizedConstructorsJsonConstructor(string value, int age)
|
||||
{
|
||||
Value = value;
|
||||
Age = age;
|
||||
Constructor = "Public Paramatized 2";
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void MultipleParamatrizedConstructorsJsonConstructorTest()
|
||||
{
|
||||
@@ -5064,11 +4986,6 @@ To fix this error either change the environment to be fully trusted, change the
|
||||
Assert.AreEqual(1, c.Age);
|
||||
}
|
||||
|
||||
public class EnumerableClass
|
||||
{
|
||||
public IEnumerable<string> Enumerable { get; set; }
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeserializeEnumerable()
|
||||
{
|
||||
@@ -5094,18 +5011,6 @@ To fix this error either change the environment to be fully trusted, change the
|
||||
Assert.AreEqual("Three", c2.Enumerable.ElementAt(2));
|
||||
}
|
||||
|
||||
[JsonObject(MemberSerialization.OptIn)]
|
||||
public class ItemBase
|
||||
{
|
||||
[JsonProperty]
|
||||
public string Name { get; set; }
|
||||
}
|
||||
|
||||
public class ComplexItem : ItemBase
|
||||
{
|
||||
public Stream Source { get; set; }
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void SerializeAttributesOnBase()
|
||||
{
|
||||
@@ -5118,14 +5023,6 @@ To fix this error either change the environment to be fully trusted, change the
|
||||
}", json);
|
||||
}
|
||||
|
||||
public class DeserializeStringConvert
|
||||
{
|
||||
public string Name { get; set; }
|
||||
public int Age { get; set; }
|
||||
public double Height { get; set; }
|
||||
public decimal Price { get; set; }
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeserializeStringEnglish()
|
||||
{
|
||||
@@ -7961,4 +7858,107 @@ Parameter name: value",
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
public abstract class Test<T>
|
||||
{
|
||||
public abstract T Value { get; set; }
|
||||
}
|
||||
|
||||
[JsonObject(MemberSerialization.OptIn)]
|
||||
public class DecimalTest : Test<decimal>
|
||||
{
|
||||
protected DecimalTest()
|
||||
{
|
||||
}
|
||||
|
||||
public DecimalTest(decimal val)
|
||||
{
|
||||
Value = val;
|
||||
}
|
||||
|
||||
[JsonProperty]
|
||||
public override decimal Value { get; set; }
|
||||
}
|
||||
|
||||
public class NonPublicConstructorWithJsonConstructor
|
||||
{
|
||||
public string Value { get; private set; }
|
||||
public string Constructor { get; private set; }
|
||||
|
||||
[JsonConstructor]
|
||||
private NonPublicConstructorWithJsonConstructor()
|
||||
{
|
||||
Constructor = "NonPublic";
|
||||
}
|
||||
|
||||
public NonPublicConstructorWithJsonConstructor(string value)
|
||||
{
|
||||
Value = value;
|
||||
Constructor = "Public Paramatized";
|
||||
}
|
||||
}
|
||||
|
||||
public class PublicConstructorOverridenByJsonConstructor
|
||||
{
|
||||
public string Value { get; private set; }
|
||||
public string Constructor { get; private set; }
|
||||
|
||||
public PublicConstructorOverridenByJsonConstructor()
|
||||
{
|
||||
Constructor = "NonPublic";
|
||||
}
|
||||
|
||||
[JsonConstructor]
|
||||
public PublicConstructorOverridenByJsonConstructor(string value)
|
||||
{
|
||||
Value = value;
|
||||
Constructor = "Public Paramatized";
|
||||
}
|
||||
}
|
||||
|
||||
public class MultipleParamatrizedConstructorsJsonConstructor
|
||||
{
|
||||
public string Value { get; private set; }
|
||||
public int Age { get; private set; }
|
||||
public string Constructor { get; private set; }
|
||||
|
||||
public MultipleParamatrizedConstructorsJsonConstructor(string value)
|
||||
{
|
||||
Value = value;
|
||||
Constructor = "Public Paramatized 1";
|
||||
}
|
||||
|
||||
[JsonConstructor]
|
||||
public MultipleParamatrizedConstructorsJsonConstructor(string value, int age)
|
||||
{
|
||||
Value = value;
|
||||
Age = age;
|
||||
Constructor = "Public Paramatized 2";
|
||||
}
|
||||
}
|
||||
|
||||
public class EnumerableClass
|
||||
{
|
||||
public IEnumerable<string> Enumerable { get; set; }
|
||||
}
|
||||
|
||||
[JsonObject(MemberSerialization.OptIn)]
|
||||
public class ItemBase
|
||||
{
|
||||
[JsonProperty]
|
||||
public string Name { get; set; }
|
||||
}
|
||||
|
||||
public class ComplexItem : ItemBase
|
||||
{
|
||||
public Stream Source { get; set; }
|
||||
}
|
||||
|
||||
public class DeserializeStringConvert
|
||||
{
|
||||
public string Name { get; set; }
|
||||
public int Age { get; set; }
|
||||
public double Height { get; set; }
|
||||
public decimal Price { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -279,7 +279,7 @@ Newtonsoft.Json Error: 0 : Error!
|
||||
Assert.IsTrue(traceWriter.TraceRecords[2].Message.StartsWith("Finished deserializing System.Collections.Generic.IList`1[System.Int32]. Path 'IntList'"));
|
||||
Assert.AreEqual("Started deserializing System.String[]. Path 'StringArray', line 6, position 19.", traceWriter.TraceRecords[3].Message);
|
||||
Assert.IsTrue(traceWriter.TraceRecords[4].Message.StartsWith("Finished deserializing System.String[]. Path 'StringArray'"));
|
||||
Assert.AreEqual("Deserializing System.Version using a non-default constructor 'Void .ctor(Int32, Int32, Int32, Int32)'. Path 'Version.Major', line 11, position 13.", traceWriter.TraceRecords[5].Message);
|
||||
Assert.AreEqual("Deserializing System.Version using creator with parameters: Major, Minor, Build, Revision. Path 'Version.Major', line 11, position 13.", traceWriter.TraceRecords[5].Message);
|
||||
Assert.IsTrue(traceWriter.TraceRecords[6].Message.StartsWith("Started deserializing System.Version. Path 'Version'"));
|
||||
Assert.IsTrue(traceWriter.TraceRecords[7].Message.StartsWith("Finished deserializing System.Version. Path 'Version'"));
|
||||
Assert.AreEqual("Started deserializing System.Collections.Generic.IDictionary`2[System.String,System.String]. Path 'StringDictionary.1', line 19, position 9.", traceWriter.TraceRecords[8].Message);
|
||||
@@ -533,7 +533,7 @@ Newtonsoft.Json Error: 0 : Error!
|
||||
Assert.AreEqual("Started deserializing System.Collections.Generic.Dictionary`2[System.String,System.String]. Path '$values[0].key!', line 6, position 14.", traceWriter.TraceRecords[3].Message);
|
||||
Assert.IsTrue(traceWriter.TraceRecords[4].Message.StartsWith("Finished deserializing System.Collections.Generic.Dictionary`2[System.String,System.String]. Path '$values[0]'"));
|
||||
Assert.AreEqual("Resolved type 'System.Version, mscorlib' to System.Version. Path '$values[1].$type', line 9, position 42.", traceWriter.TraceRecords[5].Message);
|
||||
Assert.AreEqual("Deserializing System.Version using a custom method 'Void .ctor(Int32, Int32, Int32, Int32)'. Path '$values[1].Major', line 10, position 15.", traceWriter.TraceRecords[6].Message);
|
||||
Assert.AreEqual("Deserializing System.Version using creator with parameters: Major, Minor, Build, Revision. Path '$values[1].Major', line 10, position 15.", traceWriter.TraceRecords[6].Message);
|
||||
Assert.IsTrue(traceWriter.TraceRecords[7].Message.StartsWith("Started deserializing System.Version. Path '$values[1]'"));
|
||||
Assert.IsTrue(traceWriter.TraceRecords[8].Message.StartsWith("Finished deserializing System.Version. Path '$values[1]'"));
|
||||
Assert.IsTrue(traceWriter.TraceRecords[9].Message.StartsWith("Finished deserializing System.Collections.Generic.List`1[System.Object]. Path '$values'"));
|
||||
@@ -610,12 +610,33 @@ Newtonsoft.Json Error: 0 : Error!
|
||||
TraceWriter = traceWriter
|
||||
});
|
||||
|
||||
Assert.AreEqual("Deserializing System.Version using a custom method 'Void .ctor(Int32, Int32, Int32, Int32)'. Path 'Major', line 2, position 11.", traceWriter.TraceRecords[0].Message);
|
||||
Assert.AreEqual("Deserializing System.Version using creator with parameters: Major, Minor, Build, Revision. Path 'Major', line 2, position 11.", traceWriter.TraceRecords[0].Message);
|
||||
Assert.AreEqual("Could not find member 'MissingMemberProperty' on System.Version. Path 'MissingMemberProperty', line 8, position 32.", traceWriter.TraceRecords[1].Message);
|
||||
Assert.IsTrue(traceWriter.TraceRecords[2].Message.StartsWith("Started deserializing System.Version. Path ''"));
|
||||
Assert.IsTrue(traceWriter.TraceRecords[3].Message.StartsWith("Finished deserializing System.Version. Path ''"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void PublicParametizedConstructorWithPropertyNameConflictWithAttribute()
|
||||
{
|
||||
InMemoryTraceWriter traceWriter = new InMemoryTraceWriter
|
||||
{
|
||||
LevelFilter = TraceLevel.Verbose
|
||||
};
|
||||
|
||||
string json = @"{name:""1""}";
|
||||
|
||||
PublicParametizedConstructorWithPropertyNameConflictWithAttribute c = JsonConvert.DeserializeObject<PublicParametizedConstructorWithPropertyNameConflictWithAttribute>(json, new JsonSerializerSettings
|
||||
{
|
||||
TraceWriter = traceWriter
|
||||
});
|
||||
|
||||
Assert.IsNotNull(c);
|
||||
Assert.AreEqual(1, c.Name);
|
||||
|
||||
Assert.AreEqual("Deserializing Newtonsoft.Json.Tests.TestObjects.PublicParametizedConstructorWithPropertyNameConflictWithAttribute using creator with parameters: name. Path 'name', line 1, position 6.", traceWriter.TraceRecords[0].Message);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ShouldSerializeTestClass()
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user