Пример #1
0
        private static object ParseExpression(JsonToken token, TokenStream ts)
        {
            if (token == null)
            {
                token = ts.GetNextToken();
            }

            switch (token.Type)
            {
                case JsonToken.TokenType.String:
                    logger.Debug("Found String " + token.Value);
                    return token.Value;
                case JsonToken.TokenType.Number:
                    logger.Debug("Found Number " + token.Number);
                    return token.Number;
                case JsonToken.TokenType.False:
                    logger.Debug("Found False");
                    return false;
                case JsonToken.TokenType.True:
                    logger.Debug("Found True");
                    return true;
                case JsonToken.TokenType.Null:
                    logger.Debug("Found Null");
                    return null;
                case JsonToken.TokenType.ObjectStart:
                    logger.Debug("Found ObjectStart");
                    return ParseObject(ts);
                case JsonToken.TokenType.ArrayStart:
                    logger.Debug("Found ArrayStart");
                    return ParseArray(ts);
                case JsonToken.TokenType.Undefined:
                default:
                    throw new ArgumentException("parse error");
            }
        }
Пример #2
0
 public void GetNextTokenExponentPositive()
 {
     TokenStream target = new TokenStream("-1234.5678e+1");
     JsonToken actual = target.GetNextToken();
     Assert.AreEqual(JsonToken.TokenType.Number, actual.Type);
     Assert.AreEqual(-12345.678, actual.Number);
     Assert.AreEqual("-1234.5678e+1", actual.Value);
 }
Пример #3
0
        public void GetNextTokenNegativeNumber()
        {
            TokenStream target = new TokenStream("\"utc_offset\":-18000");
            JsonToken actual = target.GetNextToken();
            Assert.AreEqual(JsonToken.TokenType.String, actual.Type);
            Assert.AreEqual("utc_offset", actual.Value);

            actual = target.GetNextToken();
            Assert.AreEqual(JsonToken.TokenType.NameSeperator, actual.Type);

            actual = target.GetNextToken();
            Assert.AreEqual(JsonToken.TokenType.Number, actual.Type);
            Assert.AreEqual("-18000", actual.Value);
            Assert.AreEqual(-18000, actual.Number);
        }
Пример #4
0
 private static List<object> ParseArray(TokenStream ts)
 {
     var list = new List<object>();
     for (JsonToken cur = ts.GetNextToken(); cur != null; cur = ts.GetNextToken())
     {
         if (cur.Type == JsonToken.TokenType.ArrayEnd)
         {
             return list;
         }
         if (cur.Type != JsonToken.TokenType.MemberSeperator)
         {
             object value = ParseExpression(cur, ts);
             list.Add(value);
         }
     }
     return list;
 }
Пример #5
0
 public void GetNextTokenTestSpecialValues()
 {
     TokenStream target = new TokenStream("[ true, \"Value2\", false, null ]");
     JsonToken actual;
     actual = target.GetNextToken();
     Assert.AreEqual(JsonToken.TokenType.ArrayStart, actual.Type);
     actual = target.GetNextToken();
     Assert.AreEqual(JsonToken.TokenType.True, actual.Type);
     actual = target.GetNextToken();
     Assert.AreEqual(JsonToken.TokenType.MemberSeperator, actual.Type);
     actual = target.GetNextToken();
     Assert.AreEqual(JsonToken.TokenType.String, actual.Type);
     Assert.AreEqual("Value2", actual.Value);
     actual = target.GetNextToken();
     Assert.AreEqual(JsonToken.TokenType.MemberSeperator, actual.Type);
     actual = target.GetNextToken();
     Assert.AreEqual(JsonToken.TokenType.False, actual.Type);
     actual = target.GetNextToken();
     Assert.AreEqual(JsonToken.TokenType.MemberSeperator, actual.Type);
     actual = target.GetNextToken();
     Assert.AreEqual(JsonToken.TokenType.Null, actual.Type);
     actual = target.GetNextToken();
     Assert.AreEqual(JsonToken.TokenType.ArrayEnd, actual.Type);
     actual = target.GetNextToken();
     Assert.AreEqual(null, actual);
 }
