1

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.

Bastien Vandamme
  • 17,659
  • 30
  • 118
  • 200
  • Can you try this `IConfiguration configuration; var section = configuration.GetSection("watch"); if(section != null) { var children = section.GetChildren();` – Vivek Nuna Jun 18 '21 at 05:07
  • Does this answer your question? [How to map multiple levels of Settings from appsettings.json in .Net 5 (Asp.Net)](https://stackoverflow.com/questions/67652795/how-to-map-multiple-levels-of-settings-from-appsettings-json-in-net-5-asp-net) – touchofevil Jun 18 '21 at 05:50

2 Answers2

1

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;}
}
Maxim Zabolotskikh
  • 3,091
  • 20
  • 21
  • I will try this. I'm new with .NET Core. Can you tell me what this services class is coming from exactly. – Bastien Vandamme Jun 21 '21 at 02:52
  • I will start by using the classic Newtonsoft Json library to load my watch.json file. – Bastien Vandamme Jun 21 '21 at 02:55
  • services comes from the Startup class, which is rather common for .net core apps and where all DI staff is configured. It is added to the host in Main and takes care of the setup. Main itself has little logic. Here is some info to get started: https://learn.microsoft.com/en-us/aspnet/core/fundamentals/startup?view=aspnetcore-5.0 – Maxim Zabolotskikh Jun 21 '21 at 06:33
0

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.