Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# How do I correctly deserialize multiple custom objects in an array to an object?

I am retrieving a json object with an array, which contains multiple object types.

Given I retrieve a JSON payload that looks like this:

{
   "Lines": [
   {
       "PropertyA": "A",
       "PropertyB": "B"
   },
   {
       "Property01": 1,
       "Property02": 2
   }]
}

I want to deserialize this into a single object list.

Example:

public List<Line> Lines;

So I can compare the object with one I expect.

What I have so far:

public class Class1
{
    public string PropertyA = "A";
    public string PropertyB = "B";
}

public class Class2
{
    public int Property01 = 01;
    public int Property02 = 02;
}

public class MainClass
{
    public List<dynamic> Lines;
}

class Program
{
    static void Main(string[] args)
    {
        string json = "{\r\n \"Lines\": [\r\n {\r\n \"PropertyA\": \"A\",\r\n \"PropertyB\": \"B\"\r\n },\r\n {\r\n \"Property01\": 1,\r\n \"Property02\": 2\r\n }]\r\n}";

        MainClass actual = JsonConvert.DeserializeObject<MainClass>(json);

        MainClass expected = new MainClass()
        {
            Lines = new List<dynamic>()
            {
                new Class1(),
                new Class2()
            }
        };

        actual.Should().BeEquivalentTo(expected);
    }
}   

Any help would be greatly appreciated!

Cheers

like image 202
lacking-cypher Avatar asked Oct 15 '25 14:10

lacking-cypher


3 Answers

You can verify the propriety name and deserialize in right object like that

class Program
{
    static void Main(string[] args)
    {

        var class1List = new List<Class1>();
        var class2List = new List<Class2>();
        var genericList = new List<dynamic>();

        var actual = JsonConvert.DeserializeObject<dynamic>(json);
        foreach (var item in actual.Lines)
        {

            string itemJson = JsonConvert.SerializeObject(item);
            if (itemJson.Contains("PropertyA"))
            {
                var class1 = JsonConvert.DeserializeObject<Class1>(itemJson);
                class1List.Add(class1);
                genericList.Add(class1);
            }
            else
            {
                var class2 = JsonConvert.DeserializeObject<Class2>(itemJson);
                class2List.Add(class2);
                genericList.Add(class2);
            }
        }
    }
}

public class Class1
{
    public string PropertyA;
    public string PropertyB;
}

public class Class2
{
    public int Property01;
    public int Property02;
}
like image 69
João Rafael Colombo Avatar answered Oct 18 '25 09:10

João Rafael Colombo


You can verify the propriety name and deserialize in right object like that

class Program
{
    static void Main(string[] args)
    {

        var class1List = new List<Class1>();
        var class2List = new List<Class2>();
        var genericList = new List<dynamic>();

        var actual = JsonConvert.DeserializeObject<dynamic>(json);
        foreach (var item in actual.Lines)
        {

            string itemJson = JsonConvert.SerializeObject(item);
            if (itemJson.Contains("PropertyA"))
            {
                var class1 = JsonConvert.DeserializeObject<Class1>(itemJson);
                class1List.Add(class1);
                genericList.Add(class1);
            }
            else
            {
                var class2 = JsonConvert.DeserializeObject<Class2>(itemJson);
                class2List.Add(class2);
                genericList.Add(class2);
            }
        }
    }
}

public class Class1
{
    public string PropertyA;
    public string PropertyB;
}

public class Class2
{
    public int Property01;
    public int Property02;
}
like image 30
João Rafael Colombo Avatar answered Oct 18 '25 09:10

João Rafael Colombo


Firstly change fields to properties. Then the easiest way here is to create one common object representing both objects like this:

public class Class1
{   
    public string PropertyA {get;set;}
    public string PropertyB {get;set;}
    public int Property01 {get;set;}
    public int Property02 {get;set;}
}

public class MainClass
{
    public List<Class1> Lines;
}

and deserialization would look like this:

 MainClass actual = JsonConvert.DeserializeObject<MainClass>(json);

If you change class definition of Class1 to object with nullable properties you'll be able to detect if property existed in the json to differentiate between both objects:

public class Class1
{   
    public string PropertyA {get;set;}
    public string PropertyB {get;set;}
    public int? Property01 {get;set;}
    public int? Property02 {get;set;}
}

Another way is to use JArray and continue with your logic using this object:

JArray arr = JArray.Parse(json);
like image 43
MistyK Avatar answered Oct 18 '25 08:10

MistyK