/
GuidReferenceResolverExample.cs
100 lines (84 loc) · 2.89 KB
/
GuidReferenceResolverExample.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace GuidReferenceResolverExample
{
public class Person
{
internal Guid Id { get; set; }
public string? Name { get; set; }
public Person? Spouse { get; set; }
}
public class GuidReferenceResolver : ReferenceResolver
{
private readonly IDictionary<Guid, Person> _people = new Dictionary<Guid, Person>();
public override object ResolveReference(string referenceId)
{
Guid id = new(referenceId);
_people.TryGetValue(id, out Person? p);
return p!;
}
public override string GetReference(object? value, out bool alreadyExists)
{
Person person = (Person)value!;
if (!(alreadyExists = _people.ContainsKey(person.Id)))
{
_people[person.Id] = person;
}
return person.Id.ToString()!;
}
public override void AddReference(string reference, object value)
{
Guid id = new(reference);
Person person = (Person)value;
person.Id = id;
_people[id] = person;
}
}
static class Program
{
public static void Main()
{
Person tyler = new() { Id = Guid.NewGuid(), Name = "Tyler" };
Person adrian = new() { Id = Guid.NewGuid(), Name = "Adrian" };
tyler.Spouse = adrian;
adrian.Spouse = tyler;
var people = ImmutableArray.Create(tyler, adrian);
var options = new JsonSerializerOptions
{
WriteIndented = true,
ReferenceHandler = new ReferenceHandler<GuidReferenceResolver>()
};
string json = JsonSerializer.Serialize(people, options);
Console.WriteLine($"Output JSON {json}");
List<Person> peopleDeserialized =
JsonSerializer.Deserialize<List<Person>>(json, options)!;
Person tylerDeserialized = peopleDeserialized[0];
Person adrianDeserialized = peopleDeserialized[1];
Console.WriteLine($"Adrian is Tyler's spouse: {tylerDeserialized.Equals(adrianDeserialized.Spouse)}");
Console.WriteLine($"Tyler is Adrian's spouse: {adrianDeserialized.Equals(tylerDeserialized.Spouse)}");
}
}
}
// Produces output like the following example:
//
//Output JSON[
// {
// "$id": "79301726-9d94-499a-8cdc-0c8bcc4c9b63",
// "Name": "Tyler",
// "Spouse": {
// "$id": "94833059-35f2-4fdd-96ee-94fd0484969a",
// "Name": "Adrian",
// "Spouse": {
// "$ref": "79301726-9d94-499a-8cdc-0c8bcc4c9b63"
// }
// }
// },
// {
// "$ref": "94833059-35f2-4fdd-96ee-94fd0484969a"
// }
//]
//Adrian is Tyler's spouse: True
//Tyler is Adrian's spouse: True