Example #1
0
        public void MalformedKey()
        {
            var json = @"{ ""Age : 5 }";

            var tok = new JSONTokenizer(new StringReader(json));
            var tokenList = new List<JSONToken>();
            foreach (var t in tok)
            {
                tokenList.Add(t);
            }

            Assert.AreEqual(3, tokenList.Count);
            Assert.AreEqual(JSONTokenType.OBJECT_START, tokenList[0].Type);
            Assert.AreEqual(JSONTokenType.STRING, tokenList[1].Type);
            Assert.AreEqual("Age : 5 }", tokenList[1].StrValue);
            Assert.AreEqual(JSONTokenType.EOF, tokenList[2].Type);
        }
Example #2
0
        public void LargeRecurse()
        {
            var tempFile = Path.GetTempFileName();
            try
            {
                using (var fs = new FileStream(tempFile, FileMode.Append))
                {
                    for (int i = 0; 500000 > i; i++)
                    {
                        byte[] buf = Encoding.UTF8.GetBytes("{\"k" + i + "\" : ");
                        fs.Write(buf, 0, buf.Length);
                    }
                    for (int i = 0; 500000 > i; i++)
                    {
                        byte[] buf = Encoding.UTF8.GetBytes("}");
                        fs.Write(buf, 0, buf.Length);
                    }
                    fs.Flush();
                }

                using (var sr = new StreamReader(tempFile))
                {
                    var tok = new JSONTokenizer(sr);
                    var tokenList = new List<JSONToken>();
                    int count = 0;
                    foreach (var t in tok)
                    {
                        if (JSONTokenType.OBJECT_START == t.Type)
                        {
                            ++count;
                        }
                    }
                    Assert.AreEqual(500000, count);
                }
            }
            finally
            {
                File.Delete(tempFile);
            }
        }
Example #3
0
        public static SelectedValue[] SelectValues(TextReader json, string[] selectors, JSONSelectorOptions options)
        {
            if (null == json)
            {
                return new SelectedValue[0];
            }

            // Create tokenizer for the JSON text - this is fed to a Tree Walking Validator
            JSONTokenizer tokenizer = new JSONTokenizer(json);

            // Create an events object for the Tree Walker - events are fired as the walker
            // traverses nodes in the tree.
            JSONWalkingEvents events = new JSONWalkingEvents();

            // The selector subscribes to the Walker Events and selects the appropriate values from the
            // JSON tree.
            JSONSelector selector = new JSONSelector(events, selectors);

            // A JSON Tree Walker that validates the structure of the JSON and also fires events
            // while traversing the tree.
            JSONWalkingValidator walker = new JSONWalkingValidator();
            walker.Walk(tokenizer.GetEnumerator(), events);

            // Retrieve the values selected from the JSON.
            return selector.SelectedValues;
        }
Example #4
0
        public static SQLConstraint GenerateSQLConstraint(IDatabaseService db, Dictionary<string, Type> typeMappings, string json)
        {
            var tokenizer = new JSONTokenizer(new StringReader(json));

            var events = new JSONWalkingEvents();

            var constraint = new JSONToSQLConstraint(db, events, typeMappings);

            JSONWalkingValidator walker = new JSONWalkingValidator();
            walker.Walk(tokenizer.GetEnumerator(), events);

            return constraint.GenerateSQLConstraint();
        }