Пример #6
0
 public void GetNextTokenTestSimpleObject()
 {
     TokenStream target = new TokenStream("{ \"Member\": \"Value\" }");
     JsonToken actual;
     actual = target.GetNextToken();
     Assert.AreEqual(JsonToken.TokenType.ObjectStart, actual.Type);
     actual = target.GetNextToken();
     Assert.AreEqual(JsonToken.TokenType.String, actual.Type);
     Assert.AreEqual("Member", actual.Value);
     actual = target.GetNextToken();
     Assert.AreEqual(JsonToken.TokenType.NameSeperator, actual.Type);
     actual = target.GetNextToken();
     Assert.AreEqual(JsonToken.TokenType.String, actual.Type);
     Assert.AreEqual("Value", actual.Value);
     actual = target.GetNextToken();
     Assert.AreEqual(JsonToken.TokenType.ObjectEnd, actual.Type);
 }
Пример #7
0
 public void GetNextTokenTestSimpleArray()
 {
     TokenStream target = new TokenStream("[ \"Value1\", \"Value2\" ]");
     JsonToken actual;
     actual = target.GetNextToken();
     Assert.AreEqual(JsonToken.TokenType.ArrayStart, actual.Type);
     actual = target.GetNextToken();
     Assert.AreEqual(JsonToken.TokenType.String, actual.Type);
     Assert.AreEqual("Value1", actual.Value);
     actual = target.GetNextToken();
     Assert.AreEqual(JsonToken.TokenType.MemberSeperator, actual.Type);
     actual = target.GetNextToken();
     Assert.AreEqual(JsonToken.TokenType.String, actual.Type);
     Assert.AreEqual("Value2", actual.Value);
     actual = target.GetNextToken();
     Assert.AreEqual(JsonToken.TokenType.ArrayEnd, actual.Type);
     actual = target.GetNextToken();
     Assert.AreEqual(null, actual);
 }
Пример #8
0
        public void GetNextTokenTestNestedObjects()
        {
            TokenStream target =
                new TokenStream("{ \"name\" : [ true, \"Value2\", false ], \"sub\" : { \"subname\" : 1234 } }");
            JsonToken actual;
            actual = target.GetNextToken();
            Assert.AreEqual(JsonToken.TokenType.ObjectStart, actual.Type);
            actual = target.GetNextToken();
            Assert.AreEqual(JsonToken.TokenType.String, actual.Type);
            Assert.AreEqual("name", actual.Value);
            actual = target.GetNextToken();
            Assert.AreEqual(JsonToken.TokenType.NameSeperator, actual.Type);
            actual = target.GetNextToken();
            Assert.AreEqual(JsonToken.TokenType.ArrayStart, actual.Type);
            actual = target.GetNextToken();
            Assert.AreEqual(JsonToken.TokenType.True, actual.Type);
            actual = target.GetNextToken();
            Assert.AreEqual(JsonToken.TokenType.MemberSeperator, actual.Type);
            actual = target.GetNextToken();
            Assert.AreEqual(JsonToken.TokenType.String, actual.Type);
            Assert.AreEqual("Value2", actual.Value);
            actual = target.GetNextToken();
            Assert.AreEqual(JsonToken.TokenType.MemberSeperator, actual.Type);
            actual = target.GetNextToken();
            Assert.AreEqual(JsonToken.TokenType.False, actual.Type);
            actual = target.GetNextToken();
            Assert.AreEqual(JsonToken.TokenType.ArrayEnd, actual.Type);
            actual = target.GetNextToken();
            Assert.AreEqual(JsonToken.TokenType.MemberSeperator, actual.Type);
            actual = target.GetNextToken();
            Assert.AreEqual(JsonToken.TokenType.String, actual.Type);
            Assert.AreEqual("sub", actual.Value);
            actual = target.GetNextToken();
            Assert.AreEqual(JsonToken.TokenType.NameSeperator, actual.Type);
            actual = target.GetNextToken();
            Assert.AreEqual(JsonToken.TokenType.ObjectStart, actual.Type);
            actual = target.GetNextToken();
            Assert.AreEqual(JsonToken.TokenType.String, actual.Type);
            Assert.AreEqual("subname", actual.Value);
            actual = target.GetNextToken();
            Assert.AreEqual(JsonToken.TokenType.NameSeperator, actual.Type);
            actual = target.GetNextToken();
            Assert.AreEqual(JsonToken.TokenType.Number, actual.Type);
            Assert.AreEqual("1234", actual.Value);
            Assert.AreEqual(1234, actual.Number);
            actual = target.GetNextToken();
            Assert.AreEqual(JsonToken.TokenType.ObjectEnd, actual.Type);
            actual = target.GetNextToken();
            Assert.AreEqual(JsonToken.TokenType.ObjectEnd, actual.Type);

            actual = target.GetNextToken();
            Assert.AreEqual(null, actual);
        }
