How can I deserialize a string to Json object where the json Object can be a single or an array, right now I have this, which works but its a hack (pseudo):
class MyObject{
public string prop1
public string prop2;
}
class MyList{
List<MyObject> objects {get; set; }
}
class Test{
MyList list = JsonSerialzer.Deserialize<MyList>(str);
//if list is null - it can be single
if(list == null){
MyObject myObject = JsonSerializer.Deserialize<MyObject>(str);
if(myObject != null)
list.add(myObject);
}
}
As shown above, problem is the json String I am receiving from another service can be either single or list. How to handle this elegantly?