예제 #1
0
 /// <summary>
 /// Validates that the parser is complete.
 /// </summary>
 /// <param name="expectedPayload">The expected payload.</param>
 /// <param name="actualParser">The actual parser.</param>
 public static void IsComplete(string expectedPayload, JsonParser actualParser)
 {
     Assert.IsFalse(actualParser.Next());
     AssertToken.Matches(JsonTokenType.Complete, expectedPayload, 0, expectedPayload.Length, actualParser);
     Assert.IsFalse(actualParser.Next());
     AssertToken.Matches(JsonTokenType.Complete, expectedPayload, 0, expectedPayload.Length, actualParser);
 }
예제 #2
0
        public void JsonParser_Skip_Moves_Over_Containers()
        {
            string     payload = "{\"first\":{\"value\":1},\"second\":[1,2],\"third\":123.45}";
            JsonParser target  = new JsonParser(payload);

            Assert.IsTrue(target.Next());
            AssertToken.Matches(JsonTokenType.BeginObject, payload, 0, 1, target);
            Assert.IsTrue(target.Next());
            AssertToken.IsProperty("first", payload, 1, 8, target);
            Assert.IsTrue(target.Next());
            AssertToken.Matches(JsonTokenType.BeginObject, payload, 9, 1, target);
            Assert.IsTrue(target.Skip());
            AssertToken.Matches(JsonTokenType.EndObject, payload, 9, 11, target);
            Assert.IsTrue(target.Next());
            AssertToken.Matches(JsonTokenType.EndProperty, payload, 1, 19, target);
            Assert.IsTrue(target.Next());
            AssertToken.IsProperty("second", payload, 21, 9, target);
            Assert.IsTrue(target.Next());
            AssertToken.Matches(JsonTokenType.BeginArray, payload, 30, 1, target);
            Assert.IsTrue(target.Next());
            AssertToken.IsValue(1, payload, 31, 1, target);
            Assert.IsTrue(target.Skip());
            AssertToken.Matches(JsonTokenType.EndArray, payload, 30, 5, target);
            Assert.IsTrue(target.Next());
            AssertToken.Matches(JsonTokenType.EndProperty, payload, 21, 14, target);
            Assert.IsTrue(target.Next());
            AssertToken.IsProperty("third", payload, 36, 8, target);
            Assert.IsTrue(target.Skip());
            AssertToken.Matches(JsonTokenType.EndProperty, payload, 36, 14, target);
            Assert.IsTrue(target.Next());
            AssertToken.Matches(JsonTokenType.EndObject, payload, 0, 51, target);
            AssertToken.IsComplete(payload, target);
        }
예제 #3
0
        public void JsonParser_TryParseToken_Parses_Double()
        {
            string     payload = double.MinValue.ToString("R", CultureInfo.InvariantCulture);
            JsonParser target  = new JsonParser(payload);

            Assert.IsTrue(target.Next());
            AssertToken.IsValue(double.MinValue, payload, 0, payload.Length, target);
            AssertToken.IsComplete(payload, target);
            payload = double.Epsilon.ToString("R", CultureInfo.InvariantCulture);
            target.Reset(payload);
            Assert.IsTrue(target.Next());
            AssertToken.IsValue(double.Epsilon, payload, 0, payload.Length, target);
            AssertToken.IsComplete(payload, target);
            payload = double.MaxValue.ToString("R", CultureInfo.InvariantCulture);
            target.Reset(payload);
            Assert.IsTrue(target.Next());
            AssertToken.IsValue(double.MaxValue, payload, 0, payload.Length, target);
            AssertToken.IsComplete(payload, target);
            payload = "123.456";
            target.Reset(payload);
            Assert.IsTrue(target.Next());
            AssertToken.IsValue(123.456D, payload, 0, payload.Length, target);
            AssertToken.IsComplete(payload, target);
            payload = "12e34";
            target.Reset(payload);
            Assert.IsTrue(target.Next());
            AssertToken.IsValue(12e34D, payload, 0, payload.Length, target);
            AssertToken.IsComplete(payload, target);
        }
