Ejemplo n.º 1
0
        internal bool ContentsEqual(BacktraceJContainer container)
        {
            if (container == this)
            {
                return(true);
            }

            IList <JToken> t1 = ChildrenTokens;
            IList <JToken> t2 = container.ChildrenTokens;

            if (t1.Count != t2.Count)
            {
                return(false);
            }

            for (int i = 0; i < t1.Count; i++)
            {
                if (!t1[i].DeepEquals(t2[i]))
                {
                    return(false);
                }
            }

            return(true);
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="JTokenWriter"/> class writing to the given <see cref="BacktraceJContainer"/>.
        /// </summary>
        /// <param name="container">The container being written to.</param>
        public JTokenWriter(BacktraceJContainer container)
        {
            ValidationUtils.ArgumentNotNull(container, nameof(container));

            _token  = container;
            _parent = container;
        }
        private void RemoveParent()
        {
            _current = _parent;
            _parent  = _parent.Parent;

            if (_parent != null && _parent.Type == JTokenType.Property)
            {
                _parent = _parent.Parent;
            }
        }
Ejemplo n.º 4
0
        internal BacktraceJContainer(BacktraceJContainer other)
            : this()
        {
            ValidationUtils.ArgumentNotNull(other, "other");

            int i = 0;

            foreach (JToken child in other)
            {
                AddInternal(i, child, false);
                i++;
            }
        }
        private void AddParent(BacktraceJContainer container)
        {
            if (_parent == null)
            {
                _token = container;
            }
            else
            {
                _parent.AddAndSkipParentCheck(container);
            }

            _parent  = container;
            _current = container;
        }
Ejemplo n.º 6
0
        private bool SetEnd(BacktraceJContainer c)
        {
            JsonToken?endToken = GetEndToken(c);

            if (endToken != null)
            {
                SetToken(endToken.GetValueOrDefault());
                _current = c;
                _parent  = c;
                return(true);
            }
            else
            {
                return(ReadOver(c));
            }
        }
Ejemplo n.º 7
0
        private bool ReadInto(BacktraceJContainer c)
        {
            JToken firstChild = c.First;

            if (firstChild == null)
            {
                return(SetEnd(c));
            }
            else
            {
                SetToken(firstChild);
                _current = firstChild;
                _parent  = c;
                return(true);
            }
        }
        internal override void WriteToken(JsonReader reader, bool writeChildren, bool writeDateConstructorAsDate, bool writeComments)
        {
            JTokenReader tokenReader = reader as JTokenReader;

            // closing the token wrather than reading then writing it doesn't lose some type information, e.g. Guid, byte[], etc
            if (tokenReader != null && writeChildren && writeDateConstructorAsDate && writeComments)
            {
                if (tokenReader.TokenType == JsonToken.None)
                {
                    if (!tokenReader.Read())
                    {
                        return;
                    }
                }

                JToken value = tokenReader.CurrentToken.CloneToken();

                if (_parent != null)
                {
                    _parent.Add(value);
                    _current = _parent.Last;

                    // if the writer was in a property then move out of it and up to its parent object
                    if (_parent.Type == JTokenType.Property)
                    {
                        _parent = _parent.Parent;
                        InternalWriteValue(JsonToken.Null);
                    }
                }
                else
                {
                    _current = value;

                    if (_token == null && _value == null)
                    {
                        _token = value as BacktraceJContainer;
                        _value = value as JValue;
                    }
                }

                tokenReader.Skip();
            }
            else
            {
                base.WriteToken(reader, writeChildren, writeDateConstructorAsDate, writeComments);
            }
        }
        internal void AddValue(JValue value, JsonToken token)
        {
            if (_parent != null)
            {
                _parent.Add(value);
                _current = _parent.Last;

                if (_parent.Type == JTokenType.Property)
                {
                    _parent = _parent.Parent;
                }
            }
            else
            {
                _value   = value ?? JValue.CreateNull();
                _current = _value;
            }
        }
Ejemplo n.º 10
0
        private JsonToken?GetEndToken(BacktraceJContainer c)
        {
            switch (c.Type)
            {
            case JTokenType.Object:
                return(JsonToken.EndObject);

            case JTokenType.Array:
                return(JsonToken.EndArray);

            case JTokenType.Constructor:
                return(JsonToken.EndConstructor);

            case JTokenType.Property:
                return(null);

            default:
                throw MiscellaneousUtils.CreateArgumentOutOfRangeException("Type", c.Type, "Unexpected JContainer type.");
            }
        }
Ejemplo n.º 11
0
        internal IEnumerable <JToken> GetDescendants(bool self)
        {
            if (self)
            {
                yield return(this);
            }

            foreach (JToken o in ChildrenTokens)
            {
                yield return(o);

                BacktraceJContainer c = o as BacktraceJContainer;
                if (c != null)
                {
                    foreach (JToken d in c.Descendants())
                    {
                        yield return(d);
                    }
                }
            }
        }
Ejemplo n.º 12
0
        internal override void MergeItem(object content, JsonMergeSettings settings)
        {
            BacktraceJObject o = content as BacktraceJObject;

            if (o == null)
            {
                return;
            }

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

                if (existingProperty == null)
                {
                    Add(contentItem.Key, contentItem.Value);
                }
                else if (contentItem.Value != null)
                {
                    BacktraceJContainer existingContainer = existingProperty.Value as BacktraceJContainer;
                    if (existingContainer == null)
                    {
                        if (contentItem.Value.Type != JTokenType.Null || (settings != 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);
                    }
                }
            }
        }
Ejemplo n.º 13
0
        /// <summary>
        /// Reads the next JSON token from the stream.
        /// </summary>
        /// <returns>
        /// true if the next token was read successfully; false if there are no more tokens to read.
        /// </returns>
        public override bool Read()
        {
            if (CurrentState != State.Start)
            {
                if (_current == null)
                {
                    return(false);
                }

                BacktraceJContainer container = _current as BacktraceJContainer;
                if (container != null && _parent != container)
                {
                    return(ReadInto(container));
                }
                else
                {
                    return(ReadOver(_current));
                }
            }

            _current = _root;
            SetToken(_current);
            return(true);
        }
Ejemplo n.º 14
0
        internal void ReadContentFrom(JsonReader r, JsonLoadSettings settings)
        {
            ValidationUtils.ArgumentNotNull(r, "r");
            IJsonLineInfo lineInfo = r as IJsonLineInfo;

            BacktraceJContainer parent = this;

            do
            {
                if (parent is BacktraceJProperty && (parent as BacktraceJProperty).Value != null)
                {
                    if (parent == this)
                    {
                        return;
                    }

                    parent = parent.Parent;
                }

                switch (r.TokenType)
                {
                case JsonToken.None:
                    // new reader. move to actual content
                    break;

                case JsonToken.StartArray:
                    JArray a = new JArray();
                    a.SetLineInfo(lineInfo, settings);
                    parent.Add(a);
                    parent = a;
                    break;

                case JsonToken.EndArray:
                    if (parent == this)
                    {
                        return;
                    }

                    parent = parent.Parent;
                    break;

                case JsonToken.StartObject:
                    BacktraceJObject o = new BacktraceJObject();
                    o.SetLineInfo(lineInfo, settings);
                    parent.Add(o);
                    parent = o;
                    break;

                case JsonToken.EndObject:
                    if (parent == this)
                    {
                        return;
                    }

                    parent = parent.Parent;
                    break;

                case JsonToken.StartConstructor:
                    JConstructor constructor = new JConstructor(r.Value.ToString());
                    constructor.SetLineInfo(lineInfo, settings);
                    parent.Add(constructor);
                    parent = constructor;
                    break;

                case JsonToken.EndConstructor:
                    if (parent == this)
                    {
                        return;
                    }

                    parent = parent.Parent;
                    break;

                case JsonToken.String:
                case JsonToken.Integer:
                case JsonToken.Float:
                case JsonToken.Date:
                case JsonToken.Boolean:
                case JsonToken.Bytes:
                    JValue v = new JValue(r.Value);
                    v.SetLineInfo(lineInfo, settings);
                    parent.Add(v);
                    break;

                case JsonToken.Comment:
                    if (settings != null && settings.CommentHandling == CommentHandling.Load)
                    {
                        v = JValue.CreateComment(r.Value.ToString());
                        v.SetLineInfo(lineInfo, settings);
                        parent.Add(v);
                    }
                    break;

                case JsonToken.Null:
                    v = JValue.CreateNull();
                    v.SetLineInfo(lineInfo, settings);
                    parent.Add(v);
                    break;

                case JsonToken.Undefined:
                    v = JValue.CreateUndefined();
                    v.SetLineInfo(lineInfo, settings);
                    parent.Add(v);
                    break;

                case JsonToken.PropertyName:
                    string             propertyName = r.Value.ToString();
                    BacktraceJProperty property     = new BacktraceJProperty(propertyName);
                    property.SetLineInfo(lineInfo, settings);
                    BacktraceJObject parentObject = (BacktraceJObject)parent;
                    // handle multiple properties with the same name in JSON
                    BacktraceJProperty existingPropertyWithName = parentObject.Property(propertyName);
                    if (existingPropertyWithName == null)
                    {
                        parent.Add(property);
                    }
                    else
                    {
                        existingPropertyWithName.Replace(property);
                    }
                    parent = property;
                    break;

                default:
                    throw new InvalidOperationException("The JsonReader should not be on a token of type {0}.".FormatWith(CultureInfo.InvariantCulture, r.TokenType));
                }
            } while (r.Read());
        }
Ejemplo n.º 15
0
        internal static void MergeEnumerableContent(BacktraceJContainer 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
                Dictionary <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];

                        BacktraceJContainer existingContainer = sourceItem as BacktraceJContainer;
                        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.");
            }
        }