示例#1
0
        /// <see cref="IJsonPlusNode.Clone(IJsonPlusNode)"/>
        public IJsonPlusNode Clone(IJsonPlusNode newParent)
        {
            JsonPlusArray clone = new JsonPlusArray(newParent);

            clone.AddRange(this);
            return(clone);
        }
示例#2
0
        private static void Flatten(IJsonPlusNode node)
        {
            if (!(node is JsonPlusValue v))
            {
                return;
            }

            switch (v.Type)
            {
            case JsonPlusType.Object:
                JsonPlusObject o = v.GetObject();
                v.Clear();
                v.Add(o);
                foreach (JsonPlusObjectMember item in o.Values)
                {
                    Flatten(item);
                }
                break;

            case JsonPlusType.Array:
                List <IJsonPlusNode> a = v.GetArray();
                v.Clear();
                JsonPlusArray newArray = new JsonPlusArray(v);
                foreach (IJsonPlusNode item in a)
                {
                    Flatten(item);
                    newArray.Add(item);
                }
                v.Add(newArray);
                break;

            case JsonPlusType.Literal:
                if (v.Count == 1)
                {
                    return;
                }

                string value = v.GetString();
                v.Clear();
                if (value == null)
                {
                    v.Add(new NullValue(v));
                }
                else if (value.NeedTripleQuotes())
                {
                    v.Add(new TripleQuotedStringValue(v, value));
                }
                else if (value.NeedQuotes())
                {
                    v.Add(new QuotedStringValue(v, value));
                }
                else
                {
                    v.Add(new UnquotedStringValue(v, value));
                }
                break;
            }
        }
示例#3
0
        private IJsonPlusNode ParseSelfAssignArray(IJsonPlusNode owner)
        {
            // sanity check
            if (_tokens.Current.Type != TokenType.SelfAssignment)
            {
                throw JsonPlusParserException.Create(_tokens.Current, Path, string.Format(RS.UnexpectedTokenInArray, TokenType.SelfAssignment, _tokens.Current.Type));
            }

            // consume += operator token
            ConsumeWhitelines();

            JsonPlusArray currentArray = new JsonPlusArray(owner);

            switch (_tokens.Current.Type)
            {
            case TokenType.Include:
            case TokenType.OptionalInclude:
                // #todo
                currentArray.Add(ParseInclude(currentArray));
                break;

            case TokenType.StartOfArray:
                return(ParseArray(owner));

            case TokenType.StartOfObject:
                // #todo
                currentArray.Add(ParseObject(currentArray));
                break;

            case TokenType.LiteralValue:
                if (_tokens.Current.IsNonSignificant())
                {
                    ConsumeWhitelines();
                }
                if (_tokens.Current.Type != TokenType.LiteralValue)
                {
                    break;
                }

                currentArray.Add(ParseValue(currentArray));
                return(currentArray);

            case TokenType.OptionalSubstitution:
            case TokenType.Substitution:
                JsonPlusPath         pointerPath = JsonPlusPath.Parse(_tokens.Current.Value);
                JsonPlusSubstitution sub         = new JsonPlusSubstitution(owner, pointerPath, _tokens.Current,
                                                                            _tokens.Current.Type == TokenType.Substitution);
                _substitutions.Add(sub);
                _tokens.Next();
                return(sub);

            default:
                throw JsonPlusParserException.Create(_tokens.Current, Path, string.Format(RS.UnexpectedTokenInArray, TokenType.EndOfArray, _tokens.Current.Type));
            }

            return(currentArray);
        }
示例#4
0
        private bool Equals(JsonPlusArray other)
        {
            if (Count != other.Count)
            {
                return(false);
            }

            for (int i = 0; i < Count; ++i)
            {
                if (!Equals(this[i], other[i]))
                {
                    return(false);
                }
            }
            return(true);
        }
