Esempio n. 1
0
        static SLJsonNode ParseArray(SLJsonTokenizer tokenizer)
        {
            var res = new SLJsonNode();

            res.MakeArray();
            bool needSep = false;

            while (true)
            {
                tokenizer.ReadNext();

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

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

                    tokenizer.ReadNext();
                }

                SLJsonNode value = ParseValue(tokenizer);

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

                needSep = true;
            }
        }
Esempio n. 2
0
        SLJsonNode ParseObject(SLJsonTokenizer tokenizer)
        {
            var res = new SLJsonNode();

            res.MakeObject();
            bool needSep = false;

            while (true)
            {
                tokenizer.ReadNext();

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

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

                    tokenizer.ReadNext();
                }

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

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

                string field = tokenizer.Token;

                tokenizer.ReadNext();
                if (tokenizer.SpecialChar != ':')
                {
                    throw new SLJsonException("Colon expected");
                }

                tokenizer.ReadNext();

                SLJsonNode value = ParseValue(tokenizer);

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

                needSep = true;
            }
        }
Esempio n. 3
0
        SLJsonNode ParseRoot(string jsonExpression, bool allowArraysAndValues)
        {
            var tokenizer = new SLJsonTokenizer(jsonExpression);

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

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

                    res = ParseObject(tokenizer);
                }

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