コード例 #1
0
        public static JsonKeyValuePairs MergeKeyValuePairs(JsonKeyValuePairs set1, JsonKeyValuePairs set2)
        {
            var result = new JsonKeyValuePairs();

            foreach (var key in set1.Keys)
            {
                result.Add(key, set1[key]);
            }

            foreach (var key in set2.Keys)
            {
                if (set1.ContainsKey(key))
                {
                    if (set1[key].IsA <JsonKeyValuePairs>() && set2[key].IsA <JsonKeyValuePairs>())
                    {
                        var subset1 = (JsonKeyValuePairs)set1[key];
                        var subset2 = (JsonKeyValuePairs)set2[key];
                        result[key] = MergeKeyValuePairs(subset1, subset2);
                    }
                    else
                    {
                        // duplicate
                    }
                }
                else
                {
                    result.Add(key, set2[key]);
                }
            }

            return(result);
        }
コード例 #2
0
        private static JsonKeyValuePairs FlattenJson(JsonKeyValuePairs json, string rootKey)
        {
            var flat = new JsonKeyValuePairs();

            foreach (var key in json.Keys)
            {
                var value  = json[key];
                var newKey = rootKey + (string.IsNullOrEmpty(rootKey) ? "" : ".") + key;
                if (value.IsA <JsonKeyValuePairs>())
                {
                    var flattened = FlattenJson((JsonKeyValuePairs)value, newKey);
                    flat = JsonHelper.MergeKeyValuePairs(flat, flattened);
                }
                else
                {
                    flat.Add(newKey, value);
                }
            }

            return(flat);
        }