1

This is not a duplicate. I want to read a collection from appsettings.json in my .Net core project.

The config entries are like below

Cars": [
    {
      "Name":"BMW",
    "Type":"Sedan",
    "SedanProperty":"value"
    },
    {
       "Name":"SUZUKI",
    "Type":"SUV"
    "SUVProperty":"value"
      }
]

I need to load this to a single collection List

The classes are.

public class Car
{
    public string Name;

}

public class Sedan:Car
{
    public string SedanProperty;

}

public class SUV:Car
{
    public string SUVProperty;

}

I am trying to do something like below

var cars= new List<Car>();
_configuration.GetSection("Cars").Bind(cars);

Any help on this is appreaciated

Ajith
  • 1,447
  • 2
  • 17
  • 31
  • 1
    This very likely is a duplicate. There are posts for reading from appsettings, and there are posts for deserializing JSON. What have you actually tried? –  Dec 02 '19 at 14:34
  • 3
    Rule #3: Whenever a question starts with the phrase "This is not a duplicate." (or anything along those lines) - There's a 99.999% it actually is a duplicate. – Zohar Peled Dec 02 '19 at 14:36
  • 1
    None of them answer this scenario – Ajith Dec 02 '19 at 14:36
  • 1
    They do, though. Reading from appsettings and deserializing json are posted here like 15 times a day –  Dec 02 '19 at 14:37
  • 2
    I have realised that there are similar questions. But they are not exactly the same. Please read the complete question – Ajith Dec 02 '19 at 14:37
  • have you looked at this? https://stackoverflow.com/questions/8241392/deserializing-heterogenous-json-array-into-covariant-list-using-json-net – Jawad Dec 02 '19 at 14:39
  • 1
    @Ajith .NET Core JSON settings are just JSON. Those settings are converted to key/value pairs. There are no types or inheritance involved. When you bind to a **concrete** type or use the `Option` pattern you tell .NET Core to construct objects from those key/value pairs. – Panagiotis Kanavos Dec 02 '19 at 14:45
  • 1
    @Ajith the duplicates you need to look for are those about binding and inheritance based on key-value pairs (if applicable) You may have to provide your own binding code that tries to get all "attributes" under a specific index and decide which object to create, based on the key names or any other custom logic. Or perhaps, a custom settings provider – Panagiotis Kanavos Dec 02 '19 at 14:51
  • Check this https://stackoverflow.com/questions/17500453/deserialize-json-to-subclasses – Ignasi93 Dec 02 '19 at 15:22
  • "Duplicate" doesn't mean it has to be the *exact* same scenario. If that were the case, there would be no duplicates as everyone's scenario is always slightly different. The point and what everyone has been telling you already here is that there are existing questions and answers that can be applied to your situation to create a solution. It doesn't have to be specifically about .NET Core Configuration, since this actually just boils down to deserializing JSON. – Chris Pratt Dec 02 '19 at 17:48
  • Unfortunately this does not just "boil down to deserializing JSON" as appsettings is fundamentally _not_ a json serialisation system, despite the wording of the question. appsettings is a system that gets values from zero or more json files, and from environment vars and any other source that it is configured to, combines all of them them; and only then deserialises that to objects - not from json! It does not work the same way as NewtonSoft.Json, the techniques that work there will not help here. – Anthony May 03 '23 at 08:05
  • @ChrisPratt Them answer that you linked is not "slightly different", it is entirely different, the the answer there can not be applied to appsettings. This is not a duplicate of that. However, here is a closely related question to this, with an answer: https://stackoverflow.com/questions/55954858/how-to-load-polymorphic-objects-in-appsettings-json – Anthony May 03 '23 at 08:20

1 Answers1

0

You can use only one class as a Model as bellow.

    using Newtonsoft.Json;
    using Newtonsoft.Json.Linq;

    public class Car
    {
         public string Name {set; get;}
         public string Type {set; get;}
         public string Property {set; get;}

          [JsonExtensionData]
          private IDictionary<string, JToken> _additionalData;

          [OnDeserialized]// this is executed on desirialise..
          private void OnDeserialized(StreamingContext context)
           {
               Property = (string) context;
                 // the contexts has the value of the Property for each time.
           }

         }
Nikolao
  • 29
  • 1
  • 5