Пример #1
0
        JsonNode ParseArray(JsonTokenizer tokenizer)
        {
            var res = new JsonNode();

            res.MakeArray();
            bool needSep = false;

            while (true)
            {
                tokenizer.ReadNext();

                if (tokenizer.SpecialChar == ']')
                {
                    return(res);
                }

                if (needSep)
                {
                    if (tokenizer.SpecialChar != ',')
                    {
                        throw new JsonException("Separator expected");
                    }

                    tokenizer.ReadNext();
                }

                JsonNode value = ParseValue(tokenizer);

                res.m_Array.Add(value);
                value.AssignParent(res);

                needSep = true;
            }
        }
Пример #2
0
        JsonNode ParseObject(JsonTokenizer tokenizer)
        {
            var res = new JsonNode();

            res.MakeObject();
            bool needSep = false;

            while (true)
            {
                tokenizer.ReadNext();

                if (tokenizer.SpecialChar == '}')
                {
                    return(res);
                }

                if (needSep)
                {
                    if (tokenizer.SpecialChar != ',')
                    {
                        throw new JsonException("Separator expected");
                    }

                    tokenizer.ReadNext();
                }

                if (tokenizer.HasSpecialChar)
                {
                    throw new JsonException("Unexpected token");
                }

                if (!AreUnquotedNamesAllowed && !tokenizer.TokenIsString)
                {
                    throw new JsonException("String expected");
                }

                string field = tokenizer.Token;

                tokenizer.ReadColon();
                tokenizer.ReadNext();

                JsonNode value = ParseValue(tokenizer);

                res.m_Object[field] = value; // No exception for multiple fields with the same name
                value.AssignParent(res);

                needSep = true;
            }
        }
Пример #3
0
        JsonNode ParseRoot(string json, bool allowArraysAndValues)
        {
            var tokenizer = new JsonTokenizer(json);

            tokenizer.AreSingleQuotesEnabled = AreSingleQuotesAllowed;
            try
            {
                tokenizer.ReadNext();

                JsonNode res;
                if (allowArraysAndValues)
                {
                    res = ParseValue(tokenizer);
                }
                else
                {
                    if (!tokenizer.HasSpecialChar || tokenizer.SpecialChar != '{')
                    {
                        ThrowUnexpected(tokenizer);
                    }

                    res = ParseObject(tokenizer);
                }

                CheckEndOfExpression(tokenizer);
                return(res);
            }
            catch (JsonException e)
            {
                throw new JsonException(
                          "Syntax error in JSON expression at row " +
                          JsonConvert.ToString(tokenizer.CurrentRow) + " in column " +
                          JsonConvert.ToString(tokenizer.CurrentColumn) + ": " +
                          e.Message,
                          e);
            }
        }