Esempio n. 1
0
        internal void AddValue(JValue value, JsonToken token)
        {
            if (_parent != null)
            {
                _parent.Add(value);
                CurrentToken = _parent.Last;

                if (_parent.Type == JTokenType.Property)
                {
                    _parent = _parent.Parent;
                }
            }
            else
            {
                _value       = value ?? JValue.CreateNull();
                CurrentToken = _value;
            }
        }
Esempio n. 2
0
        internal static void MergeEnumerableContent(JContainer target, IEnumerable content, JsonMergeSettings settings)
        {
            switch (settings.MergeArrayHandling)
            {
            case MergeArrayHandling.Concat:
                foreach (JToken item in content)
                {
                    target.Add(item);
                }
                break;

            case MergeArrayHandling.Union:
                var items = new HashSet <JToken>(target, EqualityComparer);
                foreach (JToken item in content)
                {
                    if (items.Add(item))
                    {
                        target.Add(item);
                    }
                }
                break;

            case MergeArrayHandling.Replace:
                target.ClearItems();
                foreach (JToken item in content)
                {
                    target.Add(item);
                }
                break;

            case MergeArrayHandling.Merge:
                var i = 0;
                foreach (var targetItem in content)
                {
                    if (i < target.Count)
                    {
                        var sourceItem        = target[i];
                        var existingContainer = sourceItem as JContainer;
                        if (existingContainer != null)
                        {
                            existingContainer.Merge(targetItem, settings);
                        }
                        else
                        {
                            if (targetItem != null)
                            {
                                var contentValue = CreateFromContent(targetItem);
                                if (contentValue.Type != JTokenType.Null)
                                {
                                    target[i] = contentValue;
                                }
                            }
                        }
                    }
                    else
                    {
                        target.Add(targetItem);
                    }

                    i++;
                }
                break;

            default:
                throw new ArgumentOutOfRangeException("settings", "Unexpected merge array handling when merging JSON.");
            }
        }