0

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?

2
  • You should correct your code so that it at least compile. Commented Dec 2, 2020 at 13:14
  • 1
    Can you construct a minimal reproducible example consisting of (1) a minimal definition of MyJsonObject, (2) a literal JSON string and (3) a call to DeserializeObject that fails? Commented Dec 2, 2020 at 13:18

1 Answer 1

1

There is no problem with a data structure like this(Array of Array of Objects) when we are working with JSON.

Try this in your code:

List<FooData[]> foo = new List<FooData[]>()
{
    new FooData[]
    {
        new FooData() { FooString1 = "foo string 1" , FooString2 = "foo string 2", FooString3 = "foo string 3" },
        new FooData() {FooString1 = "foo string 4" , FooString2 = "foo string 5", FooString3 = "foo string 6" }
    },
    new FooData[]
    {
        new FooData() { FooString1 = "foo string 1" , FooString2 = "foo string 2", FooString3 = "foo string 3" },
        new FooData() {FooString1 = "foo string 4" , FooString2 = "foo string 5", FooString3 = "foo string 6" }
    }
};

List<FaaData[]> faa = new List<FaaData[]>()
{
    new FaaData[]
    {
        new FaaData() { FaaString1 = "faa string 1" , FaaString2 = "faa string 2", FaaString3 = "faa string 3" },
        new FaaData() {FaaString1 = "faa string 4" , FaaString2 = "faa string 5", FaaString3 = "faa string 6" }
    },
    new FaaData[]
    {
        new FaaData() { FaaString1 = "faa string 1" , FaaString2 = "faa string 2", FaaString3 = "faa string 3" },
        new FaaData() {FaaString1 = "faa string 4" , FaaString2 = "faa string 5", FaaString3 = "faa string 6" }
    }
};

var JsonThis = new 
{
    Foo = foo.ToArray(),
    Faa = faa.ToArray()
};

string JsonString = JsonConvert.SerializeObject(JsonThis, Formatting.Indented,
                    new JsonSerializerSettings { PreserveReferencesHandling = PreserveReferencesHandling.Objects });

var result = JsonConvert.DeserializeObject<MyJsonObject>(JsonString);

This is the new definition of classes. I tried to follow the C# Coding Standards and Naming Conventions

public class MyJsonObject
{
    public FooData[][] Foo { get; set; }
    public FaaData[][] Faa { get; set; }
}

public class FooData
{
    public string FooString1 { get; set; }
    public string FooString2 { get; set; }
    public string FooString3 { get; set; }
}

public class FaaData
{
    public string FaaString1 { get; set; }
    public string FaaString2 { get; set; }
    public string FaaString3 { get; set; }
}

This is the result:

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

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.