예제 #4
0
        public void JsonParser_Next_Parses_Object_Nested_In_Array()
        {
            string     payload = "[{\"value\":1},{\"value\":2}]";
            JsonParser target  = new JsonParser(payload);

            Assert.IsTrue(target.Next());
            AssertToken.Matches(JsonTokenType.BeginArray, payload, 0, 1, target);
            Assert.IsTrue(target.Next());
            AssertToken.Matches(JsonTokenType.BeginObject, payload, 1, 1, target);
            Assert.IsTrue(target.Next());
            AssertToken.IsProperty("value", payload, 2, 8, target);
            Assert.IsTrue(target.Next());
            AssertToken.IsValue(1L, payload, 10, 1, target);
            Assert.IsTrue(target.Next());
            AssertToken.Matches(JsonTokenType.EndProperty, payload, 2, 9, target);
            Assert.IsTrue(target.Next());
            AssertToken.Matches(JsonTokenType.EndObject, payload, 1, 11, target);
            Assert.IsTrue(target.Next());
            AssertToken.Matches(JsonTokenType.BeginObject, payload, 13, 1, target);
            Assert.IsTrue(target.Next());
            AssertToken.IsProperty("value", payload, 14, 8, target);
            Assert.IsTrue(target.Next());
            AssertToken.IsValue(2L, payload, 22, 1, target);
            Assert.IsTrue(target.Next());
            AssertToken.Matches(JsonTokenType.EndProperty, payload, 14, 9, target);
            Assert.IsTrue(target.Next());
            AssertToken.Matches(JsonTokenType.EndObject, payload, 13, 11, target);
            Assert.IsTrue(target.Next());
            AssertToken.Matches(JsonTokenType.EndArray, payload, 0, 25, target);
            AssertToken.IsComplete(payload, target);
        }
예제 #5
0
        public void JsonParser_Next_Halts_On_Missing_Fraction()
        {
            string     payload = "123.";
            JsonParser target  = new JsonParser(payload);

            Assert.IsFalse(target.Next());
            AssertToken.Matches(JsonTokenType.Invalid, payload, 0, 0, target);
        }
예제 #6
0
        public void JsonParser_Next_Halts_On_Broken_Null()
        {
            string     payload = "nope";
            JsonParser target  = new JsonParser(payload);

            Assert.IsFalse(target.Next());
            AssertToken.Matches(JsonTokenType.Invalid, payload, 0, 0, target);
        }
예제 #7
0
        /// <summary>
        /// Validates that the parser is on a value.
        /// </summary>
        /// <param name="expectedValue">The expected value.</param>
        /// <param name="expectedPayload">The expected payload.</param>
        /// <param name="expectedOffset">The expected offset.</param>
        /// <param name="expectedCount">The expected count.</param>
        /// <param name="actualParser">The actual parser.</param>
        public static void IsValue(ulong expectedValue, string expectedPayload, int expectedOffset, int expectedCount, JsonParser actualParser)
        {
            AssertToken.Matches(JsonTokenType.Integer, expectedPayload, expectedOffset, expectedCount, actualParser);
            ulong actualValue = 0UL;

            Assert.IsTrue(actualParser.TryParseToken(out actualValue));
            Assert.AreEqual(expectedValue, actualValue);
        }
예제 #8
0
        /// <summary>
        /// Validates that the parser is on a value.
        /// </summary>
        /// <param name="expectedValue">The expected value.</param>
        /// <param name="expectedPayload">The expected payload.</param>
        /// <param name="expectedOffset">The expected offset.</param>
        /// <param name="expectedCount">The expected count.</param>
        /// <param name="actualParser">The actual parser.</param>
        public static void IsValue(bool expectedValue, string expectedPayload, int expectedOffset, int expectedCount, JsonParser actualParser)
        {
            AssertToken.Matches(JsonTokenType.Boolean, expectedPayload, expectedOffset, expectedCount, actualParser);
            bool actualValue = false;

            Assert.IsTrue(actualParser.TryParseToken(out actualValue));
            Assert.AreEqual(expectedValue, actualValue);
        }
