コード例 #1
0
ファイル: JsonPlusPath.cs プロジェクト: lizoc/jsonplus
        internal static JsonPlusPath FromTokens(TokenizeResult tokens)
        {
            if (tokens == null)
            {
                throw new ArgumentNullException(nameof(tokens));
            }

            List <string> result = new List <string>();
            StringBuilder sb     = new StringBuilder();

            while (tokens.Current.Type == TokenType.LiteralValue)
            {
                switch (tokens.Current.LiteralType)
                {
                case LiteralTokenType.TripleQuotedLiteralValue:
                    throw JsonPlusParserException.Create(tokens.Current, null, RS.TripleQuoteUnsupportedInPath);

                case LiteralTokenType.QuotedLiteralValue:
                    // Normalize quoted keys, remove the quotes if the key doesn't need them.
                    //sb.Append(tokens.Current.Value.NeedQuotes() ? $"\"{tokens.Current.Value}\"" : tokens.Current.Value);
                    sb.Append(tokens.Current.Value);
                    break;

                default:
                    string[] split = tokens.Current.Value.Split('.');
                    for (int i = 0; i < split.Length - 1; ++i)
                    {
                        sb.Append(split[i]);
                        result.Add(sb.ToString());
                        sb.Clear();
                    }
                    sb.Append(split[split.Length - 1]);
                    break;
                }
                tokens.Next();
            }
            result.Add(sb.ToString());
            return(new JsonPlusPath(result));
        }
コード例 #2
0
        private void ParseTokens()
        {
            if (_tokens.Current.IsNonSignificant())
            {
                ConsumeWhitelines();
            }

            while (_tokens.Current.Type != TokenType.EndOfFile)
            {
                switch (_tokens.Current.Type)
                {
                case TokenType.Include:
                case TokenType.OptionalInclude:
                    _root.Add(ParseInclude(_root));
                    break;

                // may contain one array and one array only
                case TokenType.StartOfArray:
                    _root.Add(ParseArray(_root));
                    break;

                case TokenType.StartOfObject:
                    _root.Add(ParseObject(_root).GetObject());
                    break;

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

                    _root.Add(ParseObject(_root).GetObject());
                    break;

                case TokenType.Comment:
                case TokenType.EndOfLine:
                case TokenType.EndOfFile:
                case TokenType.EndOfObject:
                case TokenType.EndOfArray:
                    _tokens.Next();
                    break;

                default:
                    throw JsonPlusParserException.Create(_tokens.Current, null, string.Format(RS.IllegalTokenType, _tokens.Current.Type), null);
                }
            }
        }