Exemplo n.º 1
0
        private static object JSonToObject(string json)
        {
            JScriptEngine engine = new JScriptEngine();
            IActiveScript script = (IActiveScript)engine;
            MySite site = new MySite();
            engine.SetScriptSite(site);

            IActiveScriptParse32 scriptParser = (IActiveScriptParse32)engine;
            scriptParser.InitNew();
            engine.SetScriptState(ScriptState.SCRIPTSTATE_CONNECTED);

            // SCRIPTTEXT_ISEXPRESSION
            // If the distinction between a computational expression and a 
            // statement is important but syntactically ambiguous in the 
            // script language, this flag specifies that the scriptlet is 
            // to be interpreted as an expression, rather than as a 
            // statement or list of statements. By default, statements are 
            // assumed unless the correct choice can be determined from 
            // the syntax of the scriptlet text. 
            const int SCRIPTTEXT_ISEXPRESSION = 0x00000020;
            // Tricky: http://msdn2.microsoft.com/en-us/library/system.runtime.interopservices.variantwrapper.aspx.
            object result = null;
            System.Runtime.InteropServices.ComTypes.EXCEPINFO exceptionInfo;

            Trace.WriteLine("Parsing JSON:");
            if (json.Length < 256)
            {
                Trace.WriteLine(json);
            }
            else
            {
                Trace.WriteLine(json.Substring(0, 128) + "..." + json.Substring(json.Length - 128, 128));
            }

            scriptParser.ParseScriptText(json, null, null, null, 0, 0,
                SCRIPTTEXT_ISEXPRESSION, ref result, out exceptionInfo);

            if (exceptionInfo.scode != 0)
            {
                throw new Exception(exceptionInfo.bstrDescription);
            }

            //engine.Close();
            return result;
        }