Esempio n. 1
0
        public void ArrayMerge()
        {
            var json1 = @"{
                'ip': [
                    '1.2.3.4',
                    '7.8.9.10',
                    '11.12.13.14'
                ]
            }";

            var json2 = @"{
                'ip': {
                    '3': '15.16.17.18'
                }
            }";

            var jsonConfigSource1 = new JsonConfigurationProvider(TestStreamHelpers.ArbitraryFilePath);
            jsonConfigSource1.Load(TestStreamHelpers.StringToStream(json1));

            var jsonConfigSource2 = new JsonConfigurationProvider(TestStreamHelpers.ArbitraryFilePath);
            jsonConfigSource2.Load(TestStreamHelpers.StringToStream(json2));

            var builder = new ConfigurationBuilder();
            builder.Add(jsonConfigSource1, load: false);
            builder.Add(jsonConfigSource2, load: false);
            var config = builder.Build();

            Assert.Equal(4, config.GetSection("ip").GetChildren().Count());
            Assert.Equal("1.2.3.4", config["ip:0"]);
            Assert.Equal("7.8.9.10", config["ip:1"]);
            Assert.Equal("11.12.13.14", config["ip:2"]);
            Assert.Equal("15.16.17.18", config["ip:3"]);
        }
        static void Main(string[] args)
        {
            string configPath = GetConfigPath();
            var configProvider = new JsonConfigurationProvider<FileToSyncItemConfiguration>(configPath);
            var configurationWrapper = configProvider.GetConfig();

            if (configurationWrapper != null)
            {
                var serializationConfig = configurationWrapper.SerializationManagerConfig;
                foreach (FileToSyncItemConfiguration configuration in configurationWrapper.SynchronizationConfigs)
                {
                    var fileToSyncItemConverter = new FileToSyncItemConverter(configuration, serializationConfig);
                    fileToSyncItemConverter.Convert();
                }
            }
        }
Esempio n. 3
0
        public void ArraysAreConvertedToKeyValuePairs()
        {
            var json = @"{
                'ip': [
                    '1.2.3.4',
                    '7.8.9.10',
                    '11.12.13.14'
                ]
            }";

            var jsonConfigSource = new JsonConfigurationProvider(TestStreamHelpers.ArbitraryFilePath);
            jsonConfigSource.Load(TestStreamHelpers.StringToStream(json));
            
            Assert.Equal("1.2.3.4", jsonConfigSource.Get("ip:0"));
            Assert.Equal("7.8.9.10", jsonConfigSource.Get("ip:1"));
            Assert.Equal("11.12.13.14", jsonConfigSource.Get("ip:2"));
        }
        public void SupportAndIgnoreComments()
        {
            var json          = @"/* Comments */
                {/* Comments */
                ""name"": /* Comments */ ""test"",
                ""address"": {
                    ""street"": ""Something street"", /* Comments */
                    ""zipcode"": ""12345""
                }
            }";
            var jsonConfigSrc = new JsonConfigurationProvider(TestStreamHelpers.ArbitraryFilePath);

            jsonConfigSrc.Load(TestStreamHelpers.StringToStream(json));

            Assert.Equal("test", jsonConfigSrc.Get("name"));
            Assert.Equal("Something street", jsonConfigSrc.Get("address:street"));
            Assert.Equal("12345", jsonConfigSrc.Get("address:zipcode"));
        }
        public void TransferNoJson_MustReturnNull()
        {
            var root      = CreateRoot(out var fn);
            var condition = new JsonChangeTransferCondition();
            var prov      = new NullConfigurationProvider();
            var rep       = new ChangeReport(root, prov, new ConfigurationChangeInfo[0]);
            var val       = condition.GetTransfe(rep);

            Assert.IsNull(val);
            var f = new JsonConfigurationProvider(new JsonConfigurationSource
            {
                Path = "a.xml"
            });

            rep = new ChangeReport(root, f, new ConfigurationChangeInfo[0]);
            val = condition.GetTransfe(rep);
            Assert.IsNull(val);
        }
Esempio n. 6
0
        private void SetupConfigurationProvider(IServiceCollection services)
        {
            var mediaPlayer = new MediaPlayer();

            mediaPlayer.Open(new Uri(Directory.GetCurrentDirectory() + "\\Resources\\intro.mp3"));
            mediaPlayer.Volume = 100;

            //TODO: to use Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
            var documentsFolder = AppDomain.CurrentDomain.BaseDirectory;
            var configManager   = new JsonConfigurationProvider(documentsFolder + "configuration.json");

            configManager.LoadFromSource();

            services.AddSingleton <IProfileConfigurationProvider>(configManager)
            .AddSingleton(typeof(KeyboardHook))
            .AddSingleton(typeof(PreviewImageHolder))
            .AddSingleton(mediaPlayer);
        }
