-Code reformat
This commit is contained in:
@@ -40,45 +40,45 @@ using Newtonsoft.Json.Tests.Linq;
|
||||
|
||||
namespace Newtonsoft.Json.Tests.Serialization
|
||||
{
|
||||
[TestFixture]
|
||||
public class ReferenceLoopHandlingTests : TestFixtureBase
|
||||
{
|
||||
[Test]
|
||||
public void ReferenceLoopHandlingTest()
|
||||
[TestFixture]
|
||||
public class ReferenceLoopHandlingTests : TestFixtureBase
|
||||
{
|
||||
JsonPropertyAttribute attribute = new JsonPropertyAttribute();
|
||||
Assert.AreEqual(null, attribute._defaultValueHandling);
|
||||
Assert.AreEqual(ReferenceLoopHandling.Error, attribute.ReferenceLoopHandling);
|
||||
|
||||
attribute.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
|
||||
Assert.AreEqual(ReferenceLoopHandling.Ignore, attribute._referenceLoopHandling);
|
||||
Assert.AreEqual(ReferenceLoopHandling.Ignore, attribute.ReferenceLoopHandling);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void IgnoreObjectReferenceLoop()
|
||||
{
|
||||
ReferenceLoopHandlingObjectContainerAttribute o = new ReferenceLoopHandlingObjectContainerAttribute();
|
||||
o.Value = o;
|
||||
|
||||
string json = JsonConvert.SerializeObject(o, Formatting.Indented, new JsonSerializerSettings
|
||||
[Test]
|
||||
public void ReferenceLoopHandlingTest()
|
||||
{
|
||||
ReferenceLoopHandling = ReferenceLoopHandling.Serialize
|
||||
});
|
||||
Assert.AreEqual("{}", json);
|
||||
}
|
||||
JsonPropertyAttribute attribute = new JsonPropertyAttribute();
|
||||
Assert.AreEqual(null, attribute._defaultValueHandling);
|
||||
Assert.AreEqual(ReferenceLoopHandling.Error, attribute.ReferenceLoopHandling);
|
||||
|
||||
[Test]
|
||||
public void IgnoreObjectReferenceLoopWithPropertyOverride()
|
||||
{
|
||||
ReferenceLoopHandlingObjectContainerAttributeWithPropertyOverride o = new ReferenceLoopHandlingObjectContainerAttributeWithPropertyOverride();
|
||||
o.Value = o;
|
||||
attribute.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
|
||||
Assert.AreEqual(ReferenceLoopHandling.Ignore, attribute._referenceLoopHandling);
|
||||
Assert.AreEqual(ReferenceLoopHandling.Ignore, attribute.ReferenceLoopHandling);
|
||||
}
|
||||
|
||||
string json = JsonConvert.SerializeObject(o, Formatting.Indented, new JsonSerializerSettings
|
||||
{
|
||||
ReferenceLoopHandling = ReferenceLoopHandling.Serialize
|
||||
});
|
||||
Assert.AreEqual(@"{
|
||||
[Test]
|
||||
public void IgnoreObjectReferenceLoop()
|
||||
{
|
||||
ReferenceLoopHandlingObjectContainerAttribute o = new ReferenceLoopHandlingObjectContainerAttribute();
|
||||
o.Value = o;
|
||||
|
||||
string json = JsonConvert.SerializeObject(o, Formatting.Indented, new JsonSerializerSettings
|
||||
{
|
||||
ReferenceLoopHandling = ReferenceLoopHandling.Serialize
|
||||
});
|
||||
Assert.AreEqual("{}", json);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void IgnoreObjectReferenceLoopWithPropertyOverride()
|
||||
{
|
||||
ReferenceLoopHandlingObjectContainerAttributeWithPropertyOverride o = new ReferenceLoopHandlingObjectContainerAttributeWithPropertyOverride();
|
||||
o.Value = o;
|
||||
|
||||
string json = JsonConvert.SerializeObject(o, Formatting.Indented, new JsonSerializerSettings
|
||||
{
|
||||
ReferenceLoopHandling = ReferenceLoopHandling.Serialize
|
||||
});
|
||||
Assert.AreEqual(@"{
|
||||
""Value"": {
|
||||
""Value"": {
|
||||
""Value"": {
|
||||
@@ -91,44 +91,44 @@ namespace Newtonsoft.Json.Tests.Serialization
|
||||
}
|
||||
}
|
||||
}", json);
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void IgnoreArrayReferenceLoop()
|
||||
{
|
||||
ReferenceLoopHandlingList a = new ReferenceLoopHandlingList();
|
||||
a.Add(a);
|
||||
[Test]
|
||||
public void IgnoreArrayReferenceLoop()
|
||||
{
|
||||
ReferenceLoopHandlingList a = new ReferenceLoopHandlingList();
|
||||
a.Add(a);
|
||||
|
||||
string json = JsonConvert.SerializeObject(a, Formatting.Indented, new JsonSerializerSettings
|
||||
{
|
||||
ReferenceLoopHandling = ReferenceLoopHandling.Serialize
|
||||
});
|
||||
Assert.AreEqual("[]", json);
|
||||
}
|
||||
string json = JsonConvert.SerializeObject(a, Formatting.Indented, new JsonSerializerSettings
|
||||
{
|
||||
ReferenceLoopHandling = ReferenceLoopHandling.Serialize
|
||||
});
|
||||
Assert.AreEqual("[]", json);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void IgnoreDictionaryReferenceLoop()
|
||||
{
|
||||
ReferenceLoopHandlingDictionary d = new ReferenceLoopHandlingDictionary();
|
||||
d.Add("First", d);
|
||||
[Test]
|
||||
public void IgnoreDictionaryReferenceLoop()
|
||||
{
|
||||
ReferenceLoopHandlingDictionary d = new ReferenceLoopHandlingDictionary();
|
||||
d.Add("First", d);
|
||||
|
||||
string json = JsonConvert.SerializeObject(d, Formatting.Indented, new JsonSerializerSettings
|
||||
{
|
||||
ReferenceLoopHandling = ReferenceLoopHandling.Serialize
|
||||
});
|
||||
Assert.AreEqual("{}", json);
|
||||
}
|
||||
string json = JsonConvert.SerializeObject(d, Formatting.Indented, new JsonSerializerSettings
|
||||
{
|
||||
ReferenceLoopHandling = ReferenceLoopHandling.Serialize
|
||||
});
|
||||
Assert.AreEqual("{}", json);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void SerializePropertyItemReferenceLoopHandling()
|
||||
{
|
||||
PropertyItemReferenceLoopHandling c = new PropertyItemReferenceLoopHandling();
|
||||
c.Text = "Text!";
|
||||
c.SetData(new List<PropertyItemReferenceLoopHandling> { c });
|
||||
[Test]
|
||||
public void SerializePropertyItemReferenceLoopHandling()
|
||||
{
|
||||
PropertyItemReferenceLoopHandling c = new PropertyItemReferenceLoopHandling();
|
||||
c.Text = "Text!";
|
||||
c.SetData(new List<PropertyItemReferenceLoopHandling> { c });
|
||||
|
||||
string json = JsonConvert.SerializeObject(c, Formatting.Indented);
|
||||
string json = JsonConvert.SerializeObject(c, Formatting.Indented);
|
||||
|
||||
Assert.AreEqual(@"{
|
||||
Assert.AreEqual(@"{
|
||||
""Text"": ""Text!"",
|
||||
""Data"": [
|
||||
{
|
||||
@@ -147,190 +147,190 @@ namespace Newtonsoft.Json.Tests.Serialization
|
||||
}
|
||||
]
|
||||
}", json);
|
||||
}
|
||||
}
|
||||
|
||||
#if !(PORTABLE || SILVERLIGHT || NETFX_CORE || PORTABLE40)
|
||||
public class MainClass : ISerializable
|
||||
{
|
||||
public ChildClass Child { get; set; }
|
||||
public class MainClass : ISerializable
|
||||
{
|
||||
public ChildClass Child { get; set; }
|
||||
|
||||
public void GetObjectData(SerializationInfo info, StreamingContext context)
|
||||
{
|
||||
info.AddValue("Child", Child);
|
||||
}
|
||||
}
|
||||
public void GetObjectData(SerializationInfo info, StreamingContext context)
|
||||
{
|
||||
info.AddValue("Child", Child);
|
||||
}
|
||||
}
|
||||
|
||||
public class ChildClass : ISerializable
|
||||
{
|
||||
public string Name { get; set; }
|
||||
public MainClass Parent { get; set; }
|
||||
public class ChildClass : ISerializable
|
||||
{
|
||||
public string Name { get; set; }
|
||||
public MainClass Parent { get; set; }
|
||||
|
||||
public void GetObjectData(SerializationInfo info, StreamingContext context)
|
||||
{
|
||||
info.AddValue("Parent", Parent);
|
||||
info.AddValue("Name", Name);
|
||||
}
|
||||
}
|
||||
public void GetObjectData(SerializationInfo info, StreamingContext context)
|
||||
{
|
||||
info.AddValue("Parent", Parent);
|
||||
info.AddValue("Name", Name);
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ErrorISerializableCyclicReferenceLoop()
|
||||
{
|
||||
var main = new MainClass();
|
||||
var child = new ChildClass();
|
||||
[Test]
|
||||
public void ErrorISerializableCyclicReferenceLoop()
|
||||
{
|
||||
var main = new MainClass();
|
||||
var child = new ChildClass();
|
||||
|
||||
child.Name = "Child1";
|
||||
child.Parent = main; // Obvious Circular Reference
|
||||
child.Name = "Child1";
|
||||
child.Parent = main; // Obvious Circular Reference
|
||||
|
||||
main.Child = child;
|
||||
main.Child = child;
|
||||
|
||||
var settings =
|
||||
new JsonSerializerSettings();
|
||||
var settings =
|
||||
new JsonSerializerSettings();
|
||||
|
||||
ExceptionAssert.Throws<JsonSerializationException>(
|
||||
"Self referencing loop detected with type 'Newtonsoft.Json.Tests.Serialization.ReferenceLoopHandlingTests+MainClass'. Path 'Child'.",
|
||||
() => JsonConvert.SerializeObject(main, settings));
|
||||
}
|
||||
ExceptionAssert.Throws<JsonSerializationException>(
|
||||
"Self referencing loop detected with type 'Newtonsoft.Json.Tests.Serialization.ReferenceLoopHandlingTests+MainClass'. Path 'Child'.",
|
||||
() => JsonConvert.SerializeObject(main, settings));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void IgnoreISerializableCyclicReferenceLoop()
|
||||
{
|
||||
var main = new MainClass();
|
||||
var child = new ChildClass();
|
||||
[Test]
|
||||
public void IgnoreISerializableCyclicReferenceLoop()
|
||||
{
|
||||
var main = new MainClass();
|
||||
var child = new ChildClass();
|
||||
|
||||
child.Name = "Child1";
|
||||
child.Parent = main; // Obvious Circular Reference
|
||||
child.Name = "Child1";
|
||||
child.Parent = main; // Obvious Circular Reference
|
||||
|
||||
main.Child = child;
|
||||
main.Child = child;
|
||||
|
||||
var settings =
|
||||
new JsonSerializerSettings() { ReferenceLoopHandling = ReferenceLoopHandling.Ignore };
|
||||
var settings =
|
||||
new JsonSerializerSettings() { ReferenceLoopHandling = ReferenceLoopHandling.Ignore };
|
||||
|
||||
var c = JsonConvert.SerializeObject(main, settings);
|
||||
Assert.AreEqual(@"{""Child"":{""Name"":""Child1""}}", c);
|
||||
}
|
||||
var c = JsonConvert.SerializeObject(main, settings);
|
||||
Assert.AreEqual(@"{""Child"":{""Name"":""Child1""}}", c);
|
||||
}
|
||||
#endif
|
||||
|
||||
#if !(NET20 || NET35 || PORTABLE40)
|
||||
public class DictionaryDynamicObject : DynamicObject
|
||||
{
|
||||
public IDictionary<string, object> Values { get; private set; }
|
||||
|
||||
public DictionaryDynamicObject()
|
||||
{
|
||||
Values = new Dictionary<string, object>();
|
||||
}
|
||||
|
||||
public override bool TrySetMember(SetMemberBinder binder, object value)
|
||||
{
|
||||
Values[binder.Name] = value;
|
||||
return true;
|
||||
}
|
||||
|
||||
public override bool TryGetMember(GetMemberBinder binder, out object result)
|
||||
{
|
||||
return Values.TryGetValue(binder.Name, out result);
|
||||
}
|
||||
|
||||
public override IEnumerable<string> GetDynamicMemberNames()
|
||||
{
|
||||
return Values.Keys;
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ErrorDynamicCyclicReferenceLoop()
|
||||
{
|
||||
dynamic parent = new DictionaryDynamicObject();
|
||||
dynamic child = new DictionaryDynamicObject();
|
||||
parent.child = child;
|
||||
child.parent = parent;
|
||||
|
||||
var settings = new JsonSerializerSettings();
|
||||
|
||||
ExceptionAssert.Throws<JsonSerializationException>(
|
||||
"Self referencing loop detected with type 'Newtonsoft.Json.Tests.Serialization.ReferenceLoopHandlingTests+DictionaryDynamicObject'. Path 'child'.",
|
||||
() => JsonConvert.SerializeObject(parent, settings));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void IgnoreDynamicCyclicReferenceLoop()
|
||||
{
|
||||
dynamic parent = new DictionaryDynamicObject();
|
||||
dynamic child = new DictionaryDynamicObject();
|
||||
parent.child = child;
|
||||
parent.name = "parent";
|
||||
child.parent = parent;
|
||||
child.name = "child";
|
||||
|
||||
var settings = new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore };
|
||||
|
||||
var c = JsonConvert.SerializeObject(parent, settings);
|
||||
Assert.AreEqual(@"{""child"":{""name"":""child""},""name"":""parent""}", c);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
public class PropertyItemReferenceLoopHandling
|
||||
{
|
||||
private IList<PropertyItemReferenceLoopHandling> _data;
|
||||
private int _accessCount;
|
||||
|
||||
public string Text { get; set; }
|
||||
|
||||
[JsonProperty(ItemReferenceLoopHandling = ReferenceLoopHandling.Serialize)]
|
||||
public IList<PropertyItemReferenceLoopHandling> Data
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_accessCount >= 3)
|
||||
return null;
|
||||
|
||||
_accessCount++;
|
||||
return new List<PropertyItemReferenceLoopHandling>(_data);
|
||||
}
|
||||
}
|
||||
|
||||
public void SetData(IList<PropertyItemReferenceLoopHandling> data)
|
||||
{
|
||||
_data = data;
|
||||
}
|
||||
}
|
||||
|
||||
[JsonArray(ItemReferenceLoopHandling = ReferenceLoopHandling.Ignore)]
|
||||
public class ReferenceLoopHandlingList : List<ReferenceLoopHandlingList>
|
||||
{
|
||||
}
|
||||
|
||||
[JsonDictionary(ItemReferenceLoopHandling = ReferenceLoopHandling.Ignore)]
|
||||
public class ReferenceLoopHandlingDictionary : Dictionary<string, ReferenceLoopHandlingDictionary>
|
||||
{
|
||||
}
|
||||
|
||||
[JsonObject(ItemReferenceLoopHandling = ReferenceLoopHandling.Ignore)]
|
||||
public class ReferenceLoopHandlingObjectContainerAttribute
|
||||
{
|
||||
public ReferenceLoopHandlingObjectContainerAttribute Value { get; set; }
|
||||
}
|
||||
|
||||
[JsonObject(ItemReferenceLoopHandling = ReferenceLoopHandling.Ignore)]
|
||||
public class ReferenceLoopHandlingObjectContainerAttributeWithPropertyOverride
|
||||
{
|
||||
private ReferenceLoopHandlingObjectContainerAttributeWithPropertyOverride _value;
|
||||
private int _getCount;
|
||||
|
||||
[JsonProperty(ReferenceLoopHandling = ReferenceLoopHandling.Serialize)]
|
||||
public ReferenceLoopHandlingObjectContainerAttributeWithPropertyOverride Value
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_getCount < 5)
|
||||
public class DictionaryDynamicObject : DynamicObject
|
||||
{
|
||||
_getCount++;
|
||||
return _value;
|
||||
public IDictionary<string, object> Values { get; private set; }
|
||||
|
||||
public DictionaryDynamicObject()
|
||||
{
|
||||
Values = new Dictionary<string, object>();
|
||||
}
|
||||
|
||||
public override bool TrySetMember(SetMemberBinder binder, object value)
|
||||
{
|
||||
Values[binder.Name] = value;
|
||||
return true;
|
||||
}
|
||||
|
||||
public override bool TryGetMember(GetMemberBinder binder, out object result)
|
||||
{
|
||||
return Values.TryGetValue(binder.Name, out result);
|
||||
}
|
||||
|
||||
public override IEnumerable<string> GetDynamicMemberNames()
|
||||
{
|
||||
return Values.Keys;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
set { _value = value; }
|
||||
|
||||
[Test]
|
||||
public void ErrorDynamicCyclicReferenceLoop()
|
||||
{
|
||||
dynamic parent = new DictionaryDynamicObject();
|
||||
dynamic child = new DictionaryDynamicObject();
|
||||
parent.child = child;
|
||||
child.parent = parent;
|
||||
|
||||
var settings = new JsonSerializerSettings();
|
||||
|
||||
ExceptionAssert.Throws<JsonSerializationException>(
|
||||
"Self referencing loop detected with type 'Newtonsoft.Json.Tests.Serialization.ReferenceLoopHandlingTests+DictionaryDynamicObject'. Path 'child'.",
|
||||
() => JsonConvert.SerializeObject(parent, settings));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void IgnoreDynamicCyclicReferenceLoop()
|
||||
{
|
||||
dynamic parent = new DictionaryDynamicObject();
|
||||
dynamic child = new DictionaryDynamicObject();
|
||||
parent.child = child;
|
||||
parent.name = "parent";
|
||||
child.parent = parent;
|
||||
child.name = "child";
|
||||
|
||||
var settings = new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore };
|
||||
|
||||
var c = JsonConvert.SerializeObject(parent, settings);
|
||||
Assert.AreEqual(@"{""child"":{""name"":""child""},""name"":""parent""}", c);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class PropertyItemReferenceLoopHandling
|
||||
{
|
||||
private IList<PropertyItemReferenceLoopHandling> _data;
|
||||
private int _accessCount;
|
||||
|
||||
public string Text { get; set; }
|
||||
|
||||
[JsonProperty(ItemReferenceLoopHandling = ReferenceLoopHandling.Serialize)]
|
||||
public IList<PropertyItemReferenceLoopHandling> Data
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_accessCount >= 3)
|
||||
return null;
|
||||
|
||||
_accessCount++;
|
||||
return new List<PropertyItemReferenceLoopHandling>(_data);
|
||||
}
|
||||
}
|
||||
|
||||
public void SetData(IList<PropertyItemReferenceLoopHandling> data)
|
||||
{
|
||||
_data = data;
|
||||
}
|
||||
}
|
||||
|
||||
[JsonArray(ItemReferenceLoopHandling = ReferenceLoopHandling.Ignore)]
|
||||
public class ReferenceLoopHandlingList : List<ReferenceLoopHandlingList>
|
||||
{
|
||||
}
|
||||
|
||||
[JsonDictionary(ItemReferenceLoopHandling = ReferenceLoopHandling.Ignore)]
|
||||
public class ReferenceLoopHandlingDictionary : Dictionary<string, ReferenceLoopHandlingDictionary>
|
||||
{
|
||||
}
|
||||
|
||||
[JsonObject(ItemReferenceLoopHandling = ReferenceLoopHandling.Ignore)]
|
||||
public class ReferenceLoopHandlingObjectContainerAttribute
|
||||
{
|
||||
public ReferenceLoopHandlingObjectContainerAttribute Value { get; set; }
|
||||
}
|
||||
|
||||
[JsonObject(ItemReferenceLoopHandling = ReferenceLoopHandling.Ignore)]
|
||||
public class ReferenceLoopHandlingObjectContainerAttributeWithPropertyOverride
|
||||
{
|
||||
private ReferenceLoopHandlingObjectContainerAttributeWithPropertyOverride _value;
|
||||
private int _getCount;
|
||||
|
||||
[JsonProperty(ReferenceLoopHandling = ReferenceLoopHandling.Serialize)]
|
||||
public ReferenceLoopHandlingObjectContainerAttributeWithPropertyOverride Value
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_getCount < 5)
|
||||
{
|
||||
_getCount++;
|
||||
return _value;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
set { _value = value; }
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user