예제 #9
0
        /// <summary>
        /// Validates that the parser is on a property.
        /// </summary>
        /// <param name="expectedProperty">The expected property.</param>
        /// <param name="expectedPayload">The expected payload.</param>
        /// <param name="expectedOffset">The expected offset.</param>
        /// <param name="expectedCount">The expected count.</param>
        /// <param name="actualParser">The actual parser.</param>
        public static void IsProperty(string expectedProperty, string expectedPayload, int expectedOffset, int expectedCount, JsonParser actualParser)
        {
            AssertToken.Matches(JsonTokenType.BeginProperty, expectedPayload, expectedOffset, expectedCount, actualParser);
            string actualProperty = null;

            Assert.IsTrue(actualParser.TryParseToken(out actualProperty));
            Assert.AreEqual(expectedProperty, actualProperty);
        }
예제 #10
0
        public void JsonParser_TryParseToken_Parses_Hexadecimal_UInt64()
        {
            string     payload = "0x0123456789ABCDEF";
            JsonParser target  = new JsonParser(payload);

            Assert.IsTrue(target.Next());
            AssertToken.IsValue(81985529216486895UL, payload, 0, payload.Length, target);
            AssertToken.IsComplete(payload, target);
        }
예제 #11
0
        public void JsonParser_TryParseToken_Parses_String()
        {
            string     payload = Constants.UnicodeRainbowEncoded;
            JsonParser target  = new JsonParser(payload);

            Assert.IsTrue(target.Next());
            AssertToken.IsValue(Constants.UnicodeRainbowDecoded, payload, 0, payload.Length, target);
            AssertToken.IsComplete(payload, target);
        }
예제 #12
0
        public void JsonParser_TryParseToken_Parses_UInt64()
        {
            string     payload = ulong.MaxValue.ToString(CultureInfo.InvariantCulture);
            JsonParser target  = new JsonParser(payload);

            Assert.IsTrue(target.Next());
            AssertToken.IsValue(ulong.MaxValue, payload, 0, payload.Length, target);
            AssertToken.IsComplete(payload, target);
        }
예제 #13
0
        public void JsonParser_Next_Halts_On_Empty_Element()
        {
            string     payload = "[,]";
            JsonParser target  = new JsonParser(payload);

            Assert.IsTrue(target.Next());
            AssertToken.Matches(JsonTokenType.BeginArray, payload, 0, 1, target);
            Assert.IsFalse(target.Next());
            AssertToken.Matches(JsonTokenType.Invalid, payload, 2, 0, target);
        }
예제 #14
0
        public void JsonParser_Next_Parses_Empty_Array()
        {
            string     payload = "[]";
            JsonParser target  = new JsonParser(payload);

            Assert.IsTrue(target.Next());
            AssertToken.Matches(JsonTokenType.BeginArray, payload, 0, 1, target);
            Assert.IsTrue(target.Next());
            AssertToken.Matches(JsonTokenType.EndArray, payload, 0, 2, target);
            AssertToken.IsComplete(payload, target);
        }
예제 #15
0
        public void JsonParser_Next_Halts_On_Empty_Property()
        {
            string     payload = "{\"first\":}";
            JsonParser target  = new JsonParser(payload);

            Assert.IsTrue(target.Next());
            AssertToken.Matches(JsonTokenType.BeginObject, payload, 0, 1, target);
            Assert.IsTrue(target.Next());
            AssertToken.IsProperty("first", payload, 1, 8, target);
            Assert.IsFalse(target.Next());
            AssertToken.Matches(JsonTokenType.Invalid, payload, 9, 0, target);
        }
예제 #16
0
        public void JsonParser_Next_Halts_On_Broken_Boolean()
        {
            string     payload = "fairy";
            JsonParser target  = new JsonParser(payload);

            Assert.IsFalse(target.Next());
            AssertToken.Matches(JsonTokenType.Invalid, payload, 0, 0, target);
            payload = "tale";
            target.Reset(payload);
            Assert.IsFalse(target.Next());
            AssertToken.Matches(JsonTokenType.Invalid, payload, 0, 0, target);
        }
