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
path: root/Src
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
parent2e1d0c54ce152cd92521025d7f3e61d04f76bc12 (diff)
-Added support for deserializing to readonly collections and dictionaries on classes with non-default constructors
Diffstat (limited to 'Src')
-rw-r--r--Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs70
-rw-r--r--Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs40
2 files changed, 110 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
diff --git a/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs b/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs
index dc3cc50..c824ed3 100644
--- a/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs
+++ b/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs
@@ -915,7 +915,47 @@ namespace Newtonsoft.Json.Serialization
object value = remainingPropertyValue.Value;
if (ShouldSetPropertyValue(remainingPropertyValue.Key, remainingPropertyValue.Value))
+ {
property.ValueProvider.SetValue(createdObject, value);
+ }
+ else if (!property.Writable && value != null)
+ {
+ // handle readonly collection/dictionary properties
+ JsonContract propertyContract = Serializer.ContractResolver.ResolveContract(property.PropertyType);
+
+ if (propertyContract is JsonArrayContract)
+ {
+ JsonArrayContract propertyArrayContract = propertyContract as JsonArrayContract;
+
+ object createdObjectCollection = property.ValueProvider.GetValue(createdObject);
+ if (createdObjectCollection != null)
+ {
+ IWrappedCollection createdObjectCollectionWrapper = propertyArrayContract.CreateWrapper(createdObjectCollection);
+ IWrappedCollection newValues = propertyArrayContract.CreateWrapper(value);
+
+ foreach (object newValue in newValues)
+ {
+ createdObjectCollectionWrapper.Add(newValue);
+ }
+ }
+ }
+ else if (propertyContract is JsonDictionaryContract)
+ {
+ JsonDictionaryContract jsonDictionaryContract = propertyContract as JsonDictionaryContract;
+
+ object createdObjectDictionary = property.ValueProvider.GetValue(createdObject);
+ if (createdObjectDictionary != null)
+ {
+ IWrappedDictionary createdObjectDictionaryWrapper = jsonDictionaryContract.CreateWrapper(createdObjectDictionary);
+ IWrappedDictionary newValues = jsonDictionaryContract.CreateWrapper(value);
+
+ foreach (DictionaryEntry newValue in newValues)
+ {
+ createdObjectDictionaryWrapper.Add(newValue.Key, newValue.Value);
+ }
+ }
+ }
+ }
}
contract.InvokeOnDeserialized(createdObject, Serializer.Context);