0

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 ?

2
  • Your suggestion of using identifiers to link models is also a valid approach along with the answer provided by I4V, especially if serialized content is to be propagated to another interface, say as a result of a RESTful web service. Commented Jan 18, 2013 at 22:14
  • Also, in case you use Resharper, it provides a context action to override equality members which you should really use as it provides a better alternative to I4V's post. Commented Jan 18, 2013 at 22:21

1 Answer 1

2

You are making the instance-comparison. you should override Equals and GetHashcode in Group class. Operator overloading would also be good since you use == operator in Console.WriteLine

Otherwise;

new Group() { Name = "a" } == new Group() { Name = "a" }

or

new Group() { Name = "a" }.Equals(new Group() { Name = "a" })

would always return false

.

public class Group
{
    public string Name;
    public int i;


    public static bool operator ==(Group a, Group b)
    {
        return a.Equals(b);
    }

    public static bool operator !=(Group a, Group b)
    {
        return !(a.Name == b.Name);
    }

    public override bool Equals(object obj)
    {
        var g = obj as Group;
        if (ReferenceEquals(this,g)) return true;
        return g.Name.Equals(Name);
    }

    public override int GetHashCode()
    {
        return Name.GetHashCode();
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Nice idea and pretty neat. I'll look into this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.