So I have a lot of data that just needs to be stored and not configured so I've decided to store it as Json in my database.
List<fooData[]> foo = new List<fooData[]>();
List<faaData[]> faa = new List<faaData[]>();
foreach(Choice c in choices)
{
foo.Add(getFooData(c)) // Returns IEnumerable<fooData>
faa.Add(getFaaData(c)) // Returns IEnumerable<faaData>
}
var JsonThis = MyJsonObject
{
fooArray = foo.ToArray(),
faaArray = faa.ToArray()
};
string JsonString = JsonConvert.SerializeObject(JsonThis, Formatting.Indented,
new JsonSerializerSettings { PreserveReferencesHandling = PreserveReferencesHandling.Objects });
Fine, that worked as expected. My problem occurs when I Deserialize my Json again, no matter if I take from the database or I do it on the next line, I just get thrown a NullReferenceException on the properties of fooData & faaData.
Heres the code:
MyJsonObject obj = JsonConvert.DeserializeObject<MyJsonObject>(JsonString,
new JsonSerializerSettings { PreserveReferencesHandling = PreserveReferencesHandling.Objects });
JsonObject
[JsonObject(IsReference = true)]
public class MyJsonObject
{
public fooData[][] foo { get; set; }
public faaData[][] faa { get; set; }
}
faaData & fooData are 99% identical
[JsonObject(IsReference = true)]
public class faaData
{
public string faaString1 { get; set; }
public string faaString2 { get; set; }
public string faaString3 { get; set; }
}
serialized JsonString example:
{
"Foo": [
[
{
"FooString1": "foo string 1",
"FooString2": "foo string 2",
"FooString3": "foo string 3"
},
{
"FooString1": "foo string 4",
"FooString2": "foo string 5",
"FooString3": "foo string 6"
}
],
[
{
"FooString1": "foo string 1",
"FooString2": "foo string 2",
"FooString3": "foo string 3"
},
{
"FooString1": "foo string 4",
"FooString2": "foo string 5",
"FooString3": "foo string 6"
}
]
],
"Faa": [
[
{
"FaaString1": "faa string 1",
"FaaString2": "faa string 2",
"FaaString3": "faa string 3"
},
{
"FaaString1": "faa string 4",
"FaaString2": "faa string 5",
"FaaString3": "faa string 6"
}
],
[
{
"FaaString1": "faa string 1",
"FaaString2": "faa string 2",
"FaaString3": "faa string 3"
},
{
"FaaString1": "faa string 4",
"FaaString2": "faa string 5",
"FaaString3": "faa string 6"
}
]
]
}
My question: Is it not possible to Serialize MyJsonObject with arrays of arrays of objects and get it back in the same object as initially? Or Am I doing something wrong?

MyJsonObject, (2) a literal JSON string and (3) a call toDeserializeObjectthat fails?