Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/mono/Newtonsoft.Json.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJamesNK <james@newtonking.com>2009-07-06 13:36:03 +0400
committerJamesNK <james@newtonking.com>2009-07-06 13:36:03 +0400
commit265abaf08ceb1c05b9267ad4fb183ad3ca520039 (patch)
treebfec15c72333104d7bc50c12ff8c0be8dc334304 /Src/Newtonsoft.Json.Tests/Serialization/PreserveReferencesHandlingTests.cs
parenta4022ec5d3f985834bddd2bab623606b1e684c3b (diff)
-Documentation
-Example unit tests for documentation -Added IsReference to JsonPropertyAttribute -Fixed resolving whether a property should be a reference
Diffstat (limited to 'Src/Newtonsoft.Json.Tests/Serialization/PreserveReferencesHandlingTests.cs')
-rw-r--r--Src/Newtonsoft.Json.Tests/Serialization/PreserveReferencesHandlingTests.cs78
1 files changed, 78 insertions, 0 deletions
diff --git a/Src/Newtonsoft.Json.Tests/Serialization/PreserveReferencesHandlingTests.cs b/Src/Newtonsoft.Json.Tests/Serialization/PreserveReferencesHandlingTests.cs
index 68f4790..c625e6c 100644
--- a/Src/Newtonsoft.Json.Tests/Serialization/PreserveReferencesHandlingTests.cs
+++ b/Src/Newtonsoft.Json.Tests/Serialization/PreserveReferencesHandlingTests.cs
@@ -650,5 +650,83 @@ namespace Newtonsoft.Json.Tests.Serialization
Assert.AreEqual(e1, employees["Three"]);
Assert.AreEqual(e2, employees["Four"]);
}
+
+ [Test]
+ public void ExampleWithout()
+ {
+ Person p = new Person
+ {
+ BirthDate = new DateTime(1980, 12, 23, 0, 0, 0, DateTimeKind.Utc),
+ LastModified = new DateTime(2009, 2, 20, 12, 59, 21, DateTimeKind.Utc),
+ Department = "IT",
+ Name = "James"
+ };
+
+ List<Person> people = new List<Person>();
+ people.Add(p);
+ people.Add(p);
+
+ string json = JsonConvert.SerializeObject(people, Formatting.Indented);
+ //[
+ // {
+ // "Name": "James",
+ // "BirthDate": "\/Date(346377600000)\/",
+ // "LastModified": "\/Date(1235134761000)\/"
+ // },
+ // {
+ // "Name": "James",
+ // "BirthDate": "\/Date(346377600000)\/",
+ // "LastModified": "\/Date(1235134761000)\/"
+ // }
+ //]
+ }
+
+ [Test]
+ public void ExampleWith()
+ {
+ Person p = new Person
+ {
+ BirthDate = new DateTime(1980, 12, 23, 0, 0, 0, DateTimeKind.Utc),
+ LastModified = new DateTime(2009, 2, 20, 12, 59, 21, DateTimeKind.Utc),
+ Department = "IT",
+ Name = "James"
+ };
+
+ List<Person> people = new List<Person>();
+ people.Add(p);
+ people.Add(p);
+
+ string json = JsonConvert.SerializeObject(people, Formatting.Indented,
+ new JsonSerializerSettings { PreserveReferencesHandling = PreserveReferencesHandling.Objects });
+ //[
+ // {
+ // "$id": "1",
+ // "Name": "James",
+ // "BirthDate": "\/Date(346377600000)\/",
+ // "LastModified": "\/Date(1235134761000)\/"
+ // },
+ // {
+ // "$ref": "1"
+ // }
+ //]
+
+ List<Person> deserializedPeople = JsonConvert.DeserializeObject<List<Person>>(json,
+ new JsonSerializerSettings { PreserveReferencesHandling = PreserveReferencesHandling.Objects });
+
+ Console.WriteLine(deserializedPeople.Count);
+ // 2
+
+ Person p1 = deserializedPeople[0];
+ Person p2 = deserializedPeople[1];
+
+ Console.WriteLine(p1.Name);
+ // James
+ Console.WriteLine(p2.Name);
+ // James
+
+ bool equal = Object.ReferenceEquals(p1, p2);
+ // true
+ }
+
}
}