示例#5
0
        /// <summary>
        /// Retrieves the next array token from the tokenizer.
        /// </summary>
        /// <returns>An array of elements retrieved from the token.</returns>
        private JsonPlusArray ParseArray(IJsonPlusNode owner)
        {
            JsonPlusArray currentArray = new JsonPlusArray(owner);

            // consume start of array token
            ConsumeWhitelines();

            IJsonPlusNode lastValue = null;
            bool          parsing   = true;

            while (parsing)
            {
                switch (_tokens.Current.Type)
                {
                case TokenType.Include:
                case TokenType.OptionalInclude:
                    if (lastValue != null)
                    {
                        throw JsonPlusParserException.Create(_tokens.Current, Path,
                                                             string.Format(RS.UnexpectedTokenWith2AltInArray,
                                                                           TokenType.ArraySeparator, TokenType.EndOfLine,
                                                                           _tokens.Current.Type));
                    }

                    lastValue = ParseInclude(currentArray);
                    break;

                case TokenType.StartOfArray:
                    if (lastValue != null)
                    {
                        throw JsonPlusParserException.Create(_tokens.Current, Path,
                                                             string.Format(RS.UnexpectedTokenWith2AltInArray,
                                                                           TokenType.ArraySeparator, TokenType.EndOfLine,
                                                                           _tokens.Current.Type));
                    }

                    // Array inside of arrays are parsed as values because it can be value concatenated with another array.
                    lastValue = ParseValue(currentArray);
                    break;

                case TokenType.StartOfObject:
                    if (lastValue != null)
                    {
                        throw JsonPlusParserException.Create(_tokens.Current, Path,
                                                             string.Format(RS.UnexpectedTokenWith2AltInArray,
                                                                           TokenType.ArraySeparator, TokenType.EndOfLine,
                                                                           _tokens.Current.Type));
                    }

                    lastValue = ParseObject(currentArray);
                    break;

                case TokenType.LiteralValue:
                    if (_tokens.Current.IsNonSignificant())
                    {
                        ConsumeWhitespace();
                    }
                    if (_tokens.Current.Type != TokenType.LiteralValue)
                    {
                        break;
                    }

                    if (lastValue != null)
                    {
                        throw JsonPlusParserException.Create(_tokens.Current, Path,
                                                             string.Format(RS.UnexpectedTokenWith2AltInArray,
                                                                           TokenType.ArraySeparator, TokenType.EndOfLine,
                                                                           _tokens.Current.Type));
                    }

                    lastValue = ParseValue(currentArray);
                    break;

                case TokenType.OptionalSubstitution:
                case TokenType.Substitution:
                    if (lastValue != null)
                    {
                        throw JsonPlusParserException.Create(_tokens.Current, Path,
                                                             string.Format(RS.UnexpectedTokenWith2AltInArray,
                                                                           TokenType.ArraySeparator, TokenType.EndOfLine,
                                                                           _tokens.Current.Type));
                    }

                    lastValue = ParseValue(currentArray);

                    break;

                case TokenType.Comment:
                case TokenType.EndOfLine:
                    if (lastValue == null)
                    {
                        ConsumeWhitelines();
                        break;
                    }

                    currentArray.Add(lastValue);
                    lastValue = null;
                    ConsumeWhitelines();
                    break;

                case TokenType.ArraySeparator:
                    if (lastValue == null)
                    {
                        throw JsonPlusParserException.Create(_tokens.Current, Path, string.Format(RS.BadArrayType, _tokens.Current.Type));
                    }

                    currentArray.Add(lastValue);
                    lastValue = null;
                    ConsumeWhitelines();
                    break;

                case TokenType.EndOfArray:
                    if (lastValue != null)
                    {
                        currentArray.Add(lastValue);
                        lastValue = null;
                    }
                    parsing = false;
                    break;

                default:
                    throw JsonPlusParserException.Create(_tokens.Current, Path, string.Format(RS.UnexpectedTokenInArray, TokenType.EndOfArray, _tokens.Current.Type));
                }
            }

            // Consume end of array token
            _tokens.Next();
            return(currentArray);
        }