예제 #17
0
        /// <summary>
        /// Validates that the parser is on a value.
        /// </summary>
        /// <param name="expectedValue">The expected value.</param>
        /// <param name="expectedPayload">The expected payload.</param>
        /// <param name="expectedOffset">The expected offset.</param>
        /// <param name="expectedCount">The expected count.</param>
        /// <param name="actualParser">The actual parser.</param>
        public static void IsValue(string expectedValue, string expectedPayload, int expectedOffset, int expectedCount, JsonParser actualParser)
        {
            AssertToken.Matches(JsonTokenType.String, expectedPayload, expectedOffset, expectedCount, actualParser);
            string actualValue = null;

            Assert.IsTrue(actualParser.TryParseToken(out actualValue));
            Assert.AreEqual(expectedValue.Length, actualValue.Length);
            for (int i = 0; i < expectedValue.Length; i++)
            {
                Assert.AreEqual(expectedValue[i], actualValue[i], "index:{0}", i);
            }
        }
예제 #18
0
        public void JsonParser_TryParseToken_Parses_Boolean()
        {
            string     payload = "true";
            JsonParser target  = new JsonParser(payload);

            Assert.IsTrue(target.Next());
            AssertToken.IsValue(true, payload, 0, payload.Length, target);
            AssertToken.IsComplete(payload, target);
            payload = "false";
            target.Reset(payload);
            Assert.IsTrue(target.Next());
            AssertToken.IsValue(false, payload, 0, payload.Length, target);
            AssertToken.IsComplete(payload, target);
        }
예제 #19
0
        public void JsonParser_Next_Halts_On_Missing_Element_Separator()
        {
            string     payload = "{\"first\":123 \"second\":456}";
            JsonParser target  = new JsonParser(payload);

            Assert.IsTrue(target.Next());
            AssertToken.Matches(JsonTokenType.BeginObject, payload, 0, 1, target);
            Assert.IsTrue(target.Next());
            AssertToken.IsProperty("first", payload, 1, 8, target);
            Assert.IsTrue(target.Next());
            AssertToken.Matches(JsonTokenType.Integer, payload, 9, 3, target);
            Assert.IsFalse(target.Next());
            AssertToken.Matches(JsonTokenType.Invalid, payload, 13, 0, target);
        }
예제 #20
0
        public void JsonParser_TryParseToken_Ignores_Array_White_Space()
        {
            string     payload = " [ 1 , 2 ] ";
            JsonParser target  = new JsonParser(payload);

            Assert.IsTrue(target.Next());
            AssertToken.Matches(JsonTokenType.BeginArray, payload, 1, 1, target);
            Assert.IsTrue(target.Next());
            AssertToken.IsValue(1, payload, 3, 1, target);
            Assert.IsTrue(target.Next());
            AssertToken.IsValue(2, payload, 7, 1, target);
            Assert.IsTrue(target.Next());
            AssertToken.Matches(JsonTokenType.EndArray, payload, 1, 9, target);
            AssertToken.IsComplete(payload, target);
        }
예제 #21
0
        public void JsonParser_TryParseToken_Ignores_Object_White_Space()
        {
            string     payload = " { \"key\" : \"value\" } ";
            JsonParser target  = new JsonParser(payload);

            Assert.IsTrue(target.Next());
            AssertToken.Matches(JsonTokenType.BeginObject, payload, 1, 1, target);
            Assert.IsTrue(target.Next());
            AssertToken.IsProperty("key", payload, 3, 7, target);
            Assert.IsTrue(target.Next());
            AssertToken.IsValue("value", payload, 11, 7, target);
            Assert.IsTrue(target.Next());
            AssertToken.Matches(JsonTokenType.EndProperty, payload, 3, 15, target);
            Assert.IsTrue(target.Next());
            AssertToken.Matches(JsonTokenType.EndObject, payload, 1, 19, target);
            AssertToken.IsComplete(payload, target);
        }
