Thursday, October 28, 2010

JSON (without the fear of "Friday the 13th")

JavaScript Object Notation, JSON, is simply a format for data exchange which is simpler than XML. As XML has rules for creating its documents, JSON has some simpler rules and is lighter because it does not use tags (

JSON is being widely used in most programming languages and you can find several API's that make the use of JSON to be very simple. For JAVA, stands Google's "Gson" ( Its simplicity and its use of the Java Reflection API, allows developers to create JSON representations of their objects in a transparent and easy way... in just 2 lines of code!

However, mobile devices can not use the Java Refelction API and therefore can not use Gson. If you want to be able to use JSON in Java ME, you can use the JSON ME library plus a little bit of more code to map the objects... Anyway it's still a very attractive option for data exchange between servers and mobile clients.

I still remember when I started with Java ME, and I wanted to transmit information from the server to the mobile client, I used to concatenate every attribute value with a special character that I could split later. For example, I used to create the String "name&lastName&address..." and sent it to the mobile client, then I split the String using the character '&' in order to get all the data and map my objects... Where were you JSON? (LOL).

The JSON ME library has disappeared from the JSON official website (, but some people have uploaded it again so you can use it for development. From this forum you can follow the link to Download the library:

Or if you are someone who likes to have everything under control, you can download the source code and compile JSON ME yourself:

I have already downloaded the source code and build the library so I can use it in my projects. In a future post I'll explain the steps to download the source code from the repository of to NetBeans 6.9.

What if you want to transmit binary data? Images? It is possible, but to do so using JSON, you must encode binary data to  its character representation and once in the mobile client, re-transform it to binary data. However, it is not recommended to send binary data as characters, but to send a URL so the mobile client can then directly access binary resources on the server.

What about special characters like accents? You should be vey careful with this issue, it is possible that special characters are not transmitted correctly if you are not using the correct charset. I Recommend on the server side, that the response has the charset "UTF-8" and the content type "application/json" (assigned by IANA). On the Mobile client side, when creating the string received from the transmission you should use the charset "UTF-8" too.

Wait for Gson examples and the integration of Servlets/Gson - Java ME/JSON in future posts.

See you soon!


JSON. [online]. Available from Internet: [accessed on October the 26th 2010].

google-gson. Agosto 19 de 2010. Google Code [online]. Available from Internet: [accessed on October the 26th 2010].

meapplicationdevelopers: Subversion. 2007. Oracle [online]. Available from Internet: [accessed on October the 27th 2010].

Application Media Types. Internet Assigned Numbers Authority (IANA) [online]. Available from Internet: [accessed on October the 28th 2010].

Using JavaScript Object Notation (JSON) in Java ME for Data Interchange. Agosto 2008. Oracle [online]. Available from Internet: [accessed on October the 26th 2010].