Passing JSON serialized data via hidden field
In the past, I've usually done the following:
- Created a C# object that I can easily serialize to XML (.Net can do this natively and there are JSON serializers out there as well). (Actually I think .Net may do JSON as well... not sure though)
- Take this object and share it with the client via a hidden field or lazy load it on the client via an AJAX request.
- The client then parses and manipulates the XML (jQuery can do this) and sends it back to the server via AJAX or a hidden field via a POST.
- Lastly I deserialize the XML (or JSON) from the client back into an Object and work with it from there.
So I'm not sure what you're trying to gain through encoding, but I think the expense of transforming the object from JSON (or XML) to something smaller is too much overhead for any packaging/shrinking benefit. (Unless your a super high traffic site concerned more about bandwidth usage than server/client side processing.)
EXAMPLE
Hopefully this gives you an idea of how to accomplish this. Let me know if you have any more questions.
<html> <head> </head> <body> <input type="button" id="btnObject" value="Show Object" /> <input type="button" id="btnShowHid" value="Show Hidden Input Value" /> <input type="hidden" id="hidInput" value="" /> </body> </html> <script type='text/javascript' src='https://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js'></script><script type='text/javascript' src='https://github.com/douglascrockford/JSON-js/raw/master/json2.js'></script> <script> //Your JSON Object var myJSONObject = {"bindings": [ {"ircEvent": "PRIVMSG", "method": "newURI", "regex": "^http://.*"}, {"ircEvent": "PRIVMSG", "method": "deleteURI", "regex": "^delete.*"}, {"ircEvent": "PRIVMSG", "method": "randomURI", "regex": "^random.*"} ] }; //jQuery Document Ready Event $(function(){ //Get a reference to your hidden field var $hidInput = $("#hidInput"); //Use json2.js library to convert the json object to a string //and assign it to the hidden input's value //NOTE: ASP.NET hidden input control reduces to a hidden input so you can treat them the same. $hidInput.val(JSON.stringify(myJSONObject)); //Set up click events to view object and hidden value $("#btnShowHid").click(function(){ alert($hidInput.val()); }); $("#btnObject").click(function(){ alert(myJSONObject); }); });</script>