예제 #22
0
        public void JsonParser_Next_Parses_Null_Property()
        {
            string     payload = "{\"isNull\":null}";
            JsonParser target  = new JsonParser(payload);

            Assert.IsTrue(target.Next());
            AssertToken.Matches(JsonTokenType.BeginObject, payload, 0, 1, target);
            Assert.IsTrue(target.Next());
            AssertToken.IsProperty("isNull", payload, 1, 9, target);
            Assert.IsTrue(target.Next());
            AssertToken.Matches(JsonTokenType.Null, payload, 10, 4, target);
            Assert.IsTrue(target.Next());
            AssertToken.Matches(JsonTokenType.EndProperty, payload, 1, 13, target);
            Assert.IsTrue(target.Next());
            AssertToken.Matches(JsonTokenType.EndObject, payload, 0, 15, target);
            AssertToken.IsComplete(payload, target);
        }
예제 #23
0
        public void JsonParser_Next_Parses_Array_Nested_In_Array()
        {
            string     payload = "[[1,2]]";
            JsonParser target  = new JsonParser(payload);

            Assert.IsTrue(target.Next());
            AssertToken.Matches(JsonTokenType.BeginArray, payload, 0, 1, target);
            Assert.IsTrue(target.Next());
            AssertToken.Matches(JsonTokenType.BeginArray, payload, 1, 1, target);
            Assert.IsTrue(target.Next());
            AssertToken.IsValue(1L, payload, 2, 1, target);
            Assert.IsTrue(target.Next());
            AssertToken.IsValue(2L, payload, 4, 1, target);
            Assert.IsTrue(target.Next());
            AssertToken.Matches(JsonTokenType.EndArray, payload, 1, 5, target);
            Assert.IsTrue(target.Next());
            AssertToken.Matches(JsonTokenType.EndArray, payload, 0, 7, target);
            AssertToken.IsComplete(payload, target);
        }
예제 #24
0
        public void JsonParser_Bypass_Skips_Superfluous_Comma()
        {
            string     payload = "{\"test\":true,}";
            JsonParser target  = new JsonParser(payload);

            Assert.IsTrue(target.Next());
            AssertToken.Matches(JsonTokenType.BeginObject, payload, 0, 1, target);
            Assert.IsTrue(target.Next());
            AssertToken.IsProperty("test", payload, 1, 7, target);
            Assert.IsTrue(target.Next());
            AssertToken.IsValue(true, payload, 8, 4, target);
            Assert.IsTrue(target.Next());
            AssertToken.Matches(JsonTokenType.EndProperty, payload, 1, 11, target);
            Assert.IsFalse(target.Next());
            Assert.AreEqual(JsonTokenType.Invalid, target.TokenType);
            target.Bypass();
            Assert.IsTrue(target.Next());
            AssertToken.Matches(JsonTokenType.EndObject, payload, 0, 14, target);
            AssertToken.IsComplete(payload, target);
        }
예제 #25
0
        public void JsonParser_Next_Parses_Object_Nested_In_Property()
        {
            string     payload = "{\"first\":{\"value\":1},\"second\":{\"value\":2}}";
            JsonParser target  = new JsonParser(payload);

            Assert.IsTrue(target.Next());
            AssertToken.Matches(JsonTokenType.BeginObject, payload, 0, 1, target);
            Assert.IsTrue(target.Next());
            AssertToken.IsProperty("first", payload, 1, 8, target);
            Assert.IsTrue(target.Next());
            AssertToken.Matches(JsonTokenType.BeginObject, payload, 9, 1, target);
            Assert.IsTrue(target.Next());
            AssertToken.IsProperty("value", payload, 10, 8, target);
            Assert.IsTrue(target.Next());
            AssertToken.IsValue(1L, payload, 18, 1, target);
            Assert.IsTrue(target.Next());
            AssertToken.Matches(JsonTokenType.EndProperty, payload, 10, 9, target);
            Assert.IsTrue(target.Next());
            AssertToken.Matches(JsonTokenType.EndObject, payload, 9, 11, target);
            Assert.IsTrue(target.Next());
            AssertToken.Matches(JsonTokenType.EndProperty, payload, 1, 19, target);
            Assert.IsTrue(target.Next());
            AssertToken.IsProperty("second", payload, 21, 9, target);
            Assert.IsTrue(target.Next());
            AssertToken.Matches(JsonTokenType.BeginObject, payload, 30, 1, target);
            Assert.IsTrue(target.Next());
            AssertToken.IsProperty("value", payload, 31, 8, target);
            Assert.IsTrue(target.Next());
            AssertToken.IsValue(2L, payload, 39, 1, target);
            Assert.IsTrue(target.Next());
            AssertToken.Matches(JsonTokenType.EndProperty, payload, 31, 9, target);
            Assert.IsTrue(target.Next());
            AssertToken.Matches(JsonTokenType.EndObject, payload, 30, 11, target);
            Assert.IsTrue(target.Next());
            AssertToken.Matches(JsonTokenType.EndProperty, payload, 21, 20, target);
            Assert.IsTrue(target.Next());
            AssertToken.Matches(JsonTokenType.EndObject, payload, 0, 42, target);
            AssertToken.IsComplete(payload, target);
        }
