View Full Version : JsonConverter writing type info?

21 Oct 2009, 6:14 AM
I have a siutation where I need to make an AJAX call to post some bean objects. Rather than encode the list of ModelData into form data where I am adding a numeric counter to the keys (for example: "name", "name_1", "name_2" for each model data etc.) I thought..... convert my ModelDarta to JSONObjects and then call toString() on these and send that down (afterall this is how I read this from the server) and JSON lends itself well to "structured data".

I noticed that in JsonConverter when it encodes values, it inserts a type: prefix. for example, Strings get a "s:", integers get a "i:" for example (interestingly enough it does not handle long or double). This does not conform to the JSON language syntax (see http://www.json.org/) so string or number syntax, so I am wondering why this was chosen. I did try throwing this into Jettison to see if it would decode it and as I suspected I ended up with with "s:myname" for example as a name value.

So given

ModelData data = new BaseModelData();
JSONObject jsObj = JsonConverter.encode( data );
String s = jsObj.toString();

In this case s = {"name":"s:foo","id":"i:1"}
when it should be {"name":"foo","id":1} according to JSON standards.

My question is why was this done and is there any way to correct this? I not, I suspect I'll need to write my own postprocessing utility to change or "fix" the string output.


21 Oct 2009, 6:25 AM
It was done so you can get the type again when reading the json, for example that you know that it was a date. That class is a helper for the cookieprovider.

21 Oct 2009, 6:28 AM
So it was meant for use primarily within the client and not for sending through HTTP Requests to the server?

21 Oct 2009, 6:32 AM
Yes exactly.