/// <summary>
        /// 予約語(<c>true</c>, <c>false</c>, <c>null</c>)をパースします。<see cref="Parse(JsonScanner)" />で使用されます。
        /// </summary>
        /// <param name="scanner">指定するスキャナ。</param>
        /// <returns>予約語をそのまま返します。</returns>
        private static object ParseWord(JsonScanner scanner)
        {
            if (scanner.Check(IsSmallT))
            {
                if (!scanner.CheckAdvance("true"))
                {
                    throw new ArgumentMapParseException("word -> true required", scanner.Itext.PositionInfo);
                }

                return(true);
            }

            if (scanner.Check(IsSmallF))
            {
                if (!scanner.CheckAdvance("false"))
                {
                    throw new ArgumentMapParseException("word -> false required", scanner.Itext.PositionInfo);
                }

                return(false);
            }

            if (!scanner.Check(IsSmallN))
            {
                throw new ArgumentMapParseException("word -> unexpected character", scanner.Itext.PositionInfo);
            }
            if (!scanner.CheckAdvance("null"))
            {
                throw new ArgumentMapParseException("word -> null required", scanner.Itext.PositionInfo);
            }

            return(null);
        }
        /// <summary>
        /// 配列をパースします。これは<see cref="Parse(JsonScanner)" />や一部のメソッドで使用されます。
        /// </summary>
        /// <param name="scanner">指定するスキャナ。</param>
        /// <returns>パースに成功した場合<see cref="object" />の配列を返します。 <br />
        /// 何かの配列として利用するにはその他の処理が必要です。</returns>
        private static object[] ParseArray(JsonScanner scanner)
        {
            var osList = new List <object>();

            if (!scanner.CheckAdvance(IsLeftBracket))
            {
                throw new ArgumentMapParseException("array -> left bracket required", scanner.Itext.PositionInfo);
            }

            ParseWhitespace(scanner);

            if (scanner.CheckAdvance(IsRightBracket))
            {
                return(osList.ToArray());
            }

            while (true)
            {
                osList.Add(ParseValue(scanner));

                ParseWhitespace(scanner);

                if (scanner.CheckAdvance(IsRightBracket))
                {
                    return(osList.ToArray());
                }
                if (!scanner.CheckAdvance(IsComma))
                {
                    throw new ArgumentMapParseException("array -> comma required", scanner.Itext.PositionInfo);
                }

                ParseWhitespace(scanner);
            }
        }
Esempio n. 3
0
 public void Dispose()
 {
     if (m_Scanner != null)
     {
         m_Scanner.Dispose();
         m_Scanner = null;
     }
 }
Esempio n. 4
0
        public void TestMissingClosingSlash(string jsonRegex)
        {
            var buffer = new JsonBuffer(jsonRegex);

            var exception = Record.Exception(() => JsonScanner.GetNextToken(buffer));

            Assert.IsType <FormatException>(exception);
            Assert.StartsWith("Invalid JSON regular expression", exception.Message);
        }
Esempio n. 5
0
 public TableEntityReader(string json)
 {
     if (string.IsNullOrEmpty(json))
     {
         throw new ArgumentException("json");
     }
     scanner = new JsonScanner(json);
     state   = TableEntityReaderState.Initial;
 }
        /// <summary>
        /// 今までのパース引数を構文解析と共にまとめます。
        /// </summary>
        /// <param name="scanner">指定するスキャナ。</param>
        /// <returns>objectとして<see cref="Parse(JsonScanner)" />に渡します。</returns>
        private static object ParseValue(JsonScanner scanner)
        {
            ParseWhitespace(scanner);

            return(scanner.Check(IsLeftBrace) ? ParseObject(scanner) :
                   scanner.Check(IsLeftBracket) ? ParseArray(scanner) :
                   scanner.Check(IsDoubleQuote) ? ParseString(scanner) :
                   scanner.Check(IsSign) ? ParseNumber(scanner) :
                   scanner.Check(IsDigit) ? ParseNumber(scanner) : ParseWord(scanner));
        }
        public void TestValueSeparator()
        {
            var json   = "\t ,x";
            var buffer = new JsonBuffer(json);
            var token  = JsonScanner.GetNextToken(buffer);

            Assert.AreEqual(JsonTokenType.Comma, token.Type);
            Assert.AreEqual(",", token.Lexeme);
            Assert.AreEqual('x', buffer.Read());
        }
Esempio n. 8
0
        public void TestOneExponentTwelve()
        {
            var json   = "\t 1e12,";
            var buffer = new JsonBuffer(json);
            var token  = JsonScanner.GetNextToken(buffer);

            Assert.Equal(JsonTokenType.Double, token.Type);
            Assert.Equal("1e12", token.Lexeme);
            Assert.Equal(',', buffer.Read());
        }
Esempio n. 9
0
        public void TestEndObject()
        {
            var json   = "\t }x";
            var buffer = new JsonBuffer(json);
            var token  = JsonScanner.GetNextToken(buffer);

            Assert.Equal(JsonTokenType.EndObject, token.Type);
            Assert.Equal("}", token.Lexeme);
            Assert.Equal('x', buffer.Read());
        }