예제 #26
0
        public void JsonParser_Reset_Expands_Buffer()
        {
            string     payload = "{\"text\":\"\\n\"}";
            JsonParser target  = new JsonParser(payload);

            Assert.IsTrue(target.Next());
            AssertToken.Matches(JsonTokenType.BeginObject, payload, 0, 1, target);
            Assert.IsTrue(target.Next());
            AssertToken.IsProperty("text", payload, 1, 7, target);
            Assert.IsTrue(target.Next());
            AssertToken.IsValue("\n", payload, 8, 4, target);
            Assert.IsTrue(target.Next());
            AssertToken.Matches(JsonTokenType.EndProperty, payload, 1, 11, target);
            Assert.IsTrue(target.Next());
            AssertToken.Matches(JsonTokenType.EndObject, payload, 0, 13, target);
            AssertToken.IsComplete(payload, target);
            StringBuilder builder = new StringBuilder();

            builder.Append("{\"text\":\"");
            for (int i = 0; i < 100000; i++)
            {
                builder.Append("\\n");
            }

            builder.Append("\"}");
            payload = builder.ToString();
            target.Reset(payload);
            Assert.IsTrue(target.Next());
            AssertToken.Matches(JsonTokenType.BeginObject, payload, 0, 1, target);
            Assert.IsTrue(target.Next());
            AssertToken.IsProperty("text", payload, 1, 7, target);
            Assert.IsTrue(target.Next());
            AssertToken.IsValue(new string('\n', 100000), payload, 8, 200002, target);
            Assert.IsTrue(target.Next());
            AssertToken.Matches(JsonTokenType.EndProperty, payload, 1, 200009, target);
            Assert.IsTrue(target.Next());
            AssertToken.Matches(JsonTokenType.EndObject, payload, 0, 200011, target);
            AssertToken.IsComplete(payload, target);
        }
예제 #27
0
        public void JsonParser_Next_Parses_Array_Nested_In_Property()
        {
            string     payload = "{\"array\":[1,2]}";
            JsonParser target  = new JsonParser(payload);

            Assert.IsTrue(target.Next());
            AssertToken.Matches(JsonTokenType.BeginObject, payload, 0, 1, target);
            Assert.IsTrue(target.Next());
            AssertToken.IsProperty("array", payload, 1, 8, target);
            Assert.IsTrue(target.Next());
            AssertToken.Matches(JsonTokenType.BeginArray, payload, 9, 1, target);
            Assert.IsTrue(target.Next());
            AssertToken.IsValue(1L, payload, 10, 1, target);
            Assert.IsTrue(target.Next());
            AssertToken.IsValue(2L, payload, 12, 1, target);
            Assert.IsTrue(target.Next());
            AssertToken.Matches(JsonTokenType.EndArray, payload, 9, 5, target);
            Assert.IsTrue(target.Next());
            AssertToken.Matches(JsonTokenType.EndProperty, payload, 1, 13, target);
            Assert.IsTrue(target.Next());
            AssertToken.Matches(JsonTokenType.EndObject, payload, 0, 15, target);
            AssertToken.IsComplete(payload, target);
        }