构建JSON配置节 [英] Building a JSON Configuration Section

查看:161
本文介绍了构建JSON配置节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法让写在JSON而不是XML配置节?



让我们假设我有以下配置节

 公共类UsersConfig:配置节{

[的ConfigurationProperty(用户,
IsRequired = FALSE)]
公共UserCollection用户{
{返回此[用户]作为UserCollection; }
}
}

[ConfigurationCollection(typeof运算(UserElement),
AddItemName =用户]
公共类UsersCollection:ConfigurationElementCollection中{
保护覆盖的ConfigurationElement CreateNewElement(){
返回新UserElement();
}

保护覆盖对象GetElementKey(的ConfigurationElement元素){
回报率((UserElement)元素).Name点;
}
}

公共类UserElement:{的ConfigurationElement

[的ConfigurationProperty(名,
IsRequired = TRUE ,
IsKey = TRUE)]
公共字符串名称{
{返回此[名称]作为字符串;}
集合{这个[名称] =值; }
}
}

我可以创建下面的XML配置部分:

 <用户 - 配置 -  GT; 
<用户>
<使用者名称=Matt458 />
<使用者名称=约翰·列侬/>
< /用户>
< /用户,配置>



我会想实现的是十个分量的相同UsersConfig类但不是将其映射到XML,我想它映射到JSON:

  {
用户 :[
{
名:Matt458
},
{
名:约翰·列侬
}
]
}


解决方案

我推荐使用FX.configuration,你可以从的NuGet添加。
你可以找到它: http://nugetmusthaves.com/Package/FX.Configuration:A>



可以在发现一些代码示例https://bitbucket.org/friendlyx/fx.configuration



它允许您添加到App.config中的东西,如:
< ;使用FX时/>



另一个选项添加键=JsonConfigVALUE ={'富''ID':'42','名'} .configuration是增加一个新的config.json文件,所有的配置,并在创建实例,将读取并解析它。



下面的代码不正是你需要使用新的JSON配置文件

 使用System.Collections.Generic; 
使用FX.Configuration;
命名空间JsonConfigurationConsole
{
类节目
{
静态无效的主要(字串[] args)
{
无功配置=新用户( );
}
}
公共类用户:JsonConfiguration
{
公开名单<使用者>用户{搞定;组; }
}
公共类用户
{
公共字符串名称{;组; }
}
}


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"
        }
    ]
}

解决方案

I'm recommending to use FX.configuration, you can add it from NuGet. you can find it at: http://nugetmusthaves.com/Package/FX.Configuration

some code examples can be found at: https://bitbucket.org/friendlyx/fx.configuration

it enables you to add to the App.config stuff like: < add key="JsonConfig" value="{ 'Id': '42', 'Name': 'foo' }" />

another option when using the FX.configuration is to add a new config.json file with all your configuration and at the creation of the instance it will read and parse it.

the following code does exactly what you need with the new json config file.

using System.Collections.Generic;
using FX.Configuration;
namespace JsonConfigurationConsole
{
    class Program
    {
        static void Main(string[] args)
        {
            var config = new Users();
        }
    }
    public class Users : JsonConfiguration
    {
        public List<User> users { get; set; }
    }
    public class User
    {
        public string name { get; set; }
    }
}

这篇关于构建JSON配置节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