Example #5
0
        private static bool _Run()
        {
            bool                     result        = true;
            IJSONTokenizer           JsonTokenizer = new JSONTokenizer();
            IEnumerable <IJSONToken> tokens        = JsonTokenizer.Tokenize("{ \"id\" : \"one\" }");
            int index = 0;

            foreach (IJSONToken token in tokens)
            {
                if (0 == index)
                {
                    result = result && (token.Kind == JSONTokenType.OpenCurly);
                }
                if (1 == index)
                {
                    result = result && (token.Kind == JSONTokenType.QuotedString);
                }
                if (1 == index)
                {
                    result = result && (token.Value == "id");
                }
                if (2 == index)
                {
                    result = result && (token.Kind == JSONTokenType.Colon);
                }
                if (3 == index)
                {
                    result = result && (token.Kind == JSONTokenType.QuotedString);
                }
                if (3 == index)
                {
                    result = result && (token.Value == "one");
                }
                if (4 == index)
                {
                    result = result && (token.Kind == JSONTokenType.CloseCurly);
                }
                if (4 == index)
                {
                    result = result && (token.Value == "}");
                }
                if (5 == index)
                {
                    result = result && (token.Kind == JSONTokenType.EOF);
                }
                index++;
            }
            bool threw_parse_exception = false;

            try {
                tokens = JsonTokenizer.Tokenize("9.45.6");
                foreach (IJSONToken token in tokens)
                {
                    string s = token.Value;
                }
            } catch (ParseException) {
                threw_parse_exception = true;
            }
            result = result && threw_parse_exception;
            threw_parse_exception = false;
            try {
                tokens = JsonTokenizer.Tokenize(" - kdoif");
                foreach (IJSONToken token in tokens)
                {
                    string s = token.Value;
                }
            } catch (ParseException) {
                threw_parse_exception = true;
            }
            result = result && threw_parse_exception;

            // now test all the token types
            tokens = JsonTokenizer.Tokenize("{ } [] uiuo  \"literal\" : ; , .844 -43");
            index  = 0;
            foreach (IJSONToken token in tokens)
            {
                if (0 == index)
                {
                    result = result && (token.Kind == JSONTokenType.OpenCurly) && (token.Value == "{");
                }
                if (1 == index)
                {
                    result = result && (token.Kind == JSONTokenType.CloseCurly) && (token.Value == "}");
                }
                if (2 == index)
                {
                    result = result && (token.Kind == JSONTokenType.OpenBracket) && (token.Value == "[");
                }
                if (3 == index)
                {
                    result = result && (token.Kind == JSONTokenType.CloseBracket) && (token.Value == "]");
                }
                if (4 == index)
                {
                    result = result && (token.Kind == JSONTokenType.UnquotedString) && (token.Value == "uiuo");
                }
                if (5 == index)
                {
                    result = result && (token.Kind == JSONTokenType.QuotedString) && (token.Value == "literal");
                }
                if (6 == index)
                {
                    result = result && (token.Kind == JSONTokenType.Colon) && (token.Value == ":");
                }
                if (7 == index)
                {
                    result = result && (token.Kind == JSONTokenType.Semicolon) && (token.Value == ";");
                }
                if (8 == index)
                {
                    result = result && (token.Kind == JSONTokenType.Comma) && (token.Value == ",");
                }
                if (9 == index)
                {
                    result = result && (token.Kind == JSONTokenType.Number) && (token.Value == ".844");
                }
                if (10 == index)
                {
                    result = result && (token.Kind == JSONTokenType.Number) && (token.Value == "-43");
                }
                if (11 == index)
                {
                    result = result && (token.Kind == JSONTokenType.EOF);
                }
                index++;
            }
            return(result);
        }
Example #6
0
        public void NestedObjectLiteral()
        {
            var json = @"{ ""Action"" : { ""Name"" : ""Parse"" } }";
            var tok = new JSONTokenizer(new StringReader(json));
            var tokenList = new List<JSONToken>();
            foreach (var t in tok)
            {
                tokenList.Add(t);
            }

            Assert.AreEqual(10, tokenList.Count);

            Assert.AreEqual(JSONTokenType.OBJECT_START, tokenList[0].Type);
            Assert.AreEqual(JSONTokenType.STRING, tokenList[1].Type);
            Assert.AreEqual("Action", tokenList[1].StrValue);
            Assert.AreEqual(JSONTokenType.COLON, tokenList[2].Type);

            Assert.AreEqual(JSONTokenType.OBJECT_START, tokenList[3].Type);
            Assert.AreEqual(JSONTokenType.STRING, tokenList[4].Type);
            Assert.AreEqual("Name", tokenList[4].StrValue);
            Assert.AreEqual(JSONTokenType.COLON, tokenList[5].Type);
            Assert.AreEqual(JSONTokenType.STRING, tokenList[6].Type);
            Assert.AreEqual("Parse", tokenList[6].StrValue);

            Assert.AreEqual(JSONTokenType.OBJECT_END, tokenList[7].Type);
            Assert.AreEqual(JSONTokenType.OBJECT_END, tokenList[8].Type);
            Assert.AreEqual(JSONTokenType.EOF, tokenList[9].Type);
        }
