-Added support for deserializing to JToken
-Improved circular reference error message with type name
This commit is contained in:
@@ -1322,7 +1322,7 @@ keyword such as type of business.""
|
|||||||
}
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
[ExpectedException(typeof(JsonSerializationException), ExpectedMessage = @"Self referencing loop")]
|
[ExpectedException(typeof(JsonSerializationException))]
|
||||||
public void JsonPropertyWithHandlingValues_ReferenceLoopError()
|
public void JsonPropertyWithHandlingValues_ReferenceLoopError()
|
||||||
{
|
{
|
||||||
JsonPropertyWithHandlingValues o = new JsonPropertyWithHandlingValues();
|
JsonPropertyWithHandlingValues o = new JsonPropertyWithHandlingValues();
|
||||||
@@ -3959,5 +3959,28 @@ keyword such as type of business.""
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
#endif
|
#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));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -87,7 +87,7 @@ namespace Newtonsoft.Json.Tests.Serialization
|
|||||||
}
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
[ExpectedException(typeof(JsonSerializationException), ExpectedMessage = "Self referencing loop")]
|
[ExpectedException(typeof(JsonSerializationException))]
|
||||||
public void SerializeCircularListsError()
|
public void SerializeCircularListsError()
|
||||||
{
|
{
|
||||||
CircularList circularList = new CircularList();
|
CircularList circularList = new CircularList();
|
||||||
@@ -239,7 +239,7 @@ namespace Newtonsoft.Json.Tests.Serialization
|
|||||||
}
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
[ExpectedException(typeof(JsonSerializationException), ExpectedMessage = "Self referencing loop")]
|
[ExpectedException(typeof(JsonSerializationException))]
|
||||||
public void SerializeCircularDictionarysError()
|
public void SerializeCircularDictionarysError()
|
||||||
{
|
{
|
||||||
CircularDictionary circularDictionary = new CircularDictionary();
|
CircularDictionary circularDictionary = new CircularDictionary();
|
||||||
|
|||||||
@@ -498,7 +498,7 @@ namespace Newtonsoft.Json.Serialization
|
|||||||
if (JsonTypeReflector.GetJsonArrayAttribute(t) != null)
|
if (JsonTypeReflector.GetJsonArrayAttribute(t) != null)
|
||||||
return CreateArrayContract(t);
|
return CreateArrayContract(t);
|
||||||
|
|
||||||
if (t.IsSubclassOf(typeof(JToken)))
|
if (t == typeof(JToken) || t.IsSubclassOf(typeof(JToken)))
|
||||||
return CreateLinqContract(t);
|
return CreateLinqContract(t);
|
||||||
|
|
||||||
if (CollectionUtils.IsDictionaryType(t))
|
if (CollectionUtils.IsDictionaryType(t))
|
||||||
|
|||||||
@@ -214,7 +214,7 @@ namespace Newtonsoft.Json.Serialization
|
|||||||
switch (referenceLoopHandling.GetValueOrDefault(Serializer.ReferenceLoopHandling))
|
switch (referenceLoopHandling.GetValueOrDefault(Serializer.ReferenceLoopHandling))
|
||||||
{
|
{
|
||||||
case ReferenceLoopHandling.Error:
|
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:
|
case ReferenceLoopHandling.Ignore:
|
||||||
return false;
|
return false;
|
||||||
case ReferenceLoopHandling.Serialize:
|
case ReferenceLoopHandling.Serialize:
|
||||||
|
|||||||
Reference in New Issue
Block a user