예제 #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);
        }
예제 #2
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);
        }
예제 #3
0
        /// <summary>
        /// Consume all the characters in the specified sequence
        /// </summary>
        /// <param name="ctx"></param>
        /// <param name="sequence"></param>
        /// <param name="throws"></param>
        /// <returns></returns>
        private static JsonParserContext ConsumeAllChars(
            this JsonParserContext ctx,
            string sequence,
            bool throws
            )
        {
            JSonReader src = ctx.Begin();

            for (int q = 0, p = src.Position; q < sequence.Length; q++, p++)
            {
                if (src.Text[p] != sequence[q])
                {
                    if (throws)
                    {
                        throw new JsonParseException("Expected char not found.");
                    }

                    return(ctx);
                }
            }

            src.Position += sequence.Length;
            ctx.SetResult(sequence);
            return(ctx);
        }
예제 #4
0
        /// <summary>
        /// Parse a JSON-string to a specific DOM
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        public static JToken Parse(string text)
        {
            var rd  = new JSonReader(text);
            var ctx = new JsonParserContext(rd)
                      .ConsumeWhiteSpace()
                      .First(true, JsonHelpers.ConsumeObject, JsonHelpers.ConsumeArray);

            return((JToken)ctx.Result);
        }
예제 #5
0
        /// <summary>
        /// Consume an arbitrary number of whitespace characters
        /// </summary>
        /// <param name="ctx"></param>
        /// <returns></returns>
        private static JsonParserContext ConsumeWhiteSpace(
            this JsonParserContext ctx
            )
        {
            JSonReader src = ctx.Begin();

            for (int p = src.Position, len = src.Text.Length; p < len; p++)
            {
                if (WhiteSpace.IndexOf(src.Text[p]) < 0)
                {
                    src.Position = p;
                    break;
                }
            }

            return(ctx);
        }
예제 #6
0
        /// <summary>
        /// Consume any character (at least one) in the specified set
        /// </summary>
        /// <param name="ctx"></param>
        /// <param name="charset"></param>
        /// <param name="throws"></param>
        /// <returns></returns>
        private static JsonParserContext ConsumeAnyChar(
            this JsonParserContext ctx,
            string charset,
            bool throws
            )
        {
            JSonReader src = ctx.Begin();

            char c;

            if (charset.IndexOf(c = src.Text[src.Position]) >= 0)
            {
                src.Position++;
                ctx.SetResult(c);
            }
            else if (throws)
            {
                throw new JsonParseException("Expected char not found.");
            }

            return(ctx);
        }
예제 #7
0
 public JsonParserContext(JSonReader source)
 {
     this._source = source;
 }
예제 #8
0
 public JsonParserContext(JSonReader source)
 {
     this._source = source;
 }
예제 #9
0
        /// <summary>
        /// Parse a JSON-string to a specific DOM
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        public static JToken Parse(string text)
        {
            var rd = new JSonReader(text);
            var ctx = new JsonParserContext(rd)
                .ConsumeWhiteSpace()
                .First(true, JsonHelpers.ConsumeObject, JsonHelpers.ConsumeArray);

            return (JToken)ctx.Result;
        }