コード例 #1
0
        private static IEnumerable <KeyValuePair <string, ConfigurationValue> > ToDictionary(JObject jObj)
        {
            var currentPropertyName = string.Empty;

            using (var reader = new JTokenReader(jObj))
            {
                while (reader.Read())
                {
                    switch (reader.TokenType)
                    {
                    case JsonToken.PropertyName:
                        currentPropertyName = reader.CurrentPropertyName();
                        break;

                    case JsonToken.StartObject:
                        if (string.IsNullOrEmpty(currentPropertyName))
                        {
                            continue;
                        }
                        var childObj = JObject.Load(reader);
                        yield return(new KeyValuePair <string, ConfigurationValue>(
                                         currentPropertyName, new JsonConfigurationMap(childObj)));

                        break;

                    case JsonToken.StartArray:
                        if (string.IsNullOrEmpty(currentPropertyName))
                        {
                            continue;
                        }
                        var childArr = JArray.Load(reader);
                        yield return(new KeyValuePair <string, ConfigurationValue>(
                                         currentPropertyName, new JsonConfigurationCollection(childArr)));

                        break;

                    case JsonToken.Float:
                    case JsonToken.Integer:
                    case JsonToken.String:
                        if (string.IsNullOrEmpty(currentPropertyName))
                        {
                            continue;
                        }
                        var childValue = reader.CurrentToken;
                        yield return(new KeyValuePair <string, ConfigurationValue>(
                                         currentPropertyName, new JsonConfigurationValue(childValue)));

                        break;
                    }
                }
            }
        }