Exemple #1
0
        /// <summary>
        /// Consume a JSON string token
        /// </summary>
        /// <param name="ctx"></param>
        /// <param name="throws"></param>
        /// <returns></returns>
        private static JsonParserContext ConsumeString(
            this JsonParserContext ctx,
            bool throws
            )
        {
            if (ctx.ConsumeWhiteSpace().ConsumeAnyChar("\"", throws).IsSucceeded)
            {
                JSonReader src = ctx.Begin();

                for (int p = src.Position, len = src.Text.Length; p < len; p++)
                {
                    if ((src.Text[p]) == '"')
                    {
                        ctx.SetResult(
                            new JValue { BoxedValue = src.Text.Substring(src.Position, p - src.Position) }
                            );

                        src.Position = p + 1;
                        break;
                    }
                }
            }

            return ctx;
        }
Exemple #2
0
        /// <summary>
        /// Consume a JSON object
        /// </summary>
        /// <param name="ctx"></param>
        /// <param name="throws"></param>
        /// <returns></returns>
        private static JsonParserContext ConsumeObject(
            this JsonParserContext ctx,
            bool throws
            )
        {
            if (ctx.ConsumeWhiteSpace().ConsumeAnyChar("{", throws).IsSucceeded)
            {
                var jctr = new JObject();
                bool shouldThrow = false;

                do
                {
                    ctx.Begin();
                    if (ctx.ConsumeString(shouldThrow).IsSucceeded)
                    {
                        var name = (string)(JValue)ctx.Result;
                        ctx.ConsumeWhiteSpace()
                            .ConsumeAnyChar(":", true)
                            .ConsumeValue(true);

                        jctr.Add(name, (JToken)ctx.Result);
                    }

                    shouldThrow = true;
                }
                while ((char)ctx.ConsumeWhiteSpace().ConsumeAnyChar(",}", true).Result == ',');

                ctx.SetResult(jctr);
            }

            return ctx;
        }
Exemple #3
0
        /// <summary>
        /// Consume a JSON numeric token
        /// </summary>
        /// <param name="ctx"></param>
        /// <param name="throws"></param>
        /// <returns></returns>
        private static JsonParserContext ConsumeNumber(
            this JsonParserContext ctx,
            bool throws
            )
        {
            const string Leading = "-0123456789";
            const string Allowed = Leading + ".Ee+";

            if (ctx.ConsumeWhiteSpace().ConsumeAnyChar(Leading, throws).IsSucceeded)
            {
                JSonReader src = ctx.Begin();

                for (int p = src.Position, len = src.Text.Length; p < len; p++)
                {
                    if (Allowed.IndexOf(src.Text[p]) < 0)
                    {
                        ctx.SetResult(
                            new JValue { BoxedValue = double.Parse(src.Text.Substring(src.Position - 1, p - src.Position + 1)) }
                            );

                        src.Position = p;
                        break;
                    }
                }
            }

            return ctx;
        }
Exemple #4
0
        /// <summary>
        /// Consume the JSON "null" token
        /// </summary>
        /// <param name="ctx"></param>
        /// <param name="throws"></param>
        /// <returns></returns>
        private static JsonParserContext ConsumeNull(
            this JsonParserContext ctx,
            bool throws
            )
        {
            if (ctx.ConsumeWhiteSpace().ConsumeAnyChar("n", throws).IsSucceeded)
            {
                ctx.ConsumeAllChars("ull", true);
                ctx.SetResult(
                    new JValue { BoxedValue = null }
                    );
            }

            return ctx;
        }
Exemple #5
0
        /// <summary>
        /// Consume a JSON boolean token
        /// </summary>
        /// <param name="ctx"></param>
        /// <param name="throws"></param>
        /// <returns></returns>
        private static JsonParserContext ConsumeBoolean(
            this JsonParserContext ctx,
            bool throws
            )
        {
            if (ctx.ConsumeWhiteSpace().ConsumeAnyChar("ft", throws).IsSucceeded)
            {
                bool flag = (char)ctx.Result == 't';
                ctx.ConsumeAllChars(flag ? "rue" : "alse", true);
                ctx.SetResult(
                    new JValue { BoxedValue = flag }
                    );
            }

            return ctx;
        }
Exemple #6
0
        /// <summary>
        /// Consume a JSON array
        /// </summary>
        /// <param name="ctx"></param>
        /// <param name="throws"></param>
        /// <returns></returns>
        private static JsonParserContext ConsumeArray(
            this JsonParserContext ctx,
            bool throws
            )
        {
            if (ctx.ConsumeWhiteSpace().ConsumeAnyChar("[", throws).IsSucceeded)
            {
                var jctr = new JArray();
                bool shouldThrow = false;

                do
                {
                    ctx.Begin();
                    if (ctx.ConsumeValue(shouldThrow).IsSucceeded)
                        jctr.Add((JToken)ctx.Result);
                    shouldThrow = true;
                }
                while ((char)ctx.ConsumeWhiteSpace().ConsumeAnyChar(",]", true).Result == ',');

                ctx.SetResult(jctr);
            }

            return ctx;
        }