3

I am building a console app using .NET 6. To load the settings I am using the Options Pattern and I want to create json config file with a collection of objects that has variations in one of its properties.

Example json:

{
  "Persons": [
    {
      "Name": "Josh Wink",
      "Information": {
        "SSN": "BC00020032",
        "VatId": "100099332"
      },
      "Characteristics": {
        "Gender": "male",
        "Age": 23
      }
    },
    {
      "Name": "Peter Gabriel",
      "Information": {
        "SSN": "GH00092921",
        "VatId": "100003322"
      },
      "Characteristics": {
        "EyeColor": "brown",
        "HairColor": "black"
      }
    }
  ],
  "SomeOtherSection": "Some Value"
}

I thought of using an empty interface for the Characteristics property, but I don't know how to map this section using Configuration.GetSection().Bind() or Configuration.GetSection().Get

Here are the classes I created

class PersonsCollection
{
    List<Person> Persons { get; set;} = new();
}
class Person
{
    string Name { get; set; } = String.Empty;
    PersonInfo Information { get; set; } = new();
    ICaracteristics? Characteristics { get; set; }
}
class PersonInfo
{
    string SSN { get; set; } = String.Empty;
    string VatId { get; set; } = String.Empty;
}
interface ICaracteristics
{

}
class PersonCharacteristicsType1 : ICaracteristics
{
    string Name { get; set; } = String.Empty;
    int Age { get; set; } = 0;
}
class PersonCharacteristicsType2 : ICaracteristics
{
    string EyeColor { get; set; } = String.Empty;
    string HairColor { get; set; } = String.Empty;
}

2 Answers 2

1

There are multiple problems with your classes. First of all, you are missing public access modifiers for properties.

Secondary binder can't bind your ICaracteristics? Characteristics property because it is an interface and binder does not know how to convert data to it. You can introduce concrete class here which will have all the possible properties:

class PersonsCollection
{
    public List<Person> Persons { get; set;} = new();
}
class Person
{
    public string Name { get; set; } = String.Empty;
    public PersonInfo Information { get; set; } = new();
    public Characteristics? Characteristics { get; set; }
}
class Characteristics
{
    public string Name { get; set; } = String.Empty;
    public int Age { get; set; } = 0;
    public string EyeColor { get; set; } = String.Empty;
    public string HairColor { get; set; } = String.Empty;
}

class PersonInfo
{
    public string SSN { get; set; } = String.Empty;
    public string VatId { get; set; } = String.Empty;
}

And then handle the "types" in the code.

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

1 Comment

That is a kind of solution, thanks.
0

It seems if you want to keep the distinct derived classes you will need to put a discriminator in your JSON to hint to the serializer which derived class to instantiate.

See documentation on this approach here... https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json/polymorphism

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.