コード例 #1
0
ファイル: JObject.cs プロジェクト: cs130-w21/13
        internal override void MergeItem(object content, JsonMergeSettings settings)
        {
            JObject jobject = content as JObject;

            if (jobject == null)
            {
                return;
            }
            foreach (KeyValuePair <string, JToken> keyValuePair in jobject)
            {
                JProperty jproperty = this.Property(keyValuePair.Key);
                if (jproperty == null)
                {
                    this.Add(keyValuePair.Key, keyValuePair.Value);
                }
                else if (keyValuePair.Value != null)
                {
                    JContainer jcontainer = jproperty.Value as JContainer;
                    if (jcontainer == null || jcontainer.Type != keyValuePair.Value.Type)
                    {
                        if (keyValuePair.Value.Type != JTokenType.Null || settings != null && settings.MergeNullValueHandling == MergeNullValueHandling.Merge)
                        {
                            jproperty.Value = keyValuePair.Value;
                        }
                    }
                    else
                    {
                        jcontainer.Merge((object)keyValuePair.Value, settings);
                    }
                }
            }
        }
コード例 #2
0
ファイル: JContainer.cs プロジェクト: cs130-w21/13
        internal static void MergeEnumerableContent(
            JContainer target,
            IEnumerable content,
            JsonMergeSettings settings)
        {
            switch (settings.MergeArrayHandling)
            {
            case MergeArrayHandling.Concat:
                IEnumerator enumerator1 = content.GetEnumerator();
                try {
                    while (enumerator1.MoveNext())
                    {
                        JToken current = (JToken)enumerator1.Current;
                        target.Add((object)current);
                    }

                    break;
                } finally {
                    (enumerator1 as IDisposable)?.Dispose();
                }

            case MergeArrayHandling.Union:
                Dictionary <JToken, bool> dictionary =
                    new Dictionary <JToken, bool>((IEqualityComparer <JToken>)JToken.EqualityComparer);
                foreach (JToken index in (IEnumerable <JToken>)target)
                {
                    dictionary[index] = true;
                }
                IEnumerator enumerator2 = content.GetEnumerator();
                try {
                    while (enumerator2.MoveNext())
                    {
                        JToken current = (JToken)enumerator2.Current;
                        if (!dictionary.ContainsKey(current))
                        {
                            dictionary[current] = true;
                            target.Add((object)current);
                        }
                    }

                    break;
                } finally {
                    (enumerator2 as IDisposable)?.Dispose();
                }

            case MergeArrayHandling.Replace:
                target.ClearItems();
                IEnumerator enumerator3 = content.GetEnumerator();
                try {
                    while (enumerator3.MoveNext())
                    {
                        JToken current = (JToken)enumerator3.Current;
                        target.Add((object)current);
                    }

                    break;
                } finally {
                    (enumerator3 as IDisposable)?.Dispose();
                }

            case MergeArrayHandling.Merge:
                int         num         = 0;
                IEnumerator enumerator4 = content.GetEnumerator();
                try {
                    while (enumerator4.MoveNext())
                    {
                        object current = enumerator4.Current;
                        if (num < target.Count)
                        {
                            JContainer jcontainer = target[(object)num] as JContainer;
                            if (jcontainer != null)
                            {
                                jcontainer.Merge(current, settings);
                            }
                            else if (current != null)
                            {
                                JToken fromContent = JContainer.CreateFromContent(current);
                                if (fromContent.Type != JTokenType.Null)
                                {
                                    target[(object)num] = fromContent;
                                }
                            }
                        }
                        else
                        {
                            target.Add(current);
                        }

                        ++num;
                    }

                    break;
                } finally {
                    (enumerator4 as IDisposable)?.Dispose();
                }

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