Esempio n. 10
0
        public void TestMinusZeroPointZero()
        {
            var json   = "\t -0.0,";
            var buffer = new JsonBuffer(json);
            var token  = JsonScanner.GetNextToken(buffer);

            Assert.Equal(JsonTokenType.Double, token.Type);
            Assert.Equal("-0.0", token.Lexeme);
            Assert.Equal(',', buffer.Read());
        }
Esempio n. 11
0
        public void TestMinusOne()
        {
            var json   = "\t -1,";
            var buffer = new JsonBuffer(json);
            var token  = JsonScanner.GetNextToken(buffer);

            Assert.Equal(JsonTokenType.Int32, token.Type);
            Assert.Equal("-1", token.Lexeme);
            Assert.Equal(',', buffer.Read());
        }
Esempio n. 12
0
        public void TestNull()
        {
            var json   = "\t null,";
            var buffer = new JsonBuffer(json);
            var token  = JsonScanner.GetNextToken(buffer);

            Assert.Equal(JsonTokenType.UnquotedString, token.Type);
            Assert.Equal("null", token.StringValue);
            Assert.Equal(',', buffer.Read());
        }
Esempio n. 13
0
        public void TestBeginArray()
        {
            var json   = "\t [x";
            var buffer = new JsonBuffer(json);
            var token  = JsonScanner.GetNextToken(buffer);

            Assert.Equal(JsonTokenType.BeginArray, token.Type);
            Assert.Equal("[", token.Lexeme);
            Assert.Equal('x', buffer.Read());
        }
Esempio n. 14
0
        public void TestUndefined()
        {
            var json   = "\t undefined,";
            var buffer = new JsonBuffer(json);
            var token  = JsonScanner.GetNextToken(buffer);

            Assert.AreEqual(JsonTokenType.UnquotedString, token.Type);
            Assert.AreEqual("undefined", token.StringValue);
            Assert.AreEqual(',', buffer.Read());
        }
Esempio n. 15
0
        public void TestNameSeparator()
        {
            var json   = "\t :x";
            var buffer = new JsonBuffer(json);
            var token  = JsonScanner.GetNextToken(buffer);

            Assert.Equal(JsonTokenType.Colon, token.Type);
            Assert.Equal(":", token.Lexeme);
            Assert.Equal('x', buffer.Read());
        }
Esempio n. 16
0
        public void TestEndArray()
        {
            var json   = "\t ]x";
            var buffer = new JsonBuffer(json);
            var token  = JsonScanner.GetNextToken(buffer);

            Assert.AreEqual(JsonTokenType.EndArray, token.Type);
            Assert.AreEqual("]", token.Lexeme);
            Assert.AreEqual('x', buffer.Read());
        }
Esempio n. 17
0
        public void TestRegularExpressionEmpty()
        {
            var json   = "\t //,";
            var buffer = new JsonBuffer(json);
            var token  = JsonScanner.GetNextToken(buffer);

            Assert.AreEqual(JsonTokenType.RegularExpression, token.Type);
            Assert.AreEqual("//", token.Lexeme);
            Assert.AreEqual(',', buffer.Read());
        }
Esempio n. 18
0
        public void TestRegularExpressionPatternAndOptions()
        {
            var json   = "\t /pattern/imxs,";
            var buffer = new JsonBuffer(json);
            var token  = JsonScanner.GetNextToken(buffer);

            Assert.AreEqual(JsonTokenType.RegularExpression, token.Type);
            Assert.AreEqual("/pattern/imxs", token.Lexeme);
            Assert.AreEqual(',', buffer.Read());
        }
Esempio n. 19
0
        public void TestMinusOnePointTwo()
        {
            var json   = "\t -1.2,";
            var buffer = new JsonBuffer(json);
            var token  = JsonScanner.GetNextToken(buffer);

            Assert.AreEqual(JsonTokenType.Double, token.Type);
            Assert.AreEqual("-1.2", token.Lexeme);
            Assert.AreEqual(',', buffer.Read());
        }
Esempio n. 20
0
        public void TestMinusZeroExponentMinusTwelve()
        {
            var json   = "\t -1e-12,";
            var buffer = new JsonBuffer(json);
            var token  = JsonScanner.GetNextToken(buffer);

            Assert.AreEqual(JsonTokenType.Double, token.Type);
            Assert.AreEqual("-1e-12", token.Lexeme);
            Assert.AreEqual(',', buffer.Read());
        }
Esempio n. 21
0
        public void TestZeroExponentOne()
        {
            var json   = "\t 0e1,";
            var buffer = new JsonBuffer(json);
            var token  = JsonScanner.GetNextToken(buffer);

            Assert.AreEqual(JsonTokenType.Double, token.Type);
            Assert.AreEqual("0e1", token.Lexeme);
            Assert.AreEqual(',', buffer.Read());
        }
