I am trying to simulate a foreign key behaviour using JSON.Net but I can't seem to find a way to have the real references.
Let's say I have this simple example:
private static void Main(string[] args)
{
var g1 = new Group {Name = "g1"};
var g2 = new Group {Name = "g2"};
var users = new[]
{
new User{ Username = "truc", Group = g1 },
new User{ Username = "machin", Group = g2 },
new User{ Username = "dom", Group = g2 },
new User{ Username = "boum", Group = g2 }
};
string json = JsonConvert.SerializeObject(users);
var jsonUsers = JsonConvert.DeserializeObject<User[]>(json);
Console.WriteLine(jsonUsers[2].Group == jsonUsers[3].Group);
Console.ReadLine();
}
the problem here is that Console.WriteLine(jsonUsers[2].Group == jsonUsers[3].Group); is always false.
The only way I found that would make it work would be to store a list of users, then a list of group and having a GroupId proprety Inside of users. Then after everything is deserialized, I manually insert references of groups Inside of users. It's hackish.
What would be the best way to approach this problem ?