Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Building a JSON Configuration Section

Tags:

Is there a way to have configuration sections written in JSON instead of XML?

Let's suppose I have the following ConfigurationSection:

public class UsersConfig : ConfigurationSection {

      [ConfigurationProperty("users",
                             IsRequired = false)]
      public UserCollection Users {
           get { return this["users"] as UserCollection; }
      }
}

[ConfigurationCollection(typeof(UserElement),
     AddItemName = "user"]
public class UsersCollection : ConfigurationElementCollection {
      protected override ConfigurationElement CreateNewElement() {
            return new UserElement();
      }

      protected override object GetElementKey(ConfigurationElement element) {
            return ((UserElement)element).Name;
      }
}

public class UserElement : ConfigurationElement {

     [ConfigurationProperty("name",
                            IsRequired = true,
                            IsKey = true)]
     public string Name {
          get { return this["name"] as string; }
          set { this["name"] = value; }
     }
}

I can then create the following XML configuration section:

<users-config>
      <users>
            <user name="Matt458" />
            <user name="JohnLennon" />
      </users>
</users-config>

What I would want to achieve is to mantain the same UsersConfig class, but instead of mapping it to XML, I would like to map it to a JSON:

{
    "users": [
        {
            "name": "Matt458"
        },
        {
             "name": "JohnLennon"
        }
    ]
}
like image 486
Matias Cicero Avatar asked May 05 '15 01:05

Matias Cicero


People also ask

Is JSON good for config file?

Don't Use JSON as a Configuration File Format.

How do I structure a JSON file?

Rules for JSON SyntaxData should be in name/value pairs. Data should be separated by commas. Curly braces should hold objects. Square brackets hold arrays.


2 Answers

this library might help you: https://github.com/Dynalon/JsonConfig :

from the documentation:

JsonConfig is a simple to use configuration library, allowing JSON based config files for your C#/.NET application instead of cumbersome web.config/application.config xml files. It is based on JsonFX and C# 4.0 dynamic feature. Allows putting your programs config file into .json files, where a default config can be embedded as a resource or put in the (web-)application folder. Configuration can be accessed via dynamic types, no custom classes or any other stub code is necessary. JsonConfig brings support for config inheritance, meaning a set of configuration files can be used to have a single, scoped configuration at runtime which is a merged version of all provided configuration files.

like image 190
DasDas Avatar answered Nov 14 '22 02:11

DasDas


If I understand correctly you essentially want to define web.config sections as JSON rather than XML.

There is currently nothing out of the box that does this that I am aware of. However one possible solution is to use Gulp to dynamically write your web.config file as part of your build step. I don't have a concrete example as I'm not aware of anyone doing this, but this might give you a pointer.

First have a look at this article that discusses how you can use the xmlpoke module to write XML:

http://www.mikeobrien.net/blog/using-gulp-to-build-and-deploy-dotnet-apps-on-windows/

Reading JSON in Gulp would be a piece of cake, so you just need to map the JSON into XML. You can then add the Gulp task to your build step by editing your .proj xml file (gulp should be installed globally on the machine the build is being performed on).

  1. Open your proj file in a text editor and locate this section:

    <Project>
        <Target Name="BeforeBuild">
        <!-- Insert tasks to run before build here -->
        </Target>
    </Project>
    
  2. Replace the comment with a gulp command

    gulp taskname
    

An alternative is to use a library such as Newtonsoft to read a JSON file from the disk. Then create your own attributes and system for mapping the values to the properties in your class. This is not straight forward but could certainly be done with some effort.

like image 29
garryp Avatar answered Nov 14 '22 03:11

garryp