Esempio n. 22
0
        public void TestBeginObject()
        {
            var json   = "\t {x";
            var buffer = new JsonBuffer(json);
            var token  = JsonScanner.GetNextToken(buffer);

            Assert.AreEqual(JsonTokenType.BeginObject, token.Type);
            Assert.AreEqual("{", token.Lexeme);
            Assert.AreEqual('x', buffer.Read());
        }
Esempio n. 23
0
        public void TestEndOfFile()
        {
            var json   = "\t ";
            var buffer = new JsonBuffer(json);
            var token  = JsonScanner.GetNextToken(buffer);

            Assert.AreEqual(JsonTokenType.EndOfFile, token.Type);
            Assert.AreEqual("<eof>", token.Lexeme);
            Assert.AreEqual(-1, buffer.Read());
        }
Esempio n. 24
0
        public void TestTwelve()
        {
            var json   = "\t 12,";
            var buffer = new JsonBuffer(json);
            var token  = JsonScanner.GetNextToken(buffer);

            Assert.AreEqual(JsonTokenType.Int32, token.Type);
            Assert.AreEqual("12", token.Lexeme);
            Assert.AreEqual(',', buffer.Read());
        }
Esempio n. 25
0
        public void TestUnquotedString()
        {
            var json   = "\t name123:1";
            var buffer = new JsonBuffer(json);
            var token  = JsonScanner.GetNextToken(buffer);

            Assert.AreEqual(JsonTokenType.UnquotedString, token.Type);
            Assert.AreEqual("name123", token.StringValue);
            Assert.AreEqual(':', buffer.Read());
        }
Esempio n. 26
0
        public void TestEmptyString()
        {
            var json   = "\t \"\"x";
            var buffer = new JsonBuffer(json);
            var token  = JsonScanner.GetNextToken(buffer);

            Assert.Equal(JsonTokenType.String, token.Type);
            Assert.Equal("", token.StringValue);
            Assert.Equal('x', buffer.Read());
        }
Esempio n. 27
0
        public void TestEscapeSequences()
        {
            var json   = "\t \"x\\\"\\\\\\/\\b\\f\\n\\r\\t\\u0030y\"x";
            var buffer = new JsonBuffer(json);
            var token  = JsonScanner.GetNextToken(buffer);

            Assert.AreEqual(JsonTokenType.String, token.Type);
            Assert.AreEqual("x\"\\/\b\f\n\r\t0y", token.StringValue);
            Assert.AreEqual('x', buffer.Read());
        }
Esempio n. 28
0
        public void Test3CharacterString()
        {
            var json   = "\t \"123\"x";
            var buffer = new JsonBuffer(json);
            var token  = JsonScanner.GetNextToken(buffer);

            Assert.AreEqual(JsonTokenType.String, token.Type);
            Assert.AreEqual("123", token.StringValue);
            Assert.AreEqual('x', buffer.Read());
        }
        /// <summary>
        /// Json値の入れ子となるオブジェクトをパースします。<see cref="Parse(JsonScanner)" />の一部で使用されます。
        /// </summary>
        /// <param name="scanner">指定するスキャナ。</param>
        /// <returns><see cref="string" />にキー値を代入してハッシュマップで返します。</returns>
        private static Dictionary <string, object> ParseObject(JsonScanner scanner)
        {
            var osDict = new Dictionary <string, object>();

            if (!scanner.CheckAdvance(IsLeftBrace))
            {
                throw new ArgumentMapParseException("object -> left brace required", scanner.Itext.PositionInfo);
            }

            ParseWhitespace(scanner);

            if (scanner.CheckAdvance(IsRightBrace))
            {
                return(osDict);
            }

            while (true)
            {
                var key = ParseString(scanner);

                if (osDict.Keys.Contains(key))
                {
                    throw new ArgumentMapParseException("object -> same key", scanner.Itext.PositionInfo);
                }

                ParseWhitespace(scanner);

                if (!scanner.CheckAdvance(IsColon))
                {
                    throw new ArgumentMapParseException("object -> colon required", scanner.Itext.PositionInfo);
                }

                ParseWhitespace(scanner);

                osDict[key] = ParseValue(scanner);

                ParseWhitespace(scanner);

                if (scanner.CheckAdvance(IsRightBrace))
                {
                    return(osDict);
                }

                if (!scanner.CheckAdvance(IsComma))
                {
                    throw new ArgumentMapParseException("object -> comma required", scanner.Itext.PositionInfo);
                }

                ParseWhitespace(scanner);
            }
        }
Esempio n. 30
0
        private JsonScanner GetTokenizer()
        {
            MemoryStream ms = new MemoryStream();
            StreamWriter sw = new StreamWriter(ms);

            sw.Write(textJson.Text);
            sw.Flush();
            ms.Position = 0;

            JsonScanner tokenizer = new JsonScanner();

            tokenizer.SetInput(new StreamReader(ms));

            return(tokenizer);
        }