I have a class Person.cs
which needs to store a list of Friends (List of Person.cs
). However, it would be overkill to store the whole Person information in that array, just to have a complete object.
Json file
[
{
"Username": "mary",
"DisplayName": "Mary Sanchez",
"Age": 20,
"Friends": [
{"Username": "jonathan"},
{"Username": "katy"}
]
},
{
"Username": "jonathan",
"Age": 25,
"DisplayName": "Jonathan White",
"Friends": [
{"Username": "mary"},
{"Username": "katy"}
]
},
{
"Username": "katy",
"DisplayName": "Katy Rivers",
"Age": 28,
"Friends": [
{"Username": "mary"},
{"Username": "jonathan"}
]
}
]
C# class
public class Person
{
public string Username { get; set; }
public string DisplayName { get; set; }
public int Age { get; set; }
public List<Person> Friends { get; set; }
}
As you can see the Property Friends
is an array of Person
. It would be overkill for me to redefine all the persons in the array of friends of the json file, especially if it's a large file. I can't instantiate 'mary', because her friends 'jonathan' and 'katy' appear later in the json file. As you see this becomes a nesting dependency problem. Also assume this file has a lot of circular references as everyone is friends with each other.
The problem is ofcourse that I will end up with incomplete Person object in the array of Friends
. If I want to retrieve mary's friends ages, it will result in null values:
StreamReader reader = new StreamReader("Assets/Resources/Json/persons.json");
Persons = JsonConvert.DeserializeObject<List<Person>>(reader.ReadToEnd());
Person mary = Persons.Find(i => i.Username == "mary");
foreach(Person friend in mary.friends){
int age = friend.Age; //this will be null
}
So I tried to fix it by looping through the list again and manually retrieving the friend variable:
foreach (Person friend in mary.Friends)
{
Person friend = PersonManager.Persons.Find(i => i.Username == profile.Username);
int age = friend.age;
}
This works, but is very inefficient though. Especially if I have more properties that are like the Friends
property. Mind you, the Persons
array is in a separate class and I had to make the list static in order to retrieve it all times (i believe this is bad practice too). Is there a better way to achieve what I'm trying to achieve in one go?