0

Is there a way to maintain object references when reading the JSON file in C#?

Let's say I have the following JSON text:

{
   "obj1":
         {
           "prop1": 1,
           "prop2":
                  [
                    {
                      "Id": 1,
                      "Name": "me"
                    },
                    {
                      "Id": 2,
                      "Name": "you"
                    }   
                  ]
         },
    "obj2": 
          [
            {
               "Id": 1,
               "Name": "me"
            }
         ]
}

I'd like to have member with Id 1 under prop2 to to refer to the same object as the one under obj2 with Id 1. i.e. changing one have effect on the other.

EDIT: I could use PreserveReferencesHandling setting, but my problem is i'm consuming the json so i don't have much control over the SerializeObject other than to re-Serialize it after i have read it. I was wondering if there is cheat to solve my problem. Otherwise I've thought of equating them after i've read them. i.e, after reading them i just say: obj2[0] == obj1.prop2[0].

rethabile
  • 3,029
  • 6
  • 34
  • 68

0 Answers0