Weird behavior with json2.js

Nov 13, 2009 at 12:33 AM

You might be familar with json2.js.  It's a JSON parser and stringifier written in Javascript; and it's used as an alternative to eval() when JSON comes from an untrusted source.

I'm getting weird errors when I try to load json2.js into Jint.  A bunch of stuff gets written to the console and then I get a null reference error.

My test program and results are pasted below.


using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;

using Jint;

namespace Play_With_Jint
{
    class Program
    {
        static void Main(string[] args)
        {
            WebRequest wr = HttpWebRequest.Create("http://www.json.org/json2.js");
            WebResponse r = wr.GetResponse();
            TextReader tr = new StreamReader(r.GetResponseStream());
            string json2 = tr.ReadToEnd();

            JintEngine jintEngine = new JintEngine();

            try
            {
                //object result = jintEngine.Run(String.Format("return ({0})", json2));
                object result = jintEngine.Run(json2);

                Console.WriteLine(result.GetType().ToString());
                Console.WriteLine(result.ToString());
            }
            catch (Exception e)
            {
                Console.WriteLine("Error ({0})", e.GetType());
                Console.WriteLine(e.Message);
                Console.WriteLine(e.StackTrace);
            }

            Console.ReadKey();
        }
    }
}


line 196:14 no viable alternative at character 'b'
line 196:19 no viable alternative at character '\'
line 196:20 no viable alternative at character '\'
line 196:24 mismatched character '\n' expecting '''
line 199:14 no viable alternative at character 'f'
line 199:19 no viable alternative at character '\'
line 199:20 no viable alternative at character '\'
line 199:24 mismatched character '\n' expecting '''
line 196:21 missing ':' at 'b'
line 197:12 no viable alternative at input ''\t''
Error (System.NullReferenceException)
Object reference not set to an instance of an object.
   at JavaScriptParser.expression()
   at JavaScriptParser.variableDeclarationStatement()
   at JavaScriptParser.variableDeclaration()
   at JavaScriptParser.statement()
   at JavaScriptParser.blockStatement()
   at JavaScriptParser.functionExpression()
   at JavaScriptParser.value()
   at JavaScriptParser.primaryExpression()
   at JavaScriptParser.unaryExpressionNotPlusMinus()
   at JavaScriptParser.unaryExpression()
   at JavaScriptParser.multiplicativeExpression()
   at JavaScriptParser.additiveExpression()
   at JavaScriptParser.relationalExpression()
   at JavaScriptParser.equalityExpression()
   at JavaScriptParser.booleanAndExpression()
   at JavaScriptParser.booleanOrExpression()
   at JavaScriptParser.ternaryExpression()
   at JavaScriptParser.expression()
   at JavaScriptParser.primaryExpression()
   at JavaScriptParser.unaryExpressionNotPlusMinus()
   at JavaScriptParser.unaryExpression()
   at JavaScriptParser.multiplicativeExpression()
   at JavaScriptParser.additiveExpression()
   at JavaScriptParser.relationalExpression()
   at JavaScriptParser.equalityExpression()
   at JavaScriptParser.booleanAndExpression()
   at JavaScriptParser.booleanOrExpression()
   at JavaScriptParser.ternaryExpression()
   at JavaScriptParser.expression()
   at JavaScriptParser.expressionStatement()
   at JavaScriptParser.statement()
   at JavaScriptParser.program()
   at Jint.JintEngine.Compile(String source)
   at Jint.JintEngine.Run(String script)
   at Play_With_Jint.Program.Main(String[] args) in C:\Documents and Settings\Administrator\My Documents\Visual Studio 2005\Projects\Play_With_Jint\Play_With_Jint\Program.cs:line 25

Coordinator
Nov 16, 2009 at 7:02 AM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.