Esempio n. 7
0
        public void ArraysAreConvertedToKeyValuePairs()
        {
            var json = @"{
                'ip': [
                    '1.2.3.4',
                    '7.8.9.10',
                    '11.12.13.14'
                ]
            }";

            var jsonConfigSource = new JsonConfigurationProvider(TestStreamHelpers.ArbitraryFilePath);

            jsonConfigSource.Load(TestStreamHelpers.StringToStream(json));

            Assert.Equal("1.2.3.4", jsonConfigSource.Get("ip:0"));
            Assert.Equal("7.8.9.10", jsonConfigSource.Get("ip:1"));
            Assert.Equal("11.12.13.14", jsonConfigSource.Get("ip:2"));
        }
Esempio n. 8
0
        public void ArraysAreConvertedToKeyValuePairs()
        {
            var json = @"{
                ""ip"": [
                    ""1.2.3.4"",
                    ""7.8.9.10"",
                    ""11.12.13.14""
                ]
            }";

            var jsonConfigSource = new JsonConfigurationProvider(new JsonConfigurationSource());

            jsonConfigSource.Load(TestStreamHelpers.StringToStream(json));

            Assert.Equal("1.2.3.4", jsonConfigSource.Get("ip:0"));
            Assert.Equal("7.8.9.10", jsonConfigSource.Get("ip:1"));
            Assert.Equal("11.12.13.14", jsonConfigSource.Get("ip:2"));
        }
Esempio n. 9
0
        public static string GetValueByKey(string key)
        {
            var jsonConfig = new JsonConfigurationSource();

            jsonConfig.FileProvider = new PhysicalFileProvider(Directory.GetCurrentDirectory());
            jsonConfig.Path         = "appsettings.json";
            var jsonProvider = new JsonConfigurationProvider(jsonConfig);

            jsonProvider.Load();

            jsonProvider.TryGet(key, out string value);

            if (string.IsNullOrWhiteSpace(value))
            {
                throw new Exception(string.Concat("Can not find ", key, " from appsettings.json"));
            }
            return(value);
        }
Esempio n. 10
0
        public void IConfigurationProvider_JsonConfigurationProvider()
        {
            var fileProvider = new PhysicalFileProvider(Directory.GetCurrentDirectory());
            var source       = new JsonConfigurationSource
            {
                FileProvider = fileProvider,
                // 这里应该是相对fileProvider的路径而不是绝对路径, 如果是绝对路径, 即时文件存在且是fileProvider根目录下的也是NotFoundException
                Path           = "appsettings.json",
                Optional       = false,
                ReloadOnChange = false
            };
            var provider = new JsonConfigurationProvider(source);

            provider.TryGet("Name", out var name0);
            Assert.Null(name0);
            provider.Load();
            provider.TryGet("Name", out var name1);
            Assert.Equal(name1, "SinxHe");
        }
        public void LoadKeyValuePairsFromValidJson()
        {
            var json          = @"
{
    'firstname': 'test',
    'test.last.name': 'last.name',
        'residential.address': {
            'street.name': 'Something street',
            'zipcode': '12345'
        }
}";
            var jsonConfigSrc = new JsonConfigurationProvider(TestStreamHelpers.ArbitraryFilePath);

            jsonConfigSrc.Load(TestStreamHelpers.StringToStream(json));

            Assert.Equal("test", jsonConfigSrc.Get("firstname"));
            Assert.Equal("last.name", jsonConfigSrc.Get("test.last.name"));
            Assert.Equal("Something street", jsonConfigSrc.Get("residential.address:STREET.name"));
            Assert.Equal("12345", jsonConfigSrc.Get("residential.address:zipcode"));
        }
Esempio n. 12
0
        /// <summary>
        /// Builds <see cref="RecurringJob"/> automatically by using a JSON configuration.
        /// </summary>
        /// <param name="jsonFile">Json file for <see cref="RecurringJob"/> configuration.</param>
        /// <param name="reloadOnChange">Whether the <see cref="RecurringJob"/> should be reloaded if the file changes.</param>
        public static void AddOrUpdate(string jsonFile, bool reloadOnChange = true)
        {
            if (string.IsNullOrWhiteSpace(jsonFile))
            {
                throw new ArgumentNullException(nameof(jsonFile));
            }

            var configFile = File.Exists(jsonFile) ? jsonFile :
                             Path.Combine(
                AppContext.BaseDirectory,
                jsonFile);

            if (!File.Exists(configFile))
            {
                throw new FileNotFoundException($"The json file {configFile} does not exist.");
            }

            IConfigurationProvider provider = new JsonConfigurationProvider(configFile, reloadOnChange);

            AddOrUpdate(provider);
        }