Пример #9
0
        public void GetNextTokenTestEscapeUniCode()
        {
            TokenStream target = new TokenStream("'\\uABCD'");
            var actual = target.GetNextToken();
            Assert.AreEqual(JsonToken.TokenType.String, actual.Type);
            Assert.AreEqual("\uABCD", actual.Value);

            target = new TokenStream("'\\u0000'");
            actual = target.GetNextToken();
            Assert.AreEqual(JsonToken.TokenType.String, actual.Type);
            Assert.AreEqual("\u0000", actual.Value);

            target = new TokenStream("'\\uFFFF'");
            actual = target.GetNextToken();
            Assert.AreEqual(JsonToken.TokenType.String, actual.Type);
            Assert.AreEqual("\uFFFF", actual.Value);
        }
Пример #10
0
 /// <summary>
 /// Parses the json document into the appropriate expression/dictionary
 /// </summary>
 public static object Parse(string jsonAsText)
 {
     TokenStream ts = new TokenStream(jsonAsText);
     return ParseExpression(null, ts);
 }
Пример #11
0
 public void GetNextTokenTestEscapeSingleQuoteSimple()
 {
     TokenStream target = new TokenStream("'{\\'id\\':\\'Activitiylist\\'}'");
     var actual = target.GetNextToken();
     Assert.AreEqual(JsonToken.TokenType.String, actual.Type);
     Assert.AreEqual("{'id':'Activitiylist'}", actual.Value);
 }
Пример #12
0
 public void GetNextTokenTestEscapeSingleQuoteFull()
 {
     TokenStream target =
         new TokenStream(
             "'{\\'id\\':\\'Activitiylist\\',\\'properties\\':{\\'id\\':{\\'type\\':\\'any\\'},\\'title\\':{\\'type\\':\\'any\\'},\\'items\\':{\\'items\\':{\\'$ref\\':\\'ChiliActivitiesResourceJson\\'},\\'type\\':\\'array\\'},\\'updated\\':{\\'type\\':\\'string\\'},\\'links\\':{\\'additionalProperties\\':{\\'items\\':{\\'properties\\':{\\'title\\':{\\'type\\':\\'any\\'},\\'height\\':{\\'type\\':\\'any\\'},\\'count\\':{\\'type\\':\\'any\\'},\\'updated\\':{\\'type\\':\\'string\\'},\\'width\\':{\\'type\\':\\'any\\'},\\'type\\':{\\'type\\':\\'any\\'},\\'href\\':{\\'type\\':\\'any\\'}},\\'type\\':\\'object\\'},\\'type\\':\\'array\\'},\\'type\\':\\'object\\'},\\'kind\\':{\\'default\\':\\'buzz#activityFeed\\',\\'type\\':\\'string\\'}},\\'type\\':\\'object\\'}'");
     var actual = target.GetNextToken();
     Assert.AreEqual(JsonToken.TokenType.String, actual.Type);
     Assert.AreEqual(
         "{\'id\':\'Activitiylist\',\'properties\':{\'id\':{\'type\':\'any\'},\'title\':{\'type\':\'any\'},\'items\':{\'items\':{\'$ref\':\'ChiliActivitiesResourceJson\'},\'type\':\'array\'},\'updated\':{\'type\':\'string\'},\'links\':{\'additionalProperties\':{\'items\':{\'properties\':{\'title\':{\'type\':\'any\'},\'height\':{\'type\':\'any\'},\'count\':{\'type\':\'any\'},\'updated\':{\'type\':\'string\'},\'width\':{\'type\':\'any\'},\'type\':{\'type\':\'any\'},\'href\':{\'type\':\'any\'}},\'type\':\'object\'},\'type\':\'array\'},\'type\':\'object\'},\'kind\':{\'default\':\'buzz#activityFeed\',\'type\':\'string\'}},\'type\':\'object\'}",
         actual.Value);
 }