Example #7
0
        public void ValuesArray()
        {
            var json = @"{ ""Vals"" : [1.453,null,""str val"",true,false] }";
            var tok = new JSONTokenizer(new StringReader(json));
            var tokenList = new List<JSONToken>();
            foreach (var t in tok)
            {
                tokenList.Add(t);
            }

            Assert.AreEqual(16, tokenList.Count);

            Assert.AreEqual(JSONTokenType.OBJECT_START, tokenList[0].Type);
            Assert.AreEqual(JSONTokenType.STRING, tokenList[1].Type);
            Assert.AreEqual("Vals", tokenList[1].StrValue);
            Assert.AreEqual(JSONTokenType.COLON, tokenList[2].Type);
            Assert.AreEqual(JSONTokenType.ARRAY_START, tokenList[3].Type);
            Assert.AreEqual(JSONTokenType.NUMBER, tokenList[4].Type);
            Assert.AreEqual(1.453m, tokenList[4].NumValue);
            Assert.AreEqual(JSONTokenType.COMMA, tokenList[5].Type);

            Assert.AreEqual(JSONTokenType.KEYWORD_NULL, tokenList[6].Type);
            Assert.AreEqual(JSONTokenType.COMMA, tokenList[7].Type);

            Assert.AreEqual(JSONTokenType.STRING, tokenList[8].Type);
            Assert.AreEqual("str val", tokenList[8].StrValue);
            Assert.AreEqual(JSONTokenType.COMMA, tokenList[9].Type);

            Assert.AreEqual(JSONTokenType.KEYWORD_TRUE, tokenList[10].Type);
            Assert.AreEqual(JSONTokenType.COMMA, tokenList[11].Type);

            Assert.AreEqual(JSONTokenType.KEYWORD_FALSE, tokenList[12].Type);

            Assert.AreEqual(JSONTokenType.ARRAY_END, tokenList[13].Type);

            Assert.AreEqual(JSONTokenType.OBJECT_END, tokenList[14].Type);
            Assert.AreEqual(JSONTokenType.EOF, tokenList[15].Type);
        }
Example #8
0
        public void ObjectLiteral()
        {
            var json = @"{ ""Age"" : 5 }";

            var tok = new JSONTokenizer(new StringReader(json));
            var tokenList = new List<JSONToken>();
            foreach (var t in tok)
            {
                tokenList.Add(t);
            }

            Assert.AreEqual(6, tokenList.Count);

            Assert.AreEqual(JSONTokenType.OBJECT_START, tokenList[0].Type);
            Assert.AreEqual(JSONTokenType.STRING, tokenList[1].Type);
            Assert.AreEqual("Age", tokenList[1].StrValue);
            Assert.AreEqual(JSONTokenType.COLON, tokenList[2].Type);
            Assert.AreEqual(JSONTokenType.NUMBER, tokenList[3].Type);
            Assert.AreEqual(5m, tokenList[3].NumValue);
            Assert.AreEqual(JSONTokenType.OBJECT_END, tokenList[4].Type);
            Assert.AreEqual(JSONTokenType.EOF, tokenList[5].Type);
        }
Example #9
0
        public void NumberArray()
        {
            var json = @"{ ""Ages"" : [5,7,8] }";
            var tok = new JSONTokenizer(new StringReader(json));
            var tokenList = new List<JSONToken>();
            foreach (var t in tok)
            {
                tokenList.Add(t);
            }

            Assert.AreEqual(12, tokenList.Count);

            Assert.AreEqual(JSONTokenType.OBJECT_START, tokenList[0].Type);
            Assert.AreEqual(JSONTokenType.STRING, tokenList[1].Type);
            Assert.AreEqual("Ages", tokenList[1].StrValue);
            Assert.AreEqual(JSONTokenType.COLON, tokenList[2].Type);
            Assert.AreEqual(JSONTokenType.ARRAY_START, tokenList[3].Type);
            Assert.AreEqual(JSONTokenType.NUMBER, tokenList[4].Type);
            Assert.AreEqual(5m, tokenList[4].NumValue);
            Assert.AreEqual(JSONTokenType.COMMA, tokenList[5].Type);

            Assert.AreEqual(JSONTokenType.NUMBER, tokenList[6].Type);
            Assert.AreEqual(7m, tokenList[6].NumValue);
            Assert.AreEqual(JSONTokenType.COMMA, tokenList[7].Type);

            Assert.AreEqual(JSONTokenType.NUMBER, tokenList[8].Type);
            Assert.AreEqual(8m, tokenList[8].NumValue);

            Assert.AreEqual(JSONTokenType.ARRAY_END, tokenList[9].Type);

            Assert.AreEqual(JSONTokenType.OBJECT_END, tokenList[10].Type);
            Assert.AreEqual(JSONTokenType.EOF, tokenList[11].Type);
        }