Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

De-dupe serialized objects, properly handle references #14

Open
grahamboree opened this issue Jul 17, 2022 · 0 comments
Open

De-dupe serialized objects, properly handle references #14

grahamboree opened this issue Jul 17, 2022 · 0 comments
Assignees
Labels
feature A new feature
Milestone

Comments

@grahamboree
Copy link
Owner

Currently circular references will cause infinite loops in serialization and de-serialization.

This self-referential tree structure...

class Test {
    public Test Left;
    public Test Right;
}

Test Root;
Root.Left = new Test();
Root.Right = Root;

...should be serialized as:

{
    "$id": 1,
    "Left": {
        "Left": null,
        "Right": null
    },
    "Right": {
        "$ref": 1
    }
}

$id gives a specific object in the json heirarchy an ID. These IDs should be unique per-document, but don't need to be a GUID. Smaller is better.

$ref indicates that the object containing the $ref key should be replaced by the object who's $id value is 1.

Additionally, this should be used to de-dupe objects that are serialized multiple times.

For example, the following...

class Test {
    public int Value;
    public double Amount;
    public string Name;
}

var data = new List<Test>();
var testData = new Test {
    Value = 42,
    Amount = 10.5,
    Name = "test name"
};
data.Add(testData);
data.Add(testData);
data.Add(testData);

...should be serialized as

[
    {
        "$id": 1,
        "Value": 42,
        "Amount": 10.5,
        "Name": "test name"
    },
    {
        "$ref": 1
    },
    {
        "$ref": 1
    }
]
@grahamboree grahamboree added the feature A new feature label Jul 17, 2022
@grahamboree grahamboree added this to the v1.0 milestone Jul 17, 2022
@grahamboree grahamboree self-assigned this Jul 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature A new feature
Projects
None yet
Development

No branches or pull requests

1 participant