Bastien Vandamme
Bastien Vandamme

Reputation: 18485

How to properly load a configuration file as Json?

I have a question related to Json. Let me start by giving you my Json sample:

{
  "watch": [
    {
      "path": "C:/Temp",
      "conditions": "*.log",
      "actions": ""
    },
    {
      "path": "C:/Temps",
      "conditions": ".log",
      "actions": ""
    }
  ]
}

I want to watch directories. So I my Json will have an array of data.

I would like to cross check if I'm doing some stuff properly. I'm a C# developer but I'm quite new to .NET Core. Usually I load my Json file by using serialization from Newtonsoft package. With .NET Code I have the feeling we do this a little bit differently. I'm doing this:

        static void Main(string[] args)
        {
            var configurationBuilder = new ConfigurationBuilder()
                .SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile("config.json", optional: true, reloadOnChange: true)
                .Build();
        }

Then I can access my data by using this

configurationBuilder["watch:0:path"]
"C:/Temp"

Great but this is not what I want. I want to loop on my array. With the ConfigurationBuilder I have the feeling I cannot use the data as a loop. It seem all data from the config file is a key

enter image description here

This is logic because my config file can be anything. The builder has no guarantee my array will always have the same objects. So the builder consider each element in my arrays as a unique token. I can understand this.

So, how can I load my config or this Json file in a structured object? Is there a way to load this as a config file in a better way? Because for real I have 2 object in my array and I would like to loop on them. For each object I do something.

Upvotes: 1

Views: 11431

Answers (2)

Maxim Zabolotskikh
Maxim Zabolotskikh

Reputation: 3347

json files in configuration are not really meant to hold arrays. If you do staging in future, you will run in all kind of unpleasant surprises, see my answer here to know what I mean: Override array settings in appsettings.json with those in appsettings.Production.json I think in your case you are better off with Newtonsoft deserializing.

On the other hand, if staging is not an issue, you could hard type your setting:

public class Startup
{
  // Called by runtime
  public void ConfigureServices(IServiceCollection services)
  {
      services.Configure<WatchSettings>(configuration.GetSection(nameof(WatchSettings)));
  }
}

Than inject them into your classes via Options:

MyClass(IOptions<WatchSettings> watchSettings){}

And than in WatchSettings itself you can implement all kinds of things, like accessors, properties, enumerators and what not to hide the details from the calling code:

class WatchSettings{
 IEnumerable<Watch> Watches {get;set;}
}

Upvotes: 0

Yeasin Arafat
Yeasin Arafat

Reputation: 56

As your main goal is to bind JSON object into a plain C# object, you can have a look at Options Pattern in ASP .NET Core. The official documentation describes your situation well.

Upvotes: 0

Related Questions