Esempio n. 13
0
        public void Start()
        {
            var configSource = new JsonConfigurationProvider($@"{_applicationEnvironment.ApplicationBasePath}\config.json");

            var config = new ConfigurationBuilder()
                         .Add(configSource)
                         .Build();

            var builder = new WebHostBuilder(config);

            builder.UseServer("Microsoft.AspNet.Server.Kestrel");
            builder.UseServices(services => services.AddMvc());
            builder.UseStartup(appBuilder =>
            {
                appBuilder.UseDefaultFiles();
                appBuilder.UseStaticFiles();
                appBuilder.UseMvc();
            });

            _application = builder.Build().Start();
        }
Esempio n. 14
0
        static void Main(string[] args)
        {
            var builder = new ConfigurationBuilder()
                          .SetBasePath(Directory.GetCurrentDirectory())
                          .AddJsonFile("appsettings.json");

            var configuration = builder.Build();

            JsonConfigurationProvider jcp = configuration.Providers.FirstOrDefault() as JsonConfigurationProvider;

            string connStr = string.Empty;
            int    i       = 1;

            while (jcp.TryGet("TestConnection" + i, out connStr))
            {
                i++;

                Console.WriteLine("--------------------");
                Console.WriteLine("Will test " + connStr);

                using (SqlConnection connection = new SqlConnection(connStr))
                {
                    try
                    {
                        connection.Open();
                        Console.WriteLine("Success");
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                        Console.WriteLine(ex.StackTrace);
                    }
                }
            }

            Console.WriteLine("We're done. Press any key to quit.");
            Console.ReadKey();
        }
        private static IConfigurationRoot LoadAppConfigFile(string appConfigFilePath)
        {
            IConfigurationBuilder   configurationBuilder;
            JsonConfigurationSource configurationSource;
            IConfigurationProvider  configurationProvider;
            IConfigurationRoot      configurationRoot;

            if ((object)appConfigFilePath == null)
            {
                throw new ArgumentNullException(nameof(appConfigFilePath));
            }

            configurationBuilder = new ConfigurationBuilder();
            configurationSource  = new JsonConfigurationSource()
            {
                Optional = false, Path = appConfigFilePath
            };
            configurationProvider = new JsonConfigurationProvider(configurationSource);
            configurationBuilder.Add(configurationSource);
            configurationRoot = configurationBuilder.Build();

            return(configurationRoot);
        }
Esempio n. 16
0
        public void NestedArrays()
        {
            var json = @"{
                'ip': [
                    [ 
                        '1.2.3.4',
                        '5.6.7.8'
                    ],
                    [ 
                        '9.10.11.12',
                        '13.14.15.16'
                    ],
                ]
            }";

            var jsonConfigSource = new JsonConfigurationProvider(TestStreamHelpers.ArbitraryFilePath);
            jsonConfigSource.Load(TestStreamHelpers.StringToStream(json));

            Assert.Equal("1.2.3.4", jsonConfigSource.Get("ip:0:0"));
            Assert.Equal("5.6.7.8", jsonConfigSource.Get("ip:0:1"));
            Assert.Equal("9.10.11.12", jsonConfigSource.Get("ip:1:0"));
            Assert.Equal("13.14.15.16", jsonConfigSource.Get("ip:1:1"));
        }
Esempio n. 17
0
        public void ArrayOfObjects()
        {
            var json = @"{
                'ip': [
                    {
                        'address': '1.2.3.4',
                        'hidden': false
                    },
                    {
                        'address': '5.6.7.8',
                        'hidden': true
                    }
                ]
            }";

            var jsonConfigSource = new JsonConfigurationProvider(TestStreamHelpers.ArbitraryFilePath);
            jsonConfigSource.Load(TestStreamHelpers.StringToStream(json));

            Assert.Equal("1.2.3.4", jsonConfigSource.Get("ip:0:address"));
            Assert.Equal("False", jsonConfigSource.Get("ip:0:hidden"));
            Assert.Equal("5.6.7.8", jsonConfigSource.Get("ip:1:address"));
            Assert.Equal("True", jsonConfigSource.Get("ip:1:hidden"));
        }
