-Tests
This commit is contained in:
@@ -53,8 +53,10 @@ using ErrorEventArgs = Newtonsoft.Json.Serialization.ErrorEventArgs;
|
|||||||
|
|
||||||
namespace Newtonsoft.Json.Tests.Documentation
|
namespace Newtonsoft.Json.Tests.Documentation
|
||||||
{
|
{
|
||||||
public class SerializationTests
|
[TestFixture]
|
||||||
|
public class SerializationTests : TestFixtureBase
|
||||||
{
|
{
|
||||||
|
[Test]
|
||||||
public void SerializeObject()
|
public void SerializeObject()
|
||||||
{
|
{
|
||||||
#region SerializeObject
|
#region SerializeObject
|
||||||
@@ -79,6 +81,8 @@ namespace Newtonsoft.Json.Tests.Documentation
|
|||||||
|
|
||||||
Product deserializedProduct = JsonConvert.DeserializeObject<Product>(output);
|
Product deserializedProduct = JsonConvert.DeserializeObject<Product>(output);
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
Assert.AreEqual("Apple", deserializedProduct.Name);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void JsonSerializerToStream()
|
public void JsonSerializerToStream()
|
||||||
@@ -175,6 +179,7 @@ namespace Newtonsoft.Json.Tests.Documentation
|
|||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
[Test]
|
||||||
public void SerializationCallbacksExample()
|
public void SerializationCallbacksExample()
|
||||||
{
|
{
|
||||||
#region SerializationCallbacksExample
|
#region SerializationCallbacksExample
|
||||||
@@ -216,8 +221,11 @@ namespace Newtonsoft.Json.Tests.Documentation
|
|||||||
Console.WriteLine(obj.Member4);
|
Console.WriteLine(obj.Member4);
|
||||||
// This value was set after deserialization.
|
// This value was set after deserialization.
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
Assert.AreEqual(11, obj.Member1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
public void SerializationErrorHandling()
|
public void SerializationErrorHandling()
|
||||||
{
|
{
|
||||||
#region SerializationErrorHandling
|
#region SerializationErrorHandling
|
||||||
@@ -251,8 +259,11 @@ namespace Newtonsoft.Json.Tests.Documentation
|
|||||||
// Unexpected token parsing date. Expected String, got StartArray.
|
// Unexpected token parsing date. Expected String, got StartArray.
|
||||||
// Cannot convert null value to System.DateTime.
|
// Cannot convert null value to System.DateTime.
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
Assert.AreEqual(new DateTime(2009, 9, 9, 0, 0, 0, DateTimeKind.Utc), c[0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
public void SerializationErrorHandlingWithParent()
|
public void SerializationErrorHandlingWithParent()
|
||||||
{
|
{
|
||||||
#region SerializationErrorHandlingWithParent
|
#region SerializationErrorHandlingWithParent
|
||||||
@@ -302,6 +313,7 @@ namespace Newtonsoft.Json.Tests.Documentation
|
|||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
[Test]
|
||||||
public void SerializationErrorHandlingAttributeExample()
|
public void SerializationErrorHandlingAttributeExample()
|
||||||
{
|
{
|
||||||
#region SerializationErrorHandlingAttributeExample
|
#region SerializationErrorHandlingAttributeExample
|
||||||
@@ -322,6 +334,12 @@ namespace Newtonsoft.Json.Tests.Documentation
|
|||||||
// "Title": "Mister Manager"
|
// "Title": "Mister Manager"
|
||||||
//}
|
//}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
StringAssert.AreEqual(@"{
|
||||||
|
""Name"": ""George Michael Bluth"",
|
||||||
|
""Age"": 16,
|
||||||
|
""Title"": ""Mister Manager""
|
||||||
|
}", json);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void PreservingObjectReferencesOff()
|
public void PreservingObjectReferencesOff()
|
||||||
@@ -352,11 +370,33 @@ namespace Newtonsoft.Json.Tests.Documentation
|
|||||||
// }
|
// }
|
||||||
//]
|
//]
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
StringAssert.AreEqual(@"[
|
||||||
|
{
|
||||||
|
""Name"": ""James"",
|
||||||
|
""BirthDate"": ""1980-12-23T00:00:00Z"",
|
||||||
|
""LastModified"": ""2009-02-20T12:59:21Z""
|
||||||
|
},
|
||||||
|
{
|
||||||
|
""Name"": ""James"",
|
||||||
|
""BirthDate"": ""1980-12-23T00:00:00Z"",
|
||||||
|
""LastModified"": ""2009-02-20T12:59:21Z""
|
||||||
|
}
|
||||||
|
]", json);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
public void PreservingObjectReferencesOn()
|
public void PreservingObjectReferencesOn()
|
||||||
{
|
{
|
||||||
List<Person> people = new List<Person>();
|
Person p = new Person
|
||||||
|
{
|
||||||
|
Name = "James"
|
||||||
|
};
|
||||||
|
List<Person> people = new List<Person>
|
||||||
|
{
|
||||||
|
p,
|
||||||
|
p
|
||||||
|
};
|
||||||
|
|
||||||
#region PreservingObjectReferencesOn
|
#region PreservingObjectReferencesOn
|
||||||
string json = JsonConvert.SerializeObject(people, Formatting.Indented,
|
string json = JsonConvert.SerializeObject(people, Formatting.Indented,
|
||||||
@@ -391,6 +431,8 @@ namespace Newtonsoft.Json.Tests.Documentation
|
|||||||
bool equal = Object.ReferenceEquals(p1, p2);
|
bool equal = Object.ReferenceEquals(p1, p2);
|
||||||
// true
|
// true
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
Assert.AreEqual(true, equal);
|
||||||
}
|
}
|
||||||
|
|
||||||
#region PreservingObjectReferencesAttribute
|
#region PreservingObjectReferencesAttribute
|
||||||
@@ -429,9 +471,25 @@ namespace Newtonsoft.Json.Tests.Documentation
|
|||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
[Test]
|
||||||
public void CustomCreationConverterExample()
|
public void CustomCreationConverterExample()
|
||||||
{
|
{
|
||||||
string json = null;
|
string json = @"[
|
||||||
|
{
|
||||||
|
""FirstName"": ""Maurice"",
|
||||||
|
""LastName"": ""Moss"",
|
||||||
|
""BirthDate"": ""1981-03-08T00:00Z"",
|
||||||
|
""Department"": ""IT"",
|
||||||
|
""JobTitle"": ""Support""
|
||||||
|
},
|
||||||
|
{
|
||||||
|
""FirstName"": ""Jen"",
|
||||||
|
""LastName"": ""Barber"",
|
||||||
|
""BirthDate"": ""1985-12-10T00:00Z"",
|
||||||
|
""Department"": ""IT"",
|
||||||
|
""JobTitle"": ""Manager""
|
||||||
|
}
|
||||||
|
]";
|
||||||
|
|
||||||
#region CustomCreationConverterExample
|
#region CustomCreationConverterExample
|
||||||
//[
|
//[
|
||||||
@@ -466,8 +524,11 @@ namespace Newtonsoft.Json.Tests.Documentation
|
|||||||
Console.WriteLine(employee.JobTitle);
|
Console.WriteLine(employee.JobTitle);
|
||||||
// Support
|
// Support
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
Assert.AreEqual("Support", employee.JobTitle);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
public void ContractResolver()
|
public void ContractResolver()
|
||||||
{
|
{
|
||||||
#region ContractResolver
|
#region ContractResolver
|
||||||
@@ -488,7 +549,7 @@ namespace Newtonsoft.Json.Tests.Documentation
|
|||||||
|
|
||||||
//{
|
//{
|
||||||
// "name": "Widget",
|
// "name": "Widget",
|
||||||
// "expiryDate": "2010-12-20T18:01Z",
|
// "expiryDate": "2010-12-20T18:01:00Z",
|
||||||
// "price": 9.99,
|
// "price": 9.99,
|
||||||
// "sizes": [
|
// "sizes": [
|
||||||
// "Small",
|
// "Small",
|
||||||
@@ -497,8 +558,20 @@ namespace Newtonsoft.Json.Tests.Documentation
|
|||||||
// ]
|
// ]
|
||||||
//}
|
//}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
Assert.AreEqual(@"{
|
||||||
|
""name"": ""Widget"",
|
||||||
|
""expiryDate"": ""2010-12-20T18:01:00Z"",
|
||||||
|
""price"": 9.99,
|
||||||
|
""sizes"": [
|
||||||
|
""Small"",
|
||||||
|
""Medium"",
|
||||||
|
""Large""
|
||||||
|
]
|
||||||
|
}", json);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
public void SerializingCollectionsSerializing()
|
public void SerializingCollectionsSerializing()
|
||||||
{
|
{
|
||||||
#region SerializingCollectionsSerializing
|
#region SerializingCollectionsSerializing
|
||||||
@@ -523,20 +596,36 @@ namespace Newtonsoft.Json.Tests.Documentation
|
|||||||
//[
|
//[
|
||||||
// {
|
// {
|
||||||
// "Name": "Product 1",
|
// "Name": "Product 1",
|
||||||
// "ExpiryDate": "2000-12-29T00:00Z",
|
// "ExpiryDate": "2000-12-29T00:00:00Z",
|
||||||
// "Price": 99.95,
|
// "Price": 99.95,
|
||||||
// "Sizes": null
|
// "Sizes": null
|
||||||
// },
|
// },
|
||||||
// {
|
// {
|
||||||
// "Name": "Product 2",
|
// "Name": "Product 2",
|
||||||
// "ExpiryDate": "2009-07-31T00:00Z",
|
// "ExpiryDate": "2009-07-31T00:00:00Z",
|
||||||
// "Price": 12.50,
|
// "Price": 12.50,
|
||||||
// "Sizes": null
|
// "Sizes": null
|
||||||
// }
|
// }
|
||||||
//]
|
//]
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
Assert.AreEqual(@"[
|
||||||
|
{
|
||||||
|
""Name"": ""Product 1"",
|
||||||
|
""ExpiryDate"": ""2000-12-29T00:00:00Z"",
|
||||||
|
""Price"": 99.95,
|
||||||
|
""Sizes"": null
|
||||||
|
},
|
||||||
|
{
|
||||||
|
""Name"": ""Product 2"",
|
||||||
|
""ExpiryDate"": ""2009-07-31T00:00:00Z"",
|
||||||
|
""Price"": 12.50,
|
||||||
|
""Sizes"": null
|
||||||
|
}
|
||||||
|
]", json);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
public void SerializingCollectionsDeserializing()
|
public void SerializingCollectionsDeserializing()
|
||||||
{
|
{
|
||||||
#region SerializingCollectionsDeserializing
|
#region SerializingCollectionsDeserializing
|
||||||
@@ -565,8 +654,11 @@ namespace Newtonsoft.Json.Tests.Documentation
|
|||||||
Console.WriteLine(p1.Name);
|
Console.WriteLine(p1.Name);
|
||||||
// Product 1
|
// Product 1
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
Assert.AreEqual("Product 1", p1.Name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
public void SerializingCollectionsDeserializingDictionaries()
|
public void SerializingCollectionsDeserializingDictionaries()
|
||||||
{
|
{
|
||||||
#region SerializingCollectionsDeserializingDictionaries
|
#region SerializingCollectionsDeserializingDictionaries
|
||||||
@@ -580,6 +672,8 @@ namespace Newtonsoft.Json.Tests.Documentation
|
|||||||
Console.WriteLine(values["key1"]);
|
Console.WriteLine(values["key1"]);
|
||||||
// value1
|
// value1
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
Assert.AreEqual("value1", values["key1"]);
|
||||||
}
|
}
|
||||||
|
|
||||||
#region SerializingDatesInJson
|
#region SerializingDatesInJson
|
||||||
@@ -658,6 +752,7 @@ namespace Newtonsoft.Json.Tests.Documentation
|
|||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
[Test]
|
||||||
public void ReducingSerializedJsonSizeNullValueHandlingExample()
|
public void ReducingSerializedJsonSizeNullValueHandlingExample()
|
||||||
{
|
{
|
||||||
#region ReducingSerializedJsonSizeNullValueHandlingExample
|
#region ReducingSerializedJsonSizeNullValueHandlingExample
|
||||||
@@ -687,6 +782,20 @@ namespace Newtonsoft.Json.Tests.Documentation
|
|||||||
// "Description": "It's no Bad Boys"
|
// "Description": "It's no Bad Boys"
|
||||||
// }
|
// }
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
Assert.AreEqual(@"{
|
||||||
|
""Name"": ""Bad Boys III"",
|
||||||
|
""Description"": ""It's no Bad Boys"",
|
||||||
|
""Classification"": null,
|
||||||
|
""Studio"": null,
|
||||||
|
""ReleaseDate"": null,
|
||||||
|
""ReleaseCountries"": null
|
||||||
|
}", included);
|
||||||
|
|
||||||
|
Assert.AreEqual(@"{
|
||||||
|
""Name"": ""Bad Boys III"",
|
||||||
|
""Description"": ""It's no Bad Boys""
|
||||||
|
}", ignored);
|
||||||
}
|
}
|
||||||
|
|
||||||
#region ReducingSerializedJsonSizeDefaultValueHandlingObject
|
#region ReducingSerializedJsonSizeDefaultValueHandlingObject
|
||||||
@@ -744,6 +853,20 @@ namespace Newtonsoft.Json.Tests.Documentation
|
|||||||
// "Amount": 50.0
|
// "Amount": 50.0
|
||||||
// }
|
// }
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
Assert.AreEqual(@"{
|
||||||
|
""Company"": ""Acme Ltd."",
|
||||||
|
""Amount"": 50.0,
|
||||||
|
""Paid"": false,
|
||||||
|
""PaidDate"": null,
|
||||||
|
""FollowUpDays"": 30,
|
||||||
|
""FollowUpEmailAddress"": """"
|
||||||
|
}", included);
|
||||||
|
|
||||||
|
Assert.AreEqual(@"{
|
||||||
|
""Company"": ""Acme Ltd."",
|
||||||
|
""Amount"": 50.0
|
||||||
|
}", included);
|
||||||
}
|
}
|
||||||
|
|
||||||
#region ReducingSerializedJsonSizeContractResolverObject
|
#region ReducingSerializedJsonSizeContractResolverObject
|
||||||
@@ -807,6 +930,17 @@ namespace Newtonsoft.Json.Tests.Documentation
|
|||||||
// "BookPrice": 16.19
|
// "BookPrice": 16.19
|
||||||
// }
|
// }
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
Assert.AreEqual(@"{
|
||||||
|
""AuthorName"": ""Brandon Sanderson"",
|
||||||
|
""AuthorAge"": 34,
|
||||||
|
""AuthorCountry"": ""United States of America""
|
||||||
|
}", startingWithA);
|
||||||
|
|
||||||
|
Assert.AreEqual(@"{
|
||||||
|
""BookName"": ""The Gathering Storm"",
|
||||||
|
""BookPrice"": 16.19
|
||||||
|
}", startingWithB);
|
||||||
}
|
}
|
||||||
|
|
||||||
#region SerializingPartialJsonFragmentsObject
|
#region SerializingPartialJsonFragmentsObject
|
||||||
@@ -818,6 +952,7 @@ namespace Newtonsoft.Json.Tests.Documentation
|
|||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
[Test]
|
||||||
public void SerializingPartialJsonFragmentsExample()
|
public void SerializingPartialJsonFragmentsExample()
|
||||||
{
|
{
|
||||||
#region SerializingPartialJsonFragmentsExample
|
#region SerializingPartialJsonFragmentsExample
|
||||||
@@ -894,8 +1029,11 @@ namespace Newtonsoft.Json.Tests.Documentation
|
|||||||
// Content = Self: Zoolander. Socialite <b>Paris Hilton</b>...
|
// Content = Self: Zoolander. Socialite <b>Paris Hilton</b>...
|
||||||
// Url = http://www.imdb.com/name/nm0385296/
|
// Url = http://www.imdb.com/name/nm0385296/
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
Assert.AreEqual("<b>Paris Hilton</b> - Wikipedia, the free encyclopedia", searchResults[0].Title);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
public void SerializeMultidimensionalArrayExample()
|
public void SerializeMultidimensionalArrayExample()
|
||||||
{
|
{
|
||||||
string[,] famousCouples = new string[,]
|
string[,] famousCouples = new string[,]
|
||||||
@@ -918,6 +1056,8 @@ namespace Newtonsoft.Json.Tests.Documentation
|
|||||||
|
|
||||||
Console.WriteLine(deserialized[3, 0] + ", " + deserialized[3, 1]);
|
Console.WriteLine(deserialized[3, 0] + ", " + deserialized[3, 1]);
|
||||||
// Han, Leia
|
// Han, Leia
|
||||||
|
|
||||||
|
Assert.AreEqual("Han", deserialized[3, 0]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user