-Added JsonConstructorAttribute to explicitly define which constructor to use during deserialization
-Added IFormattable to JValue -Changed JValue ToString to call ToString on the internal value -Fixed code emit error when deserializing interfaces in certain situations
This commit is contained in:
@@ -7,6 +7,7 @@ using System.Text;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using NUnit.Framework;
|
||||
using Newtonsoft.Json.Utilities;
|
||||
using System.Globalization;
|
||||
|
||||
namespace Newtonsoft.Json.Tests.Linq
|
||||
{
|
||||
@@ -106,6 +107,56 @@ namespace Newtonsoft.Json.Tests.Linq
|
||||
Assert.AreEqual("blah blah", (string)v);
|
||||
}
|
||||
|
||||
//[Test]
|
||||
//public void JValueEquals()
|
||||
//{
|
||||
// JObject o = new JObject(
|
||||
// new JProperty("Null", new JValue(null, JTokenType.Null)),
|
||||
// new JProperty("Integer", new JValue(1)),
|
||||
// new JProperty("Float", new JValue(1.1)),
|
||||
// new JProperty("DateTime", new JValue(new DateTime(2000, 12, 29, 23, 51, 10, DateTimeKind.Utc))),
|
||||
// new JProperty("Boolean", new JValue(true)),
|
||||
// new JProperty("String", new JValue("A string lol!")),
|
||||
// new JProperty("Bytes", new JValue(Encoding.UTF8.GetBytes("A string lol!")))
|
||||
// );
|
||||
|
||||
// dynamic d = o;
|
||||
|
||||
// Assert.IsTrue(d.Null == null);
|
||||
// Assert.IsTrue(d.Null == new JValue(null, JTokenType.Null));
|
||||
|
||||
// //if (d.NullValue == null)
|
||||
// // Console.WriteLine("Null");
|
||||
// //else
|
||||
// // Console.WriteLine("Not null");
|
||||
|
||||
// //Console.WriteLine(d.IntValue == 1);
|
||||
//}
|
||||
|
||||
[Test]
|
||||
public void JValueToString()
|
||||
{
|
||||
JObject o = new JObject(
|
||||
new JProperty("Null", new JValue(null, JTokenType.Null)),
|
||||
new JProperty("Integer", new JValue(1)),
|
||||
new JProperty("Float", new JValue(1.1)),
|
||||
new JProperty("DateTime", new JValue(new DateTime(2000, 12, 29, 23, 51, 10, DateTimeKind.Utc))),
|
||||
new JProperty("Boolean", new JValue(true)),
|
||||
new JProperty("String", new JValue("A string lol!")),
|
||||
new JProperty("Bytes", new JValue(Encoding.UTF8.GetBytes("A string lol!")))
|
||||
);
|
||||
|
||||
dynamic d = o;
|
||||
|
||||
Assert.AreEqual("", d.Null.ToString());
|
||||
Assert.AreEqual("1", d.Integer.ToString());
|
||||
Assert.AreEqual("1.1", d.Float.ToString());
|
||||
Assert.AreEqual("12/29/2000 23:51:10", d.DateTime.ToString(null, CultureInfo.InvariantCulture));
|
||||
Assert.AreEqual("True", d.Boolean.ToString());
|
||||
Assert.AreEqual("A string lol!", d.String.ToString());
|
||||
Assert.AreEqual("System.Byte[]", d.Bytes.ToString());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void JObjectGetDynamicPropertyNames()
|
||||
{
|
||||
|
||||
@@ -29,6 +29,7 @@ using System.Linq;
|
||||
using System.Text;
|
||||
using NUnit.Framework;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using System.Globalization;
|
||||
|
||||
namespace Newtonsoft.Json.Tests.Linq
|
||||
{
|
||||
@@ -92,6 +93,30 @@ namespace Newtonsoft.Json.Tests.Linq
|
||||
Assert.AreEqual(JTokenType.String, stringValue.Type);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ToString()
|
||||
{
|
||||
JValue v;
|
||||
|
||||
v = new JValue(true);
|
||||
Assert.AreEqual("True", v.ToString());
|
||||
|
||||
v = new JValue(Encoding.UTF8.GetBytes("Blah"));
|
||||
Assert.AreEqual("System.Byte[]", v.ToString(null, CultureInfo.InvariantCulture));
|
||||
|
||||
v = new JValue("I am a string!");
|
||||
Assert.AreEqual("I am a string!", v.ToString());
|
||||
|
||||
v = new JValue(null, JTokenType.Null);
|
||||
Assert.AreEqual("", v.ToString());
|
||||
|
||||
v = new JValue(null, JTokenType.Null);
|
||||
Assert.AreEqual("", v.ToString(null, CultureInfo.InvariantCulture));
|
||||
|
||||
v = new JValue(new DateTime(2000, 12, 12, 20, 59, 59, DateTimeKind.Utc), JTokenType.Date);
|
||||
Assert.AreEqual("12/12/2000 20:59:59", v.ToString(null, CultureInfo.InvariantCulture));
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(InvalidOperationException), ExpectedMessage = "Cannot access child value on Newtonsoft.Json.Linq.JValue.")]
|
||||
public void Last()
|
||||
|
||||
@@ -287,7 +287,7 @@ keyword such as type of business.""
|
||||
Assert.AreEqual(@"""Width"": 1.1", o.Property("Width").ToString());
|
||||
Assert.AreEqual(@"1.1", o.Property("Width").Value.ToString());
|
||||
Assert.AreEqual(@"""Open"": false", o.Property("Open").ToString());
|
||||
Assert.AreEqual(@"false", o.Property("Open").Value.ToString());
|
||||
Assert.AreEqual(@"False", o.Property("Open").Value.ToString());
|
||||
|
||||
json = @"[null,undefined]";
|
||||
|
||||
@@ -296,8 +296,8 @@ keyword such as type of business.""
|
||||
null,
|
||||
undefined
|
||||
]", a.ToString());
|
||||
Assert.AreEqual(@"null", a.Children().ElementAt(0).ToString());
|
||||
Assert.AreEqual(@"undefined", a.Children().ElementAt(1).ToString());
|
||||
Assert.AreEqual(@"", a.Children().ElementAt(0).ToString());
|
||||
Assert.AreEqual(@"", a.Children().ElementAt(1).ToString());
|
||||
}
|
||||
|
||||
[Test]
|
||||
|
||||
@@ -10,7 +10,7 @@ namespace Newtonsoft.Json.Tests.Serialization
|
||||
public class ConstructorHandlingTests : TestFixtureBase
|
||||
{
|
||||
[Test]
|
||||
[ExpectedException(typeof(JsonSerializationException), ExpectedMessage = "Unable to find a constructor to use for type Newtonsoft.Json.Tests.TestObjects.PrivateConstructorTestClass. A class should either have a default constructor or only one constructor with arguments.")]
|
||||
[ExpectedException(typeof(JsonSerializationException), ExpectedMessage = "Unable to find a constructor to use for type Newtonsoft.Json.Tests.TestObjects.PrivateConstructorTestClass. A class should either have a default constructor, one constructor with arguments or a constructor marked with the JsonConstructor attribute.")]
|
||||
public void FailWithPrivateConstructorAndDefault()
|
||||
{
|
||||
string json = @"{Name:""Name!""}";
|
||||
|
||||
@@ -39,8 +39,58 @@ namespace Newtonsoft.Json.Tests.Serialization
|
||||
public string AuthorCountry { get; set; }
|
||||
}
|
||||
|
||||
public interface IPerson
|
||||
{
|
||||
string FirstName { get; set; }
|
||||
string LastName { get; set; }
|
||||
DateTime BirthDate { get; set; }
|
||||
}
|
||||
|
||||
public class Employee : IPerson
|
||||
{
|
||||
public string FirstName { get; set; }
|
||||
public string LastName { get; set; }
|
||||
public DateTime BirthDate { get; set; }
|
||||
|
||||
public string Department { get; set; }
|
||||
public string JobTitle { get; set; }
|
||||
}
|
||||
|
||||
public class IPersonContractResolver : DefaultContractResolver
|
||||
{
|
||||
protected override JsonContract CreateContract(Type objectType)
|
||||
{
|
||||
if (objectType == typeof(Employee))
|
||||
objectType = typeof(IPerson);
|
||||
|
||||
return base.CreateContract(objectType);
|
||||
}
|
||||
}
|
||||
|
||||
public class ContractResolverTests : TestFixtureBase
|
||||
{
|
||||
[Test]
|
||||
public void SerializeInterface()
|
||||
{
|
||||
Employee employee = new Employee
|
||||
{
|
||||
BirthDate = new DateTime(1977, 12, 30, 1, 1, 1, DateTimeKind.Utc),
|
||||
FirstName = "Maurice",
|
||||
LastName = "Moss",
|
||||
Department = "IT",
|
||||
JobTitle = "Support"
|
||||
};
|
||||
|
||||
string iPersonJson = JsonConvert.SerializeObject(employee, Formatting.Indented,
|
||||
new JsonSerializerSettings { ContractResolver = new IPersonContractResolver() });
|
||||
|
||||
Assert.AreEqual(@"{
|
||||
""FirstName"": ""Maurice"",
|
||||
""LastName"": ""Moss"",
|
||||
""BirthDate"": ""\/Date(252291661000)\/""
|
||||
}", iPersonJson);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void SingleTypeWithMultipleContractResolvers()
|
||||
{
|
||||
@@ -127,4 +177,4 @@ namespace Newtonsoft.Json.Tests.Serialization
|
||||
}", includeCompilerGeneratedJson);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1802,7 +1802,7 @@ keyword such as type of business.""
|
||||
#endif
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(JsonSerializationException), ExpectedMessage = @"Unable to find a constructor to use for type Newtonsoft.Json.Tests.TestObjects.Event. A class should either have a default constructor or only one constructor with arguments.")]
|
||||
[ExpectedException(typeof(JsonSerializationException), ExpectedMessage = @"Unable to find a constructor to use for type Newtonsoft.Json.Tests.TestObjects.Event. A class should either have a default constructor, one constructor with arguments or a constructor marked with the JsonConstructor attribute.")]
|
||||
public void FailWhenClassWithNoDefaultConstructorHasMultipleConstructorsWithArguments()
|
||||
{
|
||||
string json = @"{""sublocation"":""AlertEmailSender.Program.Main"",""userId"":0,""type"":0,""summary"":""Loading settings variables"",""details"":null,""stackTrace"":"" at System.Environment.GetStackTrace(Exception e, Boolean needFileInfo)\r\n at System.Environment.get_StackTrace()\r\n at mr.Logging.Event..ctor(String summary) in C:\\Projects\\MRUtils\\Logging\\Event.vb:line 71\r\n at AlertEmailSender.Program.Main(String[] args) in C:\\Projects\\AlertEmailSender\\AlertEmailSender\\Program.cs:line 25"",""tag"":null,""time"":""\/Date(1249591032026-0400)\/""}";
|
||||
@@ -4043,5 +4043,86 @@ keyword such as type of business.""
|
||||
|
||||
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()
|
||||
{
|
||||
NonPublicConstructorWithJsonConstructor c = JsonConvert.DeserializeObject<NonPublicConstructorWithJsonConstructor>("{}");
|
||||
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()
|
||||
{
|
||||
PublicConstructorOverridenByJsonConstructor c = JsonConvert.DeserializeObject<PublicConstructorOverridenByJsonConstructor>("{Value:'value!'}");
|
||||
Assert.AreEqual("Public Paramatized", c.Constructor);
|
||||
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()
|
||||
{
|
||||
MultipleParamatrizedConstructorsJsonConstructor c = JsonConvert.DeserializeObject<MultipleParamatrizedConstructorsJsonConstructor>("{Value:'value!', Age:1}");
|
||||
Assert.AreEqual("Public Paramatized 2", c.Constructor);
|
||||
Assert.AreEqual("value!", c.Value);
|
||||
Assert.AreEqual(1, c.Age);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user