Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit 2038f87

Browse files
committed
Add Cyclical Dependency example
1 parent 6f8cd44 commit 2038f87

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

tests/ServiceStack.Text.Tests/CyclicalDependencyTests.cs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,5 +176,45 @@ public void Can_limit_cyclical_dependencies()
176176
p.ToJson().Print();
177177
}
178178
}
179+
180+
class Node
181+
{
182+
public string Name { get; set; }
183+
184+
[IgnoreDataMember]
185+
public Node Parent { get; set; }
186+
187+
public List<Node> Children { get; set; }
188+
}
189+
190+
[Test]
191+
public void Ignore_Cyclical_dependencies()
192+
{
193+
JsConfig<Node>.OnDeserializedFn = (node) =>
194+
{
195+
node.Children.Each(child => child.Parent = node);
196+
return node;
197+
};
198+
199+
var parent = new Node
200+
{
201+
Name = "Parent",
202+
};
203+
parent.Children = new List<Node>
204+
{
205+
new Node { Name = "Child", Parent = parent },
206+
};
207+
208+
var json = parent.ToJson();
209+
Assert.That(json,
210+
Is.EqualTo("{\"Name\":\"Parent\",\"Children\":[{\"Name\":\"Child\"}]}"));
211+
212+
var fromJson = json.FromJson<Node>();
213+
214+
Assert.That(fromJson.Children[0].Parent, Is.EqualTo(fromJson));
215+
216+
JsConfig<Node>.OnDeserializedFn = null;
217+
JsConfig.Reset();
218+
}
179219
}
180220
}

0 commit comments

Comments
 (0)