Esempio n. 18
0
        public void ArrayOfObjects()
        {
            var json = @"{
                'ip': [
                    {
                        'address': '1.2.3.4',
                        'hidden': false
                    },
                    {
                        'address': '5.6.7.8',
                        'hidden': true
                    }
                ]
            }";

            var jsonConfigSource = new JsonConfigurationProvider(TestStreamHelpers.ArbitraryFilePath);

            jsonConfigSource.Load(TestStreamHelpers.StringToStream(json));

            Assert.Equal("1.2.3.4", jsonConfigSource.Get("ip:0:address"));
            Assert.Equal("False", jsonConfigSource.Get("ip:0:hidden"));
            Assert.Equal("5.6.7.8", jsonConfigSource.Get("ip:1:address"));
            Assert.Equal("True", jsonConfigSource.Get("ip:1:hidden"));
        }
Esempio n. 19
0
        public void ArrayMerge()
        {
            var json1 = @"{
                'ip': [
                    '1.2.3.4',
                    '7.8.9.10',
                    '11.12.13.14'
                ]
            }";

            var json2 = @"{
                'ip': {
                    '3': '15.16.17.18'
                }
            }";

            var jsonConfigSource1 = new JsonConfigurationProvider(TestStreamHelpers.ArbitraryFilePath);

            jsonConfigSource1.Load(TestStreamHelpers.StringToStream(json1));

            var jsonConfigSource2 = new JsonConfigurationProvider(TestStreamHelpers.ArbitraryFilePath);

            jsonConfigSource2.Load(TestStreamHelpers.StringToStream(json2));

            var configurationBuilder = new ConfigurationBuilder();

            configurationBuilder.Add(jsonConfigSource1, load: false);
            configurationBuilder.Add(jsonConfigSource2, load: false);
            var config = configurationBuilder.Build();

            Assert.Equal(4, config.GetSection("ip").GetChildren().Count());
            Assert.Equal("1.2.3.4", config["ip:0"]);
            Assert.Equal("7.8.9.10", config["ip:1"]);
            Assert.Equal("11.12.13.14", config["ip:2"]);
            Assert.Equal("15.16.17.18", config["ip:3"]);
        }
Esempio n. 20
0
        public void NestedArrays()
        {
            var json = @"{
                'ip': [
                    [ 
                        '1.2.3.4',
                        '5.6.7.8'
                    ],
                    [ 
                        '9.10.11.12',
                        '13.14.15.16'
                    ],
                ]
            }";

            var jsonConfigSource = new JsonConfigurationProvider(TestStreamHelpers.ArbitraryFilePath);

            jsonConfigSource.Load(TestStreamHelpers.StringToStream(json));

            Assert.Equal("1.2.3.4", jsonConfigSource.Get("ip:0:0"));
            Assert.Equal("5.6.7.8", jsonConfigSource.Get("ip:0:1"));
            Assert.Equal("9.10.11.12", jsonConfigSource.Get("ip:1:0"));
            Assert.Equal("13.14.15.16", jsonConfigSource.Get("ip:1:1"));
        }
Esempio n. 21
0
        public void ArrayOfObjects()
        {
            var json = @"{
                ""ip"": [
                    {
                        ""address"": ""1.2.3.4"",
                        ""hidden"": false
                    },
                    {
                        ""address"": ""5.6.7.8"",
                        ""hidden"": true
                    }
                ]
            }";

            var jsonConfigSource = new JsonConfigurationProvider(new JsonConfigurationSource());

            jsonConfigSource.Load(TestStreamHelpers.StringToStream(json));

            Assert.Equal("1.2.3.4", jsonConfigSource.Get("ip:0:address"));
            Assert.Equal("False", jsonConfigSource.Get("ip:0:hidden"));
            Assert.Equal("5.6.7.8", jsonConfigSource.Get("ip:1:address"));
            Assert.Equal("True", jsonConfigSource.Get("ip:1:hidden"));
        }
Esempio n. 22
0
        public void NestedArrays()
        {
            var json = @"{
                ""ip"": [
                    [ 
                        ""1.2.3.4"",
                        ""5.6.7.8""
                    ],
                    [ 
                        ""9.10.11.12"",
                        ""13.14.15.16""
                    ]
                ]
            }";

            var jsonConfigSource = new JsonConfigurationProvider(new JsonConfigurationSource());

            jsonConfigSource.Load(TestStreamHelpers.StringToStream(json));

            Assert.Equal("1.2.3.4", jsonConfigSource.Get("ip:0:0"));
            Assert.Equal("5.6.7.8", jsonConfigSource.Get("ip:0:1"));
            Assert.Equal("9.10.11.12", jsonConfigSource.Get("ip:1:0"));
            Assert.Equal("13.14.15.16", jsonConfigSource.Get("ip:1:1"));
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="UserSecretsSecretProvider"/> class.
 /// </summary>
 /// <param name="jsonProvider">The JSON configuration instance to provide the loaded user secrets.</param>
 /// <exception cref="ArgumentNullException">Thrown when the <paramref name="jsonProvider"/> is <c>null</c>.</exception>
 public UserSecretsSecretProvider(JsonConfigurationProvider jsonProvider)
 {
     Guard.NotNull(jsonProvider, nameof(jsonProvider), "Requires a JSON configuration instance to provide user secrets");
     _jsonProvider = jsonProvider;
 }
