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>2011-07-04 11:55:06 +0400
committerJamesNK <james@newtonking.com>2011-07-04 11:55:06 +0400
commitb607b40753984bee86ac87a941eafe18535d0bbe (patch)
tree021589e4e25b50a552a45bfed498ab8f1aa4ed01 /Src/Newtonsoft.Json.Tests
parent2e1d0c54ce152cd92521025d7f3e61d04f76bc12 (diff)
-Added support for deserializing to readonly collections and dictionaries on classes with non-default constructors
Diffstat (limited to 'Src/Newtonsoft.Json.Tests')
-rw-r--r--Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs70
1 files changed, 70 insertions, 0 deletions
diff --git a/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs b/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs
index 821e335..83b6e44 100644
--- a/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs
+++ b/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs
@@ -4441,5 +4441,75 @@ keyword such as type of business.""
""newMemberWithProperty"": null
}", result);
}
+
+ public class NonDefaultConstructorWithReadOnlyCollectionProperty
+ {
+ public string Title { get; set; }
+ public IList<string> Categories { get; private set; }
+
+ public NonDefaultConstructorWithReadOnlyCollectionProperty(string title)
+ {
+ Title = title;
+ Categories = new List<string>();
+ }
+ }
+
+ [Test]
+ public void NonDefaultConstructorWithReadOnlyCollectionPropertyTest()
+ {
+ NonDefaultConstructorWithReadOnlyCollectionProperty c1 = new NonDefaultConstructorWithReadOnlyCollectionProperty("blah");
+ c1.Categories.Add("one");
+ c1.Categories.Add("two");
+
+ string json = JsonConvert.SerializeObject(c1, Formatting.Indented);
+ Assert.AreEqual(@"{
+ ""Title"": ""blah"",
+ ""Categories"": [
+ ""one"",
+ ""two""
+ ]
+}", json);
+
+ NonDefaultConstructorWithReadOnlyCollectionProperty c2 = JsonConvert.DeserializeObject<NonDefaultConstructorWithReadOnlyCollectionProperty>(json);
+ Assert.AreEqual(c1.Title, c2.Title);
+ Assert.AreEqual(c1.Categories.Count, c2.Categories.Count);
+ Assert.AreEqual("one", c2.Categories[0]);
+ Assert.AreEqual("two", c2.Categories[1]);
+ }
+
+ public class NonDefaultConstructorWithReadOnlyDictionaryProperty
+ {
+ public string Title { get; set; }
+ public IDictionary<string, int> Categories { get; private set; }
+
+ public NonDefaultConstructorWithReadOnlyDictionaryProperty(string title)
+ {
+ Title = title;
+ Categories = new Dictionary<string, int>();
+ }
+ }
+
+ [Test]
+ public void NonDefaultConstructorWithReadOnlyDictionaryPropertyTest()
+ {
+ NonDefaultConstructorWithReadOnlyDictionaryProperty c1 = new NonDefaultConstructorWithReadOnlyDictionaryProperty("blah");
+ c1.Categories.Add("one", 1);
+ c1.Categories.Add("two", 2);
+
+ string json = JsonConvert.SerializeObject(c1, Formatting.Indented);
+ Assert.AreEqual(@"{
+ ""Title"": ""blah"",
+ ""Categories"": {
+ ""one"": 1,
+ ""two"": 2
+ }
+}", json);
+
+ NonDefaultConstructorWithReadOnlyDictionaryProperty c2 = JsonConvert.DeserializeObject<NonDefaultConstructorWithReadOnlyDictionaryProperty>(json);
+ Assert.AreEqual(c1.Title, c2.Title);
+ Assert.AreEqual(c1.Categories.Count, c2.Categories.Count);
+ Assert.AreEqual(1, c2.Categories["one"]);
+ Assert.AreEqual(2, c2.Categories["two"]);
+ }
}
} \ No newline at end of file