In ASP.NET WebForms I want to pass arbitrary data from server to client and back again. I am serializing to JSON and have been simply producing JavaScript that creates the object on the client. I have no problem sending data to the server with ajax, but there are situations where I also want to send a Javascript object data back to the server on postbacks. So I guess it needs to be in a hidden field.
A couple general questions about this.
1) What is the best way to do this in terms of minimizing complexity and optimizing space and efficiency? In researching this I discovered Protocol Buffers but there does not seem to be a good C# implementation. I did find one, but it was a couple years old and self-described as buggy so that scared me.
2) If I just pass a JSON string, how can I be sure it will be safe to include as the value for a hidden field? Is there any reason I might not want to do this? I could Base64 encode, but it seems like this adds a lot of overhead. What is considered the best or preferred method?
In the past, I've usually done the following:
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.