Пример #1
0
        internal override void MergeItem(object content, JsonMergeSettings settings)
        {
            JConstructor c = content as JConstructor;
            if (c == null)
                return;

            if (c.Name != null)
                Name = c.Name;
            MergeEnumerableContent(this, c, settings);
        }
Пример #2
0
        internal override void MergeItem(object content, JsonMergeSettings settings)
        {
            JObject o = content as JObject;
            if (o == null)
            {
                return;
            }

            foreach (KeyValuePair<string, JToken> contentItem in o)
            {
                JProperty existingProperty = Property(contentItem.Key);

                if (existingProperty == null)
                {
                    Add(contentItem.Key, contentItem.Value);
                }
                else if (contentItem.Value != null)
                {
                    JContainer existingContainer = existingProperty.Value as JContainer;
                    if (existingContainer == null)
                    {
                        if (contentItem.Value.Type != JTokenType.Null || settings?.MergeNullValueHandling == MergeNullValueHandling.Merge)
                        {
                            existingProperty.Value = contentItem.Value;
                        }
                    }
                    else if (existingContainer.Type != contentItem.Value.Type)
                    {
                        existingProperty.Value = contentItem.Value;
                    }
                    else
                    {
                        existingContainer.Merge(contentItem.Value, settings);
                    }
                }
            }
        }
Пример #3
0
 /// <summary>
 /// Merge the specified content into this <see cref="JToken"/> using <see cref="JsonMergeSettings"/>.
 /// </summary>
 /// <param name="content">The content to be merged.</param>
 /// <param name="settings">The <see cref="JsonMergeSettings"/> used to merge the content.</param>
 public void Merge(object content, JsonMergeSettings settings)
 {
     MergeItem(content, settings);
 }
Пример #4
0
 internal abstract void MergeItem(object content, JsonMergeSettings settings);
Пример #5
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:
#if !NET20
                HashSet <JToken> items = new HashSet <JToken>(target, EqualityComparer);

                foreach (JToken item in content)
                {
                    if (items.Add(item))
                    {
                        target.Add(item);
                    }
                }
#else
                IDictionary <JToken, bool> items = new Dictionary <JToken, bool>(EqualityComparer);
                foreach (JToken t in target)
                {
                    items[t] = true;
                }

                foreach (JToken item in content)
                {
                    if (!items.ContainsKey(item))
                    {
                        items[item] = true;
                        target.Add(item);
                    }
                }
#endif
                break;

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

            case MergeArrayHandling.Merge:
                int i = 0;
                foreach (object targetItem in content)
                {
                    if (i < target.Count)
                    {
                        JToken sourceItem = target[i];

                        JContainer existingContainer = sourceItem as JContainer;
                        if (existingContainer != null)
                        {
                            existingContainer.Merge(targetItem, settings);
                        }
                        else
                        {
                            if (targetItem != null)
                            {
                                JToken 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.");
            }
        }