Ejemplo n.º 1
0
        private object _Parse(Lite.LiteJsonTokenizer tokenizer)
        {
            if (tokenizer == null)
            {
                return(null);
            }

            object topNode = null;
            var    type    = PeekAndGetType(tokenizer);

            if (type == TokenType.EOF || type == TokenType.LCURLY || type == TokenType.LSQUARE)
            {
                if (type == TokenType.EOF)
                {
                    topNode = ProduceJsonNull(tokenizer);
                }
                else if (type == TokenType.LCURLY)
                {
                    topNode = ProduceJsonObject(tokenizer);
                }
                else if (type == TokenType.LSQUARE)
                {
                    topNode = ProduceJsonArray(tokenizer);
                }
            }
            else
            {
                // ???
                throw new DotJsonMiniException("Json string should be object or Array. Input tokenType = " + TokenTypes.GetDisplayName(type));
            }

            System.Diagnostics.Debug.WriteLine("topnNode = " + topNode);
            return(topNode);
        }
Ejemplo n.º 2
0
        private async Task <object> _parseAsync(JsonTokenizer tokenizer, int depth, JsonTokenBuffer tokenTailBuffer, ObjectTailBuffer nodeTailBuffer)
        {
            object topNode = null;
            var    type    = PeekAndGetType(tokenizer, tokenTailBuffer, nodeTailBuffer);

            if (type == TokenType.EOF || type == TokenType.LCURLY || type == TokenType.LSQUARE)
            {
                if (type == TokenType.EOF)
                {
                    topNode = ProduceJsonNull(tokenizer, tokenTailBuffer, nodeTailBuffer);
                }
                else if (type == TokenType.LCURLY)
                {
                    topNode = await ProduceJsonObjectAsync(tokenizer, depth, tokenTailBuffer, nodeTailBuffer);
                }
                else if (type == TokenType.LSQUARE)
                {
                    topNode = await ProduceJsonArrayAsync(tokenizer, depth, tokenTailBuffer, nodeTailBuffer);
                }
            }
            else
            {
                // TBD:
                // Process it here if parserPolicy.AllowLeadingJsonMarker == true,
                // ???
                if (parserPolicy.AllowNonObjectOrNonArray)
                {
                    // This is actually error according to json.org JSON grammar.
                    // But, we allow partial JSON string.
                    switch (type)
                    {
                    case TokenType.NULL:
                        topNode = ProduceJsonNull(tokenizer, tokenTailBuffer, nodeTailBuffer);
                        break;

                    case TokenType.BOOLEAN:
                        topNode = ProduceJsonBoolean(tokenizer, tokenTailBuffer, nodeTailBuffer);
                        break;

                    case TokenType.NUMBER:
                        topNode = ProduceJsonNumber(tokenizer, tokenTailBuffer, nodeTailBuffer);
                        break;

                    case TokenType.STRING:
                        // log.warning(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
                        topNode = ProduceJsonString(tokenizer, tokenTailBuffer, nodeTailBuffer);
                        // log.warning(">>>>>>>>>>>>>>>>>>>>>>>>>>>>> topNode = " + topNode);
                        break;

                    default:
                        // ???
                        throw new InvalidJsonTokenException("JsonToken not recognized: tokenType = " + TokenTypes.GetDisplayName(type) + "; " + tokenTailBuffer.ToTraceString(), GetTailCharStream(tokenizer), PeekCharStream(tokenizer));
                    }
                }
                else
                {
                    // TBD
                    // this is a bit too lenient probably...
                    // there was some special char sequence which some parsers allowed, which I cannot remember..
                    // For now, if parserPolicy.AllowLeadingJsonMarker == true is interpreted as allowLeadingNonObjectNonArrayChars....
                    //    --> we remove all leading chars until we reach { or [.
                    if (parserPolicy.AllowLeadingJsonMarker)
                    {
                        while (type != TokenType.LCURLY && type != TokenType.LSQUARE)
                        {
                            JsonToken t = tokenizer.Next(); // swallow one token.
                            if (TracingEnabled)
                            {
                                tokenTailBuffer.Push(t);
                            }
                            type = PeekAndGetType(tokenizer, tokenTailBuffer, nodeTailBuffer);
                        }
                        if (type == TokenType.LCURLY)
                        {
                            topNode = await ProduceJsonObjectAsync(tokenizer, depth, tokenTailBuffer, nodeTailBuffer);
                        }
                        else if (type == TokenType.LSQUARE)
                        {
                            topNode = await ProduceJsonArrayAsync(tokenizer, depth, tokenTailBuffer, nodeTailBuffer);
                        }
                        else
                        {
                            // ???
                            throw new InvalidJsonTokenException("Invalid input Json string. " + tokenTailBuffer.ToTraceString(), GetTailCharStream(tokenizer), PeekCharStream(tokenizer));
                        }
                    }
                    else
                    {
                        // ???
                        throw new InvalidJsonTokenException("Json string should be Object or Array. Input tokenType = " + TokenTypes.GetDisplayName(type) + "; " + tokenTailBuffer.ToTraceString(), GetTailCharStream(tokenizer), PeekCharStream(tokenizer));
                    }
                }
            }

            //if (log.IsLoggable(Level.FINE)) {
            //    log.fine("topnNode = " + topNode);
            //}
            return(topNode);
        }