Home > Software design >  How to add multiple types of objects to json array?
How to add multiple types of objects to json array?

Time:07-04

I need to parse data to JSON in a particular format, which should look like this (notice that there are multiple types of objects in an array).

{
    "member details":
        [
            {
                "name": "John Doe",
                "type": "faculty",
                "teaching years": "5",
                "faculty code": "f154"
            },
            {
                "name": "Mary Storm",
                "type": "student",
                "studying years": "3",
                "student code": "s19"
            }
        ]
}

How can I add multiple objects to a JSON array?

I was thinking of interfaces. But what if none of the properties are common?

CodePudding user response:

Use an IEnumerable<T> where T is a shared base type (could be object, for example):

using System;
using System.Text.Json;
                    
public class Program
{
    public static void Main()
    {
        var values = new object[]
        {
            new A { PropertyA = "A value" },
            new B { PropertyB = "B value" },
        };
        
        Console.WriteLine(JsonSerializer.Serialize(values));
    }
    
    public class A
    {
        public string PropertyA { get; set; }
    }
    
    public class B
    {
        public string PropertyB { get; set; }
    }
}

https://dotnetfiddle.net/R2zUi2

CodePudding user response:

A. No common properties

But what if none of the properties are common?

In such case the objects should not be in the same collection.

B. Some common properties

B1. Separate collections

Separate the different types of objects into separate collections. It really makes the serialisation and deserialisation easy.

public class Org 
{
    List<Students> Students {get; set;}
    List<Teacher> Teachers {get; set;}
}

var teachers = new Teacher[] {
    new Teacher { FirstName = "Lucy", FacultyYears = 3},
};

var students = new Teacher[] {
    new Student { FirstName = "Luca", StudyingYears = 3}
};

var org = new Org
{
    Teachers = teachers,
    Students = students
}

B2. Use a base type

Rather than using object[] as Moho suggest I think using a base class Person would be more correct:

var members = new Person[] {
    new Teacher { FirstName = "Lucy", FacultyYears = 3},
    new Student { FirstName = "Luca", StudyingYears = 3}
};


class Person {
    public string FirstName {get; set;}
}

class Teacher : Person {
    public int FacultyYears { get; set;}
}

class Student : Person {
    public int StudyingYears { get; set;}
}
Note on Polymorphic collections and json

Please note that if you use polymorphic collections deserialisation (json -> objects) is not that obvious.

B3. Single shared model

It may be easier (it really depends on the scenario) to have a single type. For the collection shown it could be:

class Member {
    public string FirstName {get; set;} 
    
    public Role Role {get;set;}

    public int Years {get; set;} // If student this meas student years, if teacher it means faculty years 

    public string Code {get; set;} // If student this means student code, if teacher this means faculty years
}

  • Related