public void JSONObject_RenderTests() { JSONObject obj = new JSONObject(); Assert.AreEqual(obj.Render(), "null"); obj.Value = new JSONObjectEmpty(); Assert.AreEqual(obj.Render(), "{}"); obj.Attr("propertyName", "propertyValue"); Assert.AreEqual(obj.Attr("propertyName").Value, "propertyValue"); Assert.AreEqual(obj.Render(), "{\"propertyName\":\"propertyValue\"}"); obj = new JSONObject(new { property1 = "value1", property2 = 5 }); Assert.AreEqual(obj.Render(), "{\"property1\":\"value1\",\"property2\":5}"); }
protected override void RenderContents(HtmlTextWriter output) { List <MethodInfo> webmethods = this.Page.GetType().BaseType.GetMethods(BindingFlags.Public | BindingFlags.Static) .Where(m => m.GetCustomAttributes(false).OfType <WebMethodAttribute>().Count() > 0).ToList(); List <MethodInfo> jsmethods = this.Page.GetType().GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance) .Where(m => m.GetCustomAttributes(false).OfType <WFJScriptMethodAttribute>().Count() > 0).ToList(); JSONObject pageJSO = new JSONObject(); foreach (MethodInfo mi in webmethods) { string argCSV = ""; string argJSON = ""; JSONObject argObject = new JSONObject(); foreach (ParameterInfo pi in mi.GetParameters()) { argCSV += argCSV == "" ? pi.Name : "," + pi.Name; argObject.Attr(pi.Name, new JSONObjectLiteral() { Value = pi.Name }); } argJSON = argObject.Render(); pageJSO.Attr(mi.Name, new JSONObjectLiteral() { Value = "function(" + argCSV + "){" + "var retObj;" + "webfu.callPage(\"" + Page.Request.CurrentExecutionFilePath + "/" + mi.Name + "\",{" + "data:" + argJSON + "," + "success:function(msg){ retObj = msg;" + "}," + "async:false," + "error:function(a) { throw a.responseText; }" + (ReturnSimpleString ? "}); return retObj.d;}" : "}); return retObj;}") }); pageJSO.Attr(mi.Name + "Async", new JSONObjectLiteral() { Value = "function(successCallBack,errorCallBack," + argCSV + "){" + "webfu.callPage(\"" + Page.Request.CurrentExecutionFilePath + "/" + mi.Name + "\",{" + "data:" + argJSON + "," + (ReturnSimpleString ? "success:function(a,b,c) { successCallBack(a.d,b,c); },error:errorCallBack,async:true" : "success:successCallBack,error:errorCallBack,async:true") + "}); }" }); } foreach (MethodInfo mi in jsmethods) { pageJSO.Attr(mi.Name, new JSONObjectLiteral() { Value = "function(){" + "webfu.submitForm('" + mi.Name + "');" + "}" }); } output.Write("<script>this.webfu = this.webfu || {}; webfu.page = " + pageJSO.Render() + ";</script>"); }