-Added JsonPrimitiveContract and JsonLinqContract

-Removed JsonRaw and replaced with JRaw
-Fixed properties on existing derived values not being populated during deserialization
This commit is contained in:
JamesNK
2009-12-27 03:30:43 +00:00
parent 9144bc6c80
commit 961945657a
35 changed files with 835 additions and 436 deletions
-41
View File
@@ -1,41 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
namespace Newtonsoft.Json.Tests
{
public class JsonRawTests : TestFixtureBase
{
[Test]
public void Create()
{
JsonRaw raw = new JsonRaw("Test");
Assert.AreEqual("Test", raw.Content);
Assert.AreEqual("Test", raw.ToString());
}
[Test]
public void EqualsShouldEqualOnSameContent()
{
JsonRaw r1 = new JsonRaw("Test");
JsonRaw r2 = new JsonRaw("Test");
Assert.AreEqual(r1, r2);
Assert.AreNotEqual(r1, null);
Assert.IsFalse(r1.Equals(null));
}
[Test]
public void GetHashCodeShouldEqualOnSameContent()
{
JsonRaw r1 = new JsonRaw("Test");
JsonRaw r2 = new JsonRaw("Test");
JsonRaw r3 = new JsonRaw("Test1");
Assert.AreEqual(r1.GetHashCode(), r2.GetHashCode());
Assert.AreNotEqual(r1.GetHashCode(), r3.GetHashCode());
}
}
}
@@ -243,7 +243,7 @@ Parameter name: arrayIndex")]
PersonRaw raw = new PersonRaw
{
FirstName = "FirstNameValue",
RawContent = new JsonRaw("[1,2,3,4,5]"),
RawContent = new JRaw("[1,2,3,4,5]"),
LastName = "LastNameValue"
};
@@ -261,7 +261,7 @@ Parameter name: arrayIndex")]
PersonRaw raw = new PersonRaw
{
FirstName = "FirstNameValue",
RawContent = new JsonRaw("[1,2,3,4,5]"),
RawContent = new JRaw("[1,2,3,4,5]"),
LastName = "LastNameValue"
};
@@ -302,7 +302,7 @@ Parameter name: arrayIndex")]
PersonRaw raw = new PersonRaw
{
FirstName = "FirstNameValue",
RawContent = new JsonRaw("[1,2,3,4,5]"),
RawContent = new JRaw("[1,2,3,4,5]"),
LastName = "LastNameValue"
};
@@ -314,7 +314,7 @@ Parameter name: arrayIndex")]
Assert.AreEqual("FirstNameValue", raw.FirstName);
Assert.AreEqual("LastNameValue", raw.LastName);
Assert.AreEqual("[1,2,3,4,5]", raw.RawContent.Content);
Assert.AreEqual("[1,2,3,4,5]", raw.RawContent.Value);
}
[Test]
@@ -366,8 +366,8 @@ Parameter name: arrayIndex")]
public void RawChildValues()
{
JObject o = new JObject();
o["val1"] = JValue.CreateRaw("1");
o["val2"] = JValue.CreateRaw("1");
o["val1"] = new JRaw("1");
o["val2"] = new JRaw("1");
string json = o.ToString();
@@ -0,0 +1,32 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using Newtonsoft.Json.Linq;
namespace Newtonsoft.Json.Tests.Linq
{
public class JRawTests : TestFixtureBase
{
[Test]
public void RawEquals()
{
JRaw r1 = new JRaw("raw1");
JRaw r2 = new JRaw("raw1");
JRaw r3 = new JRaw("raw2");
Assert.IsTrue(JToken.DeepEquals(r1, r2));
Assert.IsFalse(JToken.DeepEquals(r1, r3));
}
[Test]
public void RawClone()
{
JRaw r1 = new JRaw("raw1");
JToken r2 = r1.CloneToken();
Assert.IsInstanceOfType(typeof(JRaw), r2);
}
}
}
@@ -68,6 +68,7 @@
<Compile Include="Linq\ComponentModel\JTypeDescriptionProviderTests.cs" />
<Compile Include="Linq\ComponentModel\JTypeDescriptorTests.cs" />
<Compile Include="Linq\JPathTests.cs" />
<Compile Include="Linq\JRawTests.cs" />
<Compile Include="Linq\JTokenReaderTest.cs" />
<Compile Include="Linq\JTokenWriterTest.cs" />
<Compile Include="JsonValidatingReaderTests.cs" />
@@ -87,10 +88,14 @@
<Compile Include="TestObjects\AbstractGenericBase.cs" />
<Compile Include="TestObjects\ArgumentConverterPrecedenceClassConverter.cs" />
<Compile Include="TestObjects\BadJsonPropertyClass.cs" />
<Compile Include="TestObjects\Bar.cs" />
<Compile Include="TestObjects\Car.cs" />
<Compile Include="TestObjects\CircularReferenceClass.cs" />
<Compile Include="TestObjects\ClassAndMemberConverterClass.cs" />
<Compile Include="TestObjects\ClassConverterPrecedenceClassConverter.cs" />
<Compile Include="TestObjects\Computer.cs" />
<Compile Include="TestObjects\ConstructorCaseSensitivityClass.cs" />
<Compile Include="TestObjects\ConstructorReadonlyFields.cs" />
<Compile Include="TestObjects\Container.cs" />
<Compile Include="TestObjects\Content.cs" />
<Compile Include="TestObjects\ConverterPrecedenceClass.cs" />
@@ -102,6 +107,7 @@
<Compile Include="TestObjects\DoubleClass.cs" />
<Compile Include="TestObjects\EmployeeReference.cs" />
<Compile Include="TestObjects\Event.cs" />
<Compile Include="TestObjects\Foo.cs" />
<Compile Include="TestObjects\GenericImpl.cs" />
<Compile Include="TestObjects\GenericListAndDictionaryInterfaceProperties.cs" />
<Compile Include="TestObjects\GetOnlyPropertyClass.cs" />
@@ -115,16 +121,19 @@
<Compile Include="TestObjects\JsonPropertyWithHandlingValues.cs" />
<Compile Include="TestObjects\ListErrorObject.cs" />
<Compile Include="TestObjects\ListErrorObjectCollection.cs" />
<Compile Include="TestObjects\ListOfIds.cs" />
<Compile Include="TestObjects\ListTestClass.cs" />
<Compile Include="TestObjects\LogEntry.cs" />
<Compile Include="TestObjects\MemberConverterPrecedenceClassConverter.cs" />
<Compile Include="TestObjects\MethodExecutorObject.cs" />
<Compile Include="TestObjects\Movie.cs" />
<Compile Include="TestObjects\MyClass.cs" />
<Compile Include="TestObjects\Name.cs" />
<Compile Include="TestObjects\NonRequest.cs" />
<Compile Include="TestObjects\NullableDateTimeTestClass.cs" />
<Compile Include="TestObjects\ObjectArrayPropertyTest.cs" />
<Compile Include="TestObjects\PersonError.cs" />
<Compile Include="TestObjects\PersonPropertyClass.cs" />
<Compile Include="TestObjects\PersonRaw.cs" />
<Compile Include="TestObjects\PhoneNumber.cs" />
<Compile Include="TestObjects\PrivateConstructorTestClass.cs" />
@@ -134,6 +143,7 @@
<Compile Include="TestObjects\RequestOnly.cs" />
<Compile Include="TestObjects\RequiredMembersClass.cs" />
<Compile Include="TestObjects\RoleTransfer.cs" />
<Compile Include="TestObjects\SearchResult.cs" />
<Compile Include="TestObjects\SerializationEventTestDictionary.cs" />
<Compile Include="TestObjects\SerializationEventTestList.cs" />
<Compile Include="TestObjects\SerializationEventTestObject.cs" />
@@ -159,7 +169,6 @@
<Compile Include="Schema\JsonSchemaBuilderTests.cs" />
<Compile Include="Schema\JsonSchemaGeneratorTests.cs" />
<Compile Include="Schema\JsonSchemaTests.cs" />
<Compile Include="JsonRawTests.cs" />
<Compile Include="TestObjects\DateTimeTestClass.cs" />
<Compile Include="Converters\IsoDateTimeConverterTests.cs" />
<Compile Include="JsonConvertTest.cs" />
@@ -177,12 +186,14 @@
<Compile Include="SilverlightTests.cs" />
<Compile Include="TestFixtureBase.cs" />
<Compile Include="Converters\XmlNodeConverterTest.cs" />
<Compile Include="TestObjects\StructTest.cs" />
<Compile Include="TestObjects\SubKlass.cs" />
<Compile Include="TestObjects\SuperKlass.cs" />
<Compile Include="TestObjects\TypeClass.cs" />
<Compile Include="TestObjects\TypedSubHashtable.cs" />
<Compile Include="TestObjects\UserNullable.cs" />
<Compile Include="TestObjects\VersionKeyedCollection.cs" />
<Compile Include="TestObjects\WagePerson.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Newtonsoft.Json\Newtonsoft.Json.Compact.csproj">
@@ -75,6 +75,7 @@
<Compile Include="Linq\ComponentModel\JTypeDescriptionProviderTests.cs" />
<Compile Include="Linq\ComponentModel\JTypeDescriptorTests.cs" />
<Compile Include="Linq\JPathTests.cs" />
<Compile Include="Linq\JRawTests.cs" />
<Compile Include="Serialization\ConstructorHandlingTests.cs" />
<Compile Include="TestObjects\Container.cs" />
<Compile Include="TestObjects\Content.cs" />
@@ -165,7 +166,6 @@
<Compile Include="Schema\JsonSchemaBuilderTests.cs" />
<Compile Include="Schema\JsonSchemaGeneratorTests.cs" />
<Compile Include="Schema\JsonSchemaTests.cs" />
<Compile Include="JsonRawTests.cs" />
<Compile Include="TestObjects\DateTimeTestClass.cs" />
<Compile Include="Converters\IsoDateTimeConverterTests.cs" />
<Compile Include="JsonConvertTest.cs" />
@@ -79,6 +79,7 @@
<Compile Include="Linq\ComponentModel\JTypeDescriptionProviderTests.cs" />
<Compile Include="Linq\ComponentModel\JTypeDescriptorTests.cs" />
<Compile Include="Linq\JPathTests.cs" />
<Compile Include="Linq\JRawTests.cs" />
<Compile Include="Linq\JTokenReaderTest.cs" />
<Compile Include="Linq\JTokenWriterTest.cs" />
<Compile Include="JsonValidatingReaderTests.cs" />
@@ -98,10 +99,14 @@
<Compile Include="TestObjects\AbstractGenericBase.cs" />
<Compile Include="TestObjects\ArgumentConverterPrecedenceClassConverter.cs" />
<Compile Include="TestObjects\BadJsonPropertyClass.cs" />
<Compile Include="TestObjects\Bar.cs" />
<Compile Include="TestObjects\Car.cs" />
<Compile Include="TestObjects\CircularReferenceClass.cs" />
<Compile Include="TestObjects\ClassAndMemberConverterClass.cs" />
<Compile Include="TestObjects\ClassConverterPrecedenceClassConverter.cs" />
<Compile Include="TestObjects\Computer.cs" />
<Compile Include="TestObjects\ConstructorCaseSensitivityClass.cs" />
<Compile Include="TestObjects\ConstructorReadonlyFields.cs" />
<Compile Include="TestObjects\Container.cs" />
<Compile Include="TestObjects\Content.cs" />
<Compile Include="TestObjects\ConverterPrecedenceClass.cs" />
@@ -113,6 +118,7 @@
<Compile Include="TestObjects\DoubleClass.cs" />
<Compile Include="TestObjects\EmployeeReference.cs" />
<Compile Include="TestObjects\Event.cs" />
<Compile Include="TestObjects\Foo.cs" />
<Compile Include="TestObjects\GenericImpl.cs" />
<Compile Include="TestObjects\GenericListAndDictionaryInterfaceProperties.cs" />
<Compile Include="TestObjects\GetOnlyPropertyClass.cs" />
@@ -126,16 +132,19 @@
<Compile Include="TestObjects\JsonPropertyWithHandlingValues.cs" />
<Compile Include="TestObjects\ListErrorObject.cs" />
<Compile Include="TestObjects\ListErrorObjectCollection.cs" />
<Compile Include="TestObjects\ListOfIds.cs" />
<Compile Include="TestObjects\ListTestClass.cs" />
<Compile Include="TestObjects\LogEntry.cs" />
<Compile Include="TestObjects\MemberConverterPrecedenceClassConverter.cs" />
<Compile Include="TestObjects\MethodExecutorObject.cs" />
<Compile Include="TestObjects\Movie.cs" />
<Compile Include="TestObjects\MyClass.cs" />
<Compile Include="TestObjects\Name.cs" />
<Compile Include="TestObjects\NonRequest.cs" />
<Compile Include="TestObjects\NullableDateTimeTestClass.cs" />
<Compile Include="TestObjects\ObjectArrayPropertyTest.cs" />
<Compile Include="TestObjects\PersonError.cs" />
<Compile Include="TestObjects\PersonPropertyClass.cs" />
<Compile Include="TestObjects\PersonRaw.cs" />
<Compile Include="TestObjects\PhoneNumber.cs" />
<Compile Include="TestObjects\PrivateConstructorTestClass.cs" />
@@ -145,6 +154,7 @@
<Compile Include="TestObjects\RequestOnly.cs" />
<Compile Include="TestObjects\RequiredMembersClass.cs" />
<Compile Include="TestObjects\RoleTransfer.cs" />
<Compile Include="TestObjects\SearchResult.cs" />
<Compile Include="TestObjects\SerializationEventTestDictionary.cs" />
<Compile Include="TestObjects\SerializationEventTestList.cs" />
<Compile Include="TestObjects\SerializationEventTestObject.cs" />
@@ -170,7 +180,6 @@
<Compile Include="Schema\JsonSchemaBuilderTests.cs" />
<Compile Include="Schema\JsonSchemaGeneratorTests.cs" />
<Compile Include="Schema\JsonSchemaTests.cs" />
<Compile Include="JsonRawTests.cs" />
<Compile Include="TestObjects\DateTimeTestClass.cs" />
<Compile Include="Converters\IsoDateTimeConverterTests.cs" />
<Compile Include="JsonConvertTest.cs" />
@@ -188,12 +197,14 @@
<Compile Include="SilverlightTests.cs" />
<Compile Include="TestFixtureBase.cs" />
<Compile Include="Converters\XmlNodeConverterTest.cs" />
<Compile Include="TestObjects\StructTest.cs" />
<Compile Include="TestObjects\SubKlass.cs" />
<Compile Include="TestObjects\SuperKlass.cs" />
<Compile Include="TestObjects\TypeClass.cs" />
<Compile Include="TestObjects\TypedSubHashtable.cs" />
<Compile Include="TestObjects\UserNullable.cs" />
<Compile Include="TestObjects\VersionKeyedCollection.cs" />
<Compile Include="TestObjects\WagePerson.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Newtonsoft.Json\Newtonsoft.Json.Silverlight.csproj">
@@ -114,15 +114,21 @@
<Compile Include="Linq\ComponentModel\JTypeDescriptionProviderTests.cs" />
<Compile Include="Linq\ComponentModel\JTypeDescriptorTests.cs" />
<Compile Include="Linq\JPathTests.cs" />
<Compile Include="Linq\JRawTests.cs" />
<Compile Include="Serialization\ConstructorHandlingTests.cs" />
<Compile Include="Serialization\ContractResolverTests.cs" />
<Compile Include="Serialization\DefaultValueHandlingTests.cs" />
<Compile Include="Serialization\NullValueHandlingTests.cs" />
<Compile Include="TestObjects\Bar.cs" />
<Compile Include="TestObjects\Car.cs" />
<Compile Include="TestObjects\Computer.cs" />
<Compile Include="TestObjects\ConstructorReadonlyFields.cs" />
<Compile Include="TestObjects\Container.cs" />
<Compile Include="TestObjects\Foo.cs" />
<Compile Include="TestObjects\ListOfIds.cs" />
<Compile Include="TestObjects\Movie.cs" />
<Compile Include="TestObjects\PersonError.cs" />
<Compile Include="TestObjects\PersonPropertyClass.cs" />
<Compile Include="TestObjects\PrivateConstructorTestClass.cs" />
<Compile Include="TestObjects\PrivateConstructorWithPublicParametizedConstructorTestClass.cs" />
<Compile Include="TestObjects\Content.cs" />
@@ -149,6 +155,9 @@
<Compile Include="TestObjects\LogEntry.cs" />
<Compile Include="TestObjects\NonRequest.cs" />
<Compile Include="TestObjects\ObjectArrayPropertyTest.cs" />
<Compile Include="TestObjects\SearchResult.cs" />
<Compile Include="TestObjects\StructTest.cs" />
<Compile Include="TestObjects\WagePerson.cs" />
<Compile Include="TestObjects\PropertyCase.cs" />
<Compile Include="TestObjects\RequestOnly.cs" />
<Compile Include="TestObjects\RoleTransfer.cs" />
@@ -209,7 +218,6 @@
<Compile Include="Schema\JsonSchemaBuilderTests.cs" />
<Compile Include="Schema\JsonSchemaGeneratorTests.cs" />
<Compile Include="Schema\JsonSchemaTests.cs" />
<Compile Include="JsonRawTests.cs" />
<Compile Include="TestObjects\NullableDateTimeTestClass.cs" />
<Compile Include="TestObjects\DateTimeTestClass.cs" />
<Compile Include="Converters\IsoDateTimeConverterTests.cs" />
+40 -43
View File
@@ -16,13 +16,10 @@ using Newtonsoft.Json.Bson;
namespace Newtonsoft.Json.Tests
{
/// <summary>
/// Summary description for JsonSerializerTest
/// </summary>
public class PerformanceTests : TestFixtureBase
{
private int Iterations = 100;
//private int Iterations = 5000;
private const int Iterations = 100;
//private const int Iterations = 5000;
private const string BsonHex =
@"B4-01-00-00-04-73-74-72-69-6E-67-73-00-44-00-00-00-02-30-00-05-00-00-00-52-69-63-6B-00-02-31-00-17-00-00-00-4D-61-72-6B-75-73-20-65-67-67-65-72-20-5D-5B-2C-20-28-32-6E-64-29-00-02-32-00-0E-00-00-00-4B-65-76-69-6E-20-4D-63-4E-65-69-73-68-00-00-03-64-69-63-74-69-6F-6E-61-72-79-00-27-00-00-00-10-56-61-6C-20-61-73-64-31-00-01-00-00-00-10-56-61-6C-32-00-03-00-00-00-10-56-61-6C-33-00-04-00-00-00-00-02-4E-61-6D-65-00-05-00-00-00-52-69-63-6B-00-09-4E-6F-77-00-A0-80-DB-70-25-01-00-00-01-42-69-67-4E-75-6D-62-65-72-00-E7-7B-CC-26-96-C7-1F-42-03-41-64-64-72-65-73-73-31-00-48-00-00-00-02-41-64-64-72-65-73-73-00-0B-00-00-00-66-66-66-20-53-74-72-65-65-74-00-02-50-68-6F-6E-65-00-0F-00-00-00-28-35-30-33-29-20-38-31-34-2D-36-33-33-35-00-09-45-6E-74-65-72-65-64-00-20-C2-A3-D7-25-01-00-00-00-04-41-64-64-72-65-73-73-65-73-00-A3-00-00-00-03-30-00-4B-00-00-00-02-41-64-64-72-65-73-73-00-0E-00-00-00-61-72-72-61-79-20-61-64-64-72-65-73-73-00-02-50-68-6F-6E-65-00-0F-00-00-00-28-35-30-33-29-20-38-31-34-2D-36-33-33-35-00-09-45-6E-74-65-72-65-64-00-20-36-7E-6B-25-01-00-00-00-03-31-00-4D-00-00-00-02-41-64-64-72-65-73-73-00-10-00-00-00-61-72-72-61-79-20-32-20-61-64-64-72-65-73-73-00-02-50-68-6F-6E-65-00-0F-00-00-00-28-35-30-33-29-20-38-31-34-2D-36-33-33-35-00-09-45-6E-74-65-72-65-64-00-20-DA-57-66-25-01-00-00-00-00-00";
@@ -71,6 +68,44 @@ namespace Newtonsoft.Json.Tests
private DateTime BaseDate = DateTime.Parse("01/01/2000");
[Test]
public void Serialize()
{
TestClass test = new TestClass();
test.Address1.Address = "fff Street";
test.Address1.Entered = DateTime.Now.AddDays(20);
test.BigNumber = 34123123123.121M;
test.Now = DateTime.Now.AddHours(1);
test.strings = new List<string>() { "Rick", "Markus egger ][, (2nd)", "Kevin McNeish" };
cAddress address = new cAddress();
address.Entered = DateTime.Now.AddDays(-1);
address.Address = "array address";
test.Addresses.Add(address);
address = new cAddress();
address.Entered = DateTime.Now.AddDays(-2);
address.Address = "array 2 address";
test.Addresses.Add(address);
BenchmarkSerializeMethod(SerializeMethod.JsonNet, test);
BenchmarkSerializeMethod(SerializeMethod.JsonNetBinary, test);
BenchmarkSerializeMethod(SerializeMethod.JavaScriptSerializer, test);
BenchmarkSerializeMethod(SerializeMethod.DataContractJsonSerializer, test);
}
[Test]
public void Deserialize()
{
BenchmarkDeserializeMethod<TestClass>(SerializeMethod.JsonNet, JsonText);
BenchmarkDeserializeMethod<TestClass>(SerializeMethod.JsonNetBinary, MiscellaneousUtils.HexToBytes(BsonHex));
BenchmarkDeserializeMethod<TestClass>(SerializeMethod.JavaScriptSerializer, JsonText);
BenchmarkDeserializeMethod<TestClass>(SerializeMethod.DataContractJsonSerializer, JsonText);
}
public string SerializeJsonNet(object value)
{
Type type = value.GetType();
@@ -237,44 +272,6 @@ namespace Newtonsoft.Json.Tests
Console.WriteLine(value);
Console.WriteLine();
}
[Test]
public void Serialize()
{
TestClass test = new TestClass();
test.Address1.Address = "fff Street";
test.Address1.Entered = DateTime.Now.AddDays(20);
test.BigNumber = 34123123123.121M;
test.Now = DateTime.Now.AddHours(1);
test.strings = new List<string>() { "Rick", "Markus egger ][, (2nd)", "Kevin McNeish" };
cAddress address = new cAddress();
address.Entered = DateTime.Now.AddDays(-1);
address.Address = "array address";
test.Addresses.Add(address);
address = new cAddress();
address.Entered = DateTime.Now.AddDays(-2);
address.Address = "array 2 address";
test.Addresses.Add(address);
BenchmarkSerializeMethod(SerializeMethod.JsonNet, test);
BenchmarkSerializeMethod(SerializeMethod.JsonNetBinary, test);
BenchmarkSerializeMethod(SerializeMethod.JavaScriptSerializer, test);
BenchmarkSerializeMethod(SerializeMethod.DataContractJsonSerializer, test);
}
[Test]
public void Deserialize()
{
BenchmarkDeserializeMethod<TestClass>(SerializeMethod.JsonNet, JsonText);
BenchmarkDeserializeMethod<TestClass>(SerializeMethod.JsonNetBinary, MiscellaneousUtils.HexToBytes(BsonHex));
BenchmarkDeserializeMethod<TestClass>(SerializeMethod.JavaScriptSerializer, JsonText);
BenchmarkDeserializeMethod<TestClass>(SerializeMethod.DataContractJsonSerializer, JsonText);
}
}
@@ -855,7 +855,7 @@ keyword such as type of business.""
PersonRaw personRaw = new PersonRaw
{
FirstName = "FirstNameValue",
RawContent = new JsonRaw("[1,2,3,4,5]"),
RawContent = new JRaw("[1,2,3,4,5]"),
LastName = "LastNameValue"
};
@@ -873,7 +873,7 @@ keyword such as type of business.""
PersonRaw personRaw = JsonConvert.DeserializeObject<PersonRaw>(json);
Assert.AreEqual("FirstNameValue", personRaw.FirstName);
Assert.AreEqual("[1,2,3,4,5]", personRaw.RawContent.Content);
Assert.AreEqual("[1,2,3,4,5]", personRaw.RawContent.ToString());
Assert.AreEqual("LastNameValue", personRaw.LastName);
}
@@ -1852,18 +1852,6 @@ keyword such as type of business.""
Assert.AreEqual(@"[""str_1"",""str_2"",""str_3""]", json);
}
public class ConstructorReadonlyFields
{
public readonly string A;
public readonly int B;
public ConstructorReadonlyFields(string a, int b)
{
A = a;
B = b;
}
}
[Test]
public void ConstructorReadonlyFieldsTest()
{
@@ -1879,14 +1867,6 @@ keyword such as type of business.""
Assert.AreEqual(int.MaxValue, c2.B);
}
public struct StructTest
{
public string StringProperty { get; set; }
public string StringField;
public int IntProperty { get; set; }
public int IntField;
}
[Test]
public void SerializeStruct()
{
@@ -1914,62 +1894,6 @@ keyword such as type of business.""
Assert.AreEqual(structTest.IntField, deserialized.IntField);
}
public class ListOfIds<T> : JsonConverter where T : Bar, new()
{
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
IList<T> list = (IList<T>) value;
writer.WriteStartArray();
foreach (T item in list)
{
writer.WriteValue(item.Id);
}
writer.WriteEndArray();
}
public override object ReadJson(JsonReader reader, Type objectType, JsonSerializer serializer)
{
IList<T> list = new List<T>();
reader.Read();
while (reader.TokenType != JsonToken.EndArray)
{
long id = (long)reader.Value;
list.Add(new T
{
Id = Convert.ToInt32(id)
});
reader.Read();
}
return list;
}
public override bool CanConvert(Type objectType)
{
return typeof (IList<T>).IsAssignableFrom(objectType);
}
}
public class Foo
{
public Foo()
{
Bars = new List<Bar>();
}
[JsonConverter(typeof(ListOfIds<Bar>))]
public List<Bar> Bars { get; set; }
}
public class Bar
{
public int Id { get; set; }
}
[Test]
public void SerializeListWithJsonConverter()
{
@@ -2019,13 +1943,6 @@ keyword such as type of business.""
JsonConvert.SerializeObject(dictionary, Formatting.Indented);
}
public class SearchResult
{
public string Title { get; set; }
public string Content { get; set; }
public string Url { get; set; }
}
[Test]
public void SerializeFragment()
{
@@ -2106,5 +2023,33 @@ keyword such as type of business.""
Assert.AreEqual("<b>Paris Hilton</b> - Wikipedia, the free encyclopedia", searchResults[0].Title);
Assert.AreEqual("<b>Paris Hilton</b>", searchResults[1].Title);
}
[Test]
public void DeserializeBaseReferenceWithDerivedValue()
{
PersonPropertyClass personPropertyClass = new PersonPropertyClass();
WagePerson wagePerson = (WagePerson) personPropertyClass.Person;
wagePerson.BirthDate = new DateTime(2000, 11, 29, 23, 59, 59, DateTimeKind.Utc);
wagePerson.Department = "McDees";
wagePerson.HourlyWage = 12.50m;
wagePerson.LastModified = new DateTime(2000, 11, 29, 23, 59, 59, DateTimeKind.Utc);
wagePerson.Name = "Jim Bob";
string json = JsonConvert.SerializeObject(personPropertyClass, Formatting.Indented);
Assert.AreEqual(
@"{
""Person"": {
""HourlyWage"": 12.50,
""Name"": ""Jim Bob"",
""BirthDate"": ""\/Date(975542399000)\/"",
""LastModified"": ""\/Date(975542399000)\/""
}
}",
json);
PersonPropertyClass newPersonPropertyClass = JsonConvert.DeserializeObject<PersonPropertyClass>(json);
Assert.AreEqual(wagePerson.HourlyWage, ((WagePerson) newPersonPropertyClass.Person).HourlyWage);
}
}
}
@@ -30,6 +30,7 @@ using System.Linq;
using System.Text;
using Newtonsoft.Json.Tests.TestObjects;
using NUnit.Framework;
using Newtonsoft.Json.Linq;
namespace Newtonsoft.Json.Tests.Serialization
{
@@ -131,5 +132,12 @@ namespace Newtonsoft.Json.Tests.Serialization
Assert.AreEqual(1, p.Count);
Assert.AreEqual("James", p["Name"]);
}
[Test]
[ExpectedException(typeof(JsonSerializationException), ExpectedMessage = "Unexpected initial token 'Integer' when populating object. Expected JSON object or array.")]
public void PopulateWithBadJson()
{
JsonConvert.PopulateObject("1", new Person());
}
}
}
@@ -0,0 +1,32 @@
#region License
// Copyright (c) 2007 James Newton-King
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#endregion
namespace Newtonsoft.Json.Tests.TestObjects
{
public class Bar
{
public int Id { get; set; }
}
}
@@ -1,9 +1,32 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
#region License
// Copyright (c) 2007 James Newton-King
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#endregion
using System;
using System.Runtime.Serialization;
#if !PocketPC && !NET20
namespace Newtonsoft.Json.Tests.TestObjects
{
[DataContract]
@@ -22,3 +45,4 @@ namespace Newtonsoft.Json.Tests.TestObjects
public DateTime NextShipmentDate { get; set; }
}
}
#endif
@@ -0,0 +1,39 @@
#region License
// Copyright (c) 2007 James Newton-King
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#endregion
namespace Newtonsoft.Json.Tests.TestObjects
{
public class ConstructorReadonlyFields
{
public readonly string A;
public readonly int B;
public ConstructorReadonlyFields(string a, int b)
{
A = a;
B = b;
}
}
}
@@ -0,0 +1,40 @@
#region License
// Copyright (c) 2007 James Newton-King
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#endregion
using System.Collections.Generic;
namespace Newtonsoft.Json.Tests.TestObjects
{
public class Foo
{
public Foo()
{
Bars = new List<Bar>();
}
[JsonConverter(typeof(ListOfIds<Bar>))]
public List<Bar> Bars { get; set; }
}
}
@@ -0,0 +1,70 @@
#region License
// Copyright (c) 2007 James Newton-King
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#endregion
using System;
using System.Collections.Generic;
namespace Newtonsoft.Json.Tests.TestObjects
{
public class ListOfIds<T> : JsonConverter where T : Bar, new()
{
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
IList<T> list = (IList<T>)value;
writer.WriteStartArray();
foreach (T item in list)
{
writer.WriteValue(item.Id);
}
writer.WriteEndArray();
}
public override object ReadJson(JsonReader reader, Type objectType, JsonSerializer serializer)
{
IList<T> list = new List<T>();
reader.Read();
while (reader.TokenType != JsonToken.EndArray)
{
long id = (long)reader.Value;
list.Add(new T
{
Id = Convert.ToInt32(id)
});
reader.Read();
}
return list;
}
public override bool CanConvert(Type objectType)
{
return typeof(IList<T>).IsAssignableFrom(objectType);
}
}
}
@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Newtonsoft.Json.Tests.TestObjects
{
public class PersonPropertyClass
{
public Person Person { get; set; }
public PersonPropertyClass()
{
Person = new WagePerson();
}
}
}
@@ -24,6 +24,7 @@
#endregion
using System;
using Newtonsoft.Json.Linq;
namespace Newtonsoft.Json.Tests.TestObjects
{
@@ -32,7 +33,7 @@ namespace Newtonsoft.Json.Tests.TestObjects
private Guid _internalId;
private string _firstName;
private string _lastName;
private JsonRaw _rawContent;
private JRaw _rawContent;
[JsonIgnore]
public Guid InternalId
@@ -48,7 +49,7 @@ namespace Newtonsoft.Json.Tests.TestObjects
set { _firstName = value; }
}
public JsonRaw RawContent
public JRaw RawContent
{
get { return _rawContent; }
set { _rawContent = value; }
@@ -0,0 +1,34 @@
#region License
// Copyright (c) 2007 James Newton-King
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#endregion
namespace Newtonsoft.Json.Tests.TestObjects
{
public class SearchResult
{
public string Title { get; set; }
public string Content { get; set; }
public string Url { get; set; }
}
}
@@ -0,0 +1,35 @@
#region License
// Copyright (c) 2007 James Newton-King
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#endregion
namespace Newtonsoft.Json.Tests.TestObjects
{
public struct StructTest
{
public string StringProperty { get; set; }
public string StringField;
public int IntProperty { get; set; }
public int IntField;
}
}
@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Newtonsoft.Json.Tests.TestObjects
{
public class WagePerson : Person
{
[JsonProperty]
public decimal HourlyWage { get; set; }
}
}