コード例 #1
0
ファイル: JObject.cs プロジェクト: zouhunter/SAEA
        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)
                        {
                            existingProperty.Value = contentItem.Value;
                        }
                    }
                    else if (existingContainer.Type != contentItem.Value.Type)
                    {
                        existingProperty.Value = contentItem.Value;
                    }
                    else
                    {
                        existingContainer.Merge(contentItem.Value, settings);
                    }
                }
            }
        }
コード例 #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:
#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.");
            }
        }