JSON Template

Aug 10, 2010 at 5:14 PM
Edited Aug 10, 2010 at 5:20 PM
Could you help me with an example using JSON Template[1] via v8sharp?

I would like have an ashx that receives a GET request and responds with the results of applying a JSON Template to a .NET generic list of a created .NET class type.

Please and thanks!


------------
[1]: http://json-template.googlecode.com/svn/trunk/doc/Introducing-JSON-Template.html
Aug 16, 2010 at 8:54 PM
Edited Aug 16, 2010 at 8:57 PM
Never-mind:

    public partial class JSServerWrapper
    {
        public JSServerWrapper()
        {
            QueryString = Handler.Context.Request.QueryString.ToJSON();
            Form = (
                (Handler.Context.Request.Form.HasKeys()) ?
                    Handler.Context.Request.Form.ToJSON() :
                    @"{ ""empty"": true }"
                );
        }

        public string QueryString { get; set; }
        public string Form { get; set; }
        public void ResponseWrite(string s)
        {
            Handler.Context.Response.Write(s);
        }
    }

    public class Handler : IHttpHandler, IRequiresSessionState
    {
        public static HttpContext Context;
        private static readonly string includes = ((new List<string>()
        {
            //"{0}htmlparser.js".With(Global.JSServerIncludesPath),
            
            "{0}json2.js".With(Global.JSServerIncludesPath),
            "{0}underscore.js".With(Global.JSServerIncludesPath),
            "{0}date.js".With(Global.JSServerIncludesPath),
            "{0}all.js".With(Global.JSServerIncludesPath),
            "{0}json-template.js".With(Global.JSServerIncludesPath),
            "{0}templates.js".With(Global.JSServerIncludesPath),
        }).ConvertAll(new Converter<string, string>(File.ReadAllText))).ToStringJoin("\r\n\r\n");
        private static V8Engine engine;

        public void ProcessRequest(HttpContext context)
        {
            Context = context;

            string t = Context.Request.QueryString["type"].ToUpperInvariant();
            string filename = "payload";

            switch (Context.Request.HttpMethod.ToUpperInvariant())
            {
                case "GET":
                    switch (t)
                    {
                        case "JS":
                            filename = "";
                            ServerSide();
                            break;
                    }
                    break;
            }
            if (filename.IsNotNullOrEmpty())
            {
                Context.Response.ContentType = "application/json";
                Context.Response.AddHeader("Content-Disposition", ("inline;filename={0}.json".With(filename)));
            }
        }

        private void ServerSide()
        {
            engine = engine ?? V8Engine.Create();
            engine.Register<JSServerWrapper>("Server", new JSServerWrapper());
            V8Script script = engine.Compile(
                "{0}\r\n\r\n{1}".With(
                    includes,
                    File.ReadAllText("{0}Handler.ashx.js".With(Global.JSServerIncludesPath))
                )
            );
            if (script.HasExceptions) { Context.Response.Write(script.Exceptions[0].Message); }
            else { try { engine.Execute(script); } catch (Exception ex) { Context.Response.Write(ex.Message); } }
        }
Coordinator
Aug 17, 2010 at 11:56 AM
I don't think that will be very thread safe
Aug 17, 2010 at 1:03 PM
Edited Aug 17, 2010 at 1:05 PM
Because of the static V8Engine? I was concerned about Compilation and Execution killing CPU. My thinking was that since the V8Script was executed every time that would suffice.

I've tested it with multiple connections and thought all was well -- but I guess you're saying I should not use static variables and just eat the Compile/Execute hit, huh?

Will it still be performant?