Esempio n. 24
0
 private static void ConfigureLogger(WebHostBuilderContext WebHostBuilderContext, LoggerConfiguration LoggerConfiguration)
 {
     LoggerConfiguration.ReadFrom.Configuration(JsonConfigurationProvider.BuildConfigurations());
 }
        public static void Initialise(IApplicationEnvironment applicationEnvironment)
        {
            var configProvider = new JsonConfigurationProvider($@"{applicationEnvironment.ApplicationBasePath}\config.json");

            Configuration = new ConfigurationBuilder().Add(configProvider).Build();
        }
Esempio n. 26
0
 public static IConfiguration BuildConfigurations(this IConfigurationBuilder Builder)
 {
     return(JsonConfigurationProvider.BuildConfigurations());
 }
Esempio n. 27
0
        public static IServiceCollection AddJsonConfigurations(this IServiceCollection Services)
        {
            Services.AddSingleton(JsonConfigurationProvider.BuildConfigurations());

            return(Services);
        }
Esempio n. 28
0
 /// <summary>
 /// Initializes a new instance of the <see cref="UserSecretsSecretProvider"/> class.
 /// </summary>
 public UserSecretsSecretProvider(JsonConfigurationProvider jsonProvider)
 {
     Guard.NotNull(jsonProvider, nameof(jsonProvider));
     _jsonProvider = jsonProvider;
 }
Esempio n. 29
0
        public void PropertiesAreSortedByNumberOnlyFirst()
        {
            var json = @"{
                'setting': {
                    'hello': 'a',
                    'bob': 'b',
                    '42': 'c',
                    '4':'d',
                    '10': 'e',
                    '1text': 'f',
                }
            }";

            var jsonConfigSource = new JsonConfigurationProvider(TestStreamHelpers.ArbitraryFilePath);
            jsonConfigSource.Load(TestStreamHelpers.StringToStream(json));

            var builder = new ConfigurationBuilder();
            builder.Add(jsonConfigSource, load: false);
            var config = builder.Build();

            var configurationSection = config.GetSection("setting");
            var indexConfigurationSections = configurationSection.GetChildren().ToArray();

            Assert.Equal(6, indexConfigurationSections.Count());
            Assert.Equal("4", indexConfigurationSections[0].Key);
            Assert.Equal("10", indexConfigurationSections[1].Key);
            Assert.Equal("42", indexConfigurationSections[2].Key);
            Assert.Equal("1text", indexConfigurationSections[3].Key);
            Assert.Equal("bob", indexConfigurationSections[4].Key);
            Assert.Equal("hello", indexConfigurationSections[5].Key);
        }
Esempio n. 30
0
 public Configuration(string path)
 {
     _provider = new JsonConfigurationProvider(new JsonConfigurationSource {
         Path = Path.GetFullPath(path)
     });;
 }
        public void Load_ThrowsAnException_WhenFileContentIsInvliadJsonData()
        {
            var provider = new JsonConfigurationProvider("error.json");

            Assert.Throws <JsonSerializationException>(() => provider.Load().ToList());
        }
Esempio n. 32
0
        public void ArraysAreKeptInFileOrder()
        {
            var json = @"{
                'setting': [
                    'b',
                    'a',
                    '2'
                ]
            }";

            var jsonConfigSource = new JsonConfigurationProvider(TestStreamHelpers.ArbitraryFilePath);
            jsonConfigSource.Load(TestStreamHelpers.StringToStream(json));

            var builder = new ConfigurationBuilder();
            builder.Add(jsonConfigSource, load: false);
            var config = builder.Build();

            var configurationSection = config.GetSection("setting");
            var indexConfigurationSections = configurationSection.GetChildren().ToArray();

            Assert.Equal(3, indexConfigurationSections.Count());
            Assert.Equal("b", indexConfigurationSections[0].Value);
            Assert.Equal("a", indexConfigurationSections[1].Value);
            Assert.Equal("2", indexConfigurationSections[2].Value);
        }