Example #1
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.");
            }
        }