1

Assume those hierachical object structure: TreeBranchLeaf

public class Tree
{
    public string Name { get; set; }
    public List<Branch> Nodes { get; set; } = new List<Branch>();
}
public class Branch
{
    public Branch(Tree parent) { Parent = parent; }
    public Tree Parent { get; }
    public string Name { get; set; }
    public List<Leaf> Nodes { get; set; } = new List<Leaf>();
}
public class Leaf
{
    public Leaf(Branch parent) { Parent = parent; }
    public Branch Parent { get; }
    public string Name { get; set; }
}

A not so simple serialization output would be (using ReferenceLoopHandling.Serialize AND PreserveReferencesHandling = PreserveReferencesHandling.Objects):

{
    "$id": "1",
    "Name": "AnOakTree",
    "Nodes": [
        {
            "$id": "2",
            "Parent": {
                "$ref": "1"
            },
            "Name": "TheLowestBranch",
            "Nodes": [
                {
                    "$id": "3",
                    "Parent": {
                        "$ref": "2"
                    },
                    "Name": "OneOfAMillionLeaf"
                }
            ]
        }
    ]
}

Deserialization works perfectly, nothing to complain! But I'd like to omit reference handling at all to reduce JSON footprint and improve readability.

What's the best way to deserialize JSON without reference informations and invoke the constructors with the correct parameter (the instance of the parent node)? So that the back-references will be recreated by code?

Marcel
  • 1,002
  • 2
  • 16
  • 37
  • Possible duplicate of [StackOverflowException is thrown when serialising circular dependent ISerializable object with ReferenceLoopHandling.Ignore](https://stackoverflow.com/questions/13412359/stackoverflowexception-is-thrown-when-serialising-circular-dependent-iserializab) – Sinatr May 18 '18 at 13:12
  • @Sinatr Thanks for your comment and yes: Using `ReferenceLoopHandling.Serialize` AND `PreserveReferencesHandling.Objects` does the trick. But is there a manual solution without inflating JSON? Somewhat like a custom instance creator depending on the position of the JSON.NET reading stream... – Marcel May 18 '18 at 13:36
  • 1
    In the future please avoid changing the question too much after you've got any response (especially answers). Simply ask a new question. The meta-data in json is [required](https://www.newtonsoft.com/json/help/html/PreserveObjectReferences.htm) for deserialization whenever it's a reference or type info. I don't know if you can avoid to have it easily. I am sure you don't want to write own deserializer and having `$id` and `$ref` is a little *pay* for what you get. You will need anyway some kind of identification to fill `Parent` property with corresponding reference. I doubt you can shorten it – Sinatr May 18 '18 at 13:55
  • Rather than a pure deserialization problem, you might think of this as an API design issue: how to ensure the parent/child relationships stay consistent? Looked at it from that perspective. [Adding the Parent id to Serialization as Object class](https://stackoverflow.com/q/37209142/3744182) might apply. – dbc May 19 '18 at 21:47

0 Answers0