Home > Enterprise >  Need help deserializing C# / .NET 6 into seperate objects
Need help deserializing C# / .NET 6 into seperate objects

Time:04-09

I am attempting to use JsonSerializer.Deserialize() from System.Text.JSON in .NET 6.

I have no control over the format of the JSON. Like ZERO.

I have used it successfully in the past but now the data I need to consume is more complicated (but not VERY complicated). I am assuming that I am simply describing my data incorrectly.

I have tried several things.... but the description below is the only way so far that I could consume the data at all.

I will try to keep this brief.

I think my biggest problem is that I am trying to use the wiz-bang "Paste Special -> Paste JSON as Classes" without really understanding how to form my classes for serialization/deserialization.

Here is a simple example of the JSON I am trying to consume:

[
  {
    "version": "1.0b",
    "sub_version": "x.y.barf"
  },
  {
    "somestring": "I am a string",
    "isCool": false,
    "a_cool_array": [
      "bob",
      "jill",
      "pete"
    ]
  }
]

If I use the whiz-bang "Paste Special" tool, I get the following generated for me.

public class Rootobject
{
    public Class1[] Property1 { get; set; }
}

public class Class1
{
    public string version { get; set; }//<-- I need these to remain in their own object 
    public string sub_version { get; set; }//<-- I need these to remain in their own object
    public string somestring { get; set; }
    public bool isCool { get; set; }
    public string[] a_cool_array { get; set; }
}

Here is the problem that I have.

The whiz-bang tool put my first object (with one version strings) and second (more complicated) object into the same object.

If I use a call like this:

var deserializedJSON = JsonSerializer.Deserialize<List<Class1>>(myJSONTextHere);

I end up with two objects in the list.

The first one has the versions filled out, the second one only has the other fields filled out.

This all makes sense to me but I don't know how to get around the problem.

I need these objects to model the JSON and I need them to save back in the same format when I re-serailize the modified classes elsewhere. This isn't my exact problem as I have simplified it for the question.

Can/will anyone help me please! :) I will name my next pet or a plant after you.

CodePudding user response:

Your json needs to look like this:

{
 "class1": [
   //more json
 ],
 "class2": [
   //more json
 ]
}

Then you will have:

public class RootObject 
{
 // class1 and 2 in here
}

public class Class1
{

}

public class Class2
{

}

CodePudding user response:

I have found one way around this problem.
It is ugly but seems to work. I hate that my code has to know about the data it is manipulating.

I used the actual JSON DOM to split the two disparate classes into individual JSON objects, then used the class de-serializer to load the individual objects into their given types.

In the following example, I am not checking anything.. I happen to know the order of the objects. I could check the raw JSON to make sure it was what I was looking for. In this case, I don't need to.

So, instead of taking the class structure as pasted by the super spiffy "Paste Classes from JSON" thingamajigger.. I split the classes myself.

Like this:

    public class VersionClass
    {
        public string version { get; set; }
        public string sub_version { get; set; }
    }

    public class DataClass
    {
        public string somestring { get; set; }
        public bool isCool { get; set; }
        public string[] a_cool_array { get; set; }
    }

Then, I can load the JSON into the objects I need like this:

     using var jsonDoc = JsonDocument.Parse(jsonText);

     var versionClass = jsonDoc.RootElement[0].Deserialize<VersionClass>();
     var dataClass    = jsonDoc.RootElement[1].Deserialize<DataClass>();

I hope this helps someone having the same problem.

  • Related