Пример #13
0
        public void GetNextTokenStringsWithBackSlash()
        {
            TokenStream target =
                new TokenStream(
                    "{ \"source\":\"\\u003Ca href=\\\"http:\\/\\/blackberry.com\\/twitter\\\" rel=\\\"nofollow\\\"\\u003ETwitter for BlackBerry\\u00ae\\u003C\\/a\\u003E\" }");
            JsonToken actual = target.GetNextToken();
            Assert.AreEqual(JsonToken.TokenType.ObjectStart, actual.Type);

            actual = target.GetNextToken();
            Assert.AreEqual(JsonToken.TokenType.String, actual.Type);
            Assert.AreEqual("source", actual.Value);

            actual = target.GetNextToken();
            Assert.AreEqual(JsonToken.TokenType.NameSeperator, actual.Type);

            actual = target.GetNextToken();
            Assert.AreEqual(JsonToken.TokenType.String, actual.Type);
            Assert.AreEqual(
                "\u003Ca href=\"http://blackberry.com/twitter\" rel=\"nofollow\"\u003ETwitter for BlackBerry\u00ae\u003C/a\u003E",
                actual.Value);

            actual = target.GetNextToken();
            Assert.AreEqual(JsonToken.TokenType.ObjectEnd, actual.Type);
        }
Пример #14
0
        private static JsonDictionary ParseObject(TokenStream ts)
        {
            // to parse an object, you get the object name, and then parse the value
            JsonToken token = ts.GetNextToken();

            if (token.Type != JsonToken.TokenType.String && token.Type != JsonToken.TokenType.ObjectEnd)
            {
                throw new ArgumentException(
                    "Unable to parse object. Found object " + token +
                    " while looking for property name or object end.");
            }

            JsonDictionary dict = new JsonDictionary();
            if (token.Type != JsonToken.TokenType.ObjectEnd)
            {
                for (JsonToken cur = ts.GetNextToken(); cur != null; cur = ts.GetNextToken())
                {
                    switch (cur.Type)
                    {
                        case JsonToken.TokenType.ObjectEnd:
                            logger.Debug("Found object end");
                            return dict;
                        case JsonToken.TokenType.MemberSeperator:
                            token = ts.GetNextToken();
                            break;
                        case JsonToken.TokenType.NameSeperator:
                            object value = ParseExpression(null, ts);
                            if (dict.ContainsKey(token.Value))
                            {
                                throw new ArgumentException(
                                    "JsonObject contains duplicate definition for [" + token.Value + "]");
                            }
                            dict.Add(token.Value, value);
                            break;
                        default:
                            throw new ArgumentException(
                                "Found invalid Json was expecting } or , or : found " + cur);
                    }
                }
            }
            return dict;
        }
Пример #15
0
 /// <summary>
 /// Parses the json document into the appropriate expression/dictionary
 /// </summary>
 public static object Parse(Stream jsonAsStream)
 {
     TokenStream ts = new TokenStream(jsonAsStream);
     return ParseExpression(null, ts);
 }
Пример #16
0
 public void GetNextTokenNegativeNumberWithDecimals()
 {
     TokenStream target = new TokenStream("-1234.5678");
     JsonToken actual = target.GetNextToken();
     Assert.AreEqual(JsonToken.TokenType.Number, actual.Type);
     Assert.AreEqual(-1234.5678, actual.Number);
     Assert.AreEqual("-1234.5678", actual.Value);
 }
Пример #17
0
 public void GetNextTokenTestEscapeTab()
 {
     TokenStream target = new TokenStream("'\\t'");
     var actual = target.GetNextToken();
     Assert.AreEqual(JsonToken.TokenType.String, actual.Type);
     Assert.AreEqual("\t", actual.Value);
 }