diff options
author | JamesNK <james@newtonking.com> | 2010-12-01 11:03:02 +0300 |
---|---|---|
committer | JamesNK <james@newtonking.com> | 2010-12-01 11:03:02 +0300 |
commit | 227a34ed50cb7fd5e447088af9dce38c409fcda8 (patch) | |
tree | d24eaef65d07dd52de587cc72c5d167c97f1257b | |
parent | 2eb0d3bf48f1fc675de46ee94911a71ccd62c66f (diff) |
-Added support for deserializing to JToken
-Improved circular reference error message with type name
4 files changed, 28 insertions, 5 deletions
diff --git a/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs b/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs index c3283e0..d9026ce 100644 --- a/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs +++ b/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs @@ -1322,7 +1322,7 @@ keyword such as type of business."" }
[Test]
- [ExpectedException(typeof(JsonSerializationException), ExpectedMessage = @"Self referencing loop")]
+ [ExpectedException(typeof(JsonSerializationException))]
public void JsonPropertyWithHandlingValues_ReferenceLoopError()
{
JsonPropertyWithHandlingValues o = new JsonPropertyWithHandlingValues();
@@ -3959,5 +3959,28 @@ keyword such as type of business."" });
}
#endif
+
+ public class Response
+ {
+ public string Name { get; set; }
+ public JToken Data { get; set; }
+ }
+
+ [Test]
+ public void DeserializeJToken()
+ {
+ Response response = new Response
+ {
+ Name = "Success",
+ Data = new JObject(new JProperty("First", "Value1"), new JProperty("Second", "Value2"))
+ };
+
+ string json = JsonConvert.SerializeObject(response, Formatting.Indented);
+
+ Response deserializedResponse = JsonConvert.DeserializeObject<Response>(json);
+
+ Assert.AreEqual("Success", deserializedResponse.Name);
+ Assert.IsTrue(deserializedResponse.Data.DeepEquals(response.Data));
+ }
}
}
\ No newline at end of file diff --git a/Src/Newtonsoft.Json.Tests/Serialization/PreserveReferencesHandlingTests.cs b/Src/Newtonsoft.Json.Tests/Serialization/PreserveReferencesHandlingTests.cs index c216ed6..0d5b78c 100644 --- a/Src/Newtonsoft.Json.Tests/Serialization/PreserveReferencesHandlingTests.cs +++ b/Src/Newtonsoft.Json.Tests/Serialization/PreserveReferencesHandlingTests.cs @@ -87,7 +87,7 @@ namespace Newtonsoft.Json.Tests.Serialization }
[Test]
- [ExpectedException(typeof(JsonSerializationException), ExpectedMessage = "Self referencing loop")]
+ [ExpectedException(typeof(JsonSerializationException))]
public void SerializeCircularListsError()
{
CircularList circularList = new CircularList();
@@ -239,7 +239,7 @@ namespace Newtonsoft.Json.Tests.Serialization }
[Test]
- [ExpectedException(typeof(JsonSerializationException), ExpectedMessage = "Self referencing loop")]
+ [ExpectedException(typeof(JsonSerializationException))]
public void SerializeCircularDictionarysError()
{
CircularDictionary circularDictionary = new CircularDictionary();
diff --git a/Src/Newtonsoft.Json/Serialization/DefaultContractResolver.cs b/Src/Newtonsoft.Json/Serialization/DefaultContractResolver.cs index 2632593..f8eccf8 100644 --- a/Src/Newtonsoft.Json/Serialization/DefaultContractResolver.cs +++ b/Src/Newtonsoft.Json/Serialization/DefaultContractResolver.cs @@ -498,7 +498,7 @@ namespace Newtonsoft.Json.Serialization if (JsonTypeReflector.GetJsonArrayAttribute(t) != null)
return CreateArrayContract(t);
- if (t.IsSubclassOf(typeof(JToken)))
+ if (t == typeof(JToken) || t.IsSubclassOf(typeof(JToken)))
return CreateLinqContract(t);
if (CollectionUtils.IsDictionaryType(t))
diff --git a/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalWriter.cs b/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalWriter.cs index 5b0e75d..c9ee699 100644 --- a/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalWriter.cs +++ b/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalWriter.cs @@ -214,7 +214,7 @@ namespace Newtonsoft.Json.Serialization switch (referenceLoopHandling.GetValueOrDefault(Serializer.ReferenceLoopHandling))
{
case ReferenceLoopHandling.Error:
- throw new JsonSerializationException("Self referencing loop");
+ throw new JsonSerializationException("Self referencing loop detected for type '{0}'.".FormatWith(CultureInfo.InvariantCulture, value.GetType()));
case ReferenceLoopHandling.Ignore:
return false;
case ReferenceLoopHandling.Serialize:
|