Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
New Developer
stoaster
Posts: 22
Registered: ‎10-23-2008
My Device: Bold

Tutorial on using JSON for object exchange between client and server

Hi everyone,

 

I've been using JSON to transfer objects over HTTP(S) between BlackBerry devices and services for sometime now. I also use it on Android. Here's a tutorial (http://developerlife.com/tutorials/?p=624) with helpful tips on how to do object exchange between clients (on BlackBerry and Android) and services. Code is provided for a JSON object generator that generates java code for you to do serialization and deserialization. This stuff is in used in this mobile blogging BlackBerry app (for 4.6 or higher OS) -  http://screamingtoaster.com/?p=234.

 

I'm about to release a mobile blogging app in the next few weeks which uses JSON as well. This BB app will allow you to edit your blog from Android and BlackBerry smartphones. If you have your own blog, you will be able to view/edit/manage it on the fly from BlackBerry and Android smartphones. You can see a beta of this (tied to 1 blog) running here - http://screamingtoaster.com/?p=234. You can also download it and beta test it from here http://developerlife.com/bbota/. Note that this beta is only tied to one blog.

 

Here's a list of other tutorials on developerlife that might help with BB development -  http://developerlife.com/tutorials/?cat=63

 

Enjoy,

Nazmul.

Developer
marchywka
Posts: 1,415
Registered: ‎07-30-2008
My Device: Not Specified

Re: Tutorial on using JSON for object exchange between client and server

From what I'd seen, Android was using JSON as a synonym for a binary format at least

for the GPS data and a couple of notes I'd skimmed. The actual java overhead for

a binary format is probably similar and network usage can decrease but there can

be problems with human readability and familiarity with simple text processing tools.

If you want to modify an  ASCII file or check an ASCII stream that is pretty

easy but binary becomes harder to manipulate without a little care.

I would mention that once you have something like urlencode and decode scripts

however, it really isn't that hard to convert back and forth easily.

 

Developer
mreed
Posts: 1,041
Registered: ‎07-16-2008
My Device: ಠ_ಠ

Re: Tutorial on using JSON for object exchange between client and server

[ Edited ]

There is a good start of a json pull parser on google code at http://code.google.com/p/jsonpull/

svn: http://jsonpull.googlecode.com/svn/trunk/

 

There is some functionality missing, but with a little thought you can update it correctly. I had made quite a few changes and submitted the code back to the guy running the project, but he decided not to accept the updates for some reason... which is beyond me.

 

There is a good json formatter at http://jsonformatter.curiousconcept.com/

Json usually does not have line breaks when sent through the network, so a formatter makes readability very easy.

 

I prefer json over xml.

Message Edited by mreed on 03-12-2009 10:19 AM
Developer
marchywka
Posts: 1,415
Registered: ‎07-30-2008
My Device: Not Specified

Re: Tutorial on using JSON for object exchange between client and server

The term "JSON object" seems to be used

for anything that looks like a properties table ( name-value pairs)

and can be transmitted in "json" or binary formats ( I think BISON or something

comes up along with others in some forums ). 

 

Is android largely going with the "protocol buffer" approach?

 

I'm starting to think there would be benefits to this in many cases even if

BW is reasonably good as with wifi.

 

 

New Developer
rprabhumanickam
Posts: 4
Registered: ‎05-22-2009
My Device: Not Specified

Re: Tutorial on using JSON for object exchange between client and server

You prefer, JSON is the best tool for Object exchange. Is JSON the only way to support serialization or object exchange?.

 

And I am confused with so many tools which support for JSON like, json-lib, xstream, etc. which one is the best?

Which is popular tool?. Is everybody using JSON?. 

New Developer
stoaster
Posts: 22
Registered: ‎10-23-2008
My Device: Bold

Re: Tutorial on using JSON for object exchange between client and server

@ - Google's protocol buffers don't work if you don't have Java 5 language support, so that's out of the question for BB. Personally, I find JSON much easier to work with, and I've created my own JSON code generator, which handles all the dirty work of creating Java code for my data models. On Android, you could use Proto buffers, but I would suggest using JSON. I develop for desktop/web/BB/Android and so I like to use JSON, I just have to write things once. You can compress the Strings generated by JSON and save a ton of bandwidth. All of this is used in my ScreamingToaster ONE Platform apps - Wicked, MyListy.

 

Here's a list of Android tutorials if you're interested. I'm going to write a new set of BlackBerry tutorials very soon.

Developer
Ted_Hopp
Posts: 1,305
Registered: ‎01-21-2009
My Device: Not Specified

Re: Tutorial on using JSON for object exchange between client and server

@rprabhumanickam--JSON is one of many ways to support serialization or object exchange. Other ways include XML, DataInputStream/DataOutputStream serialization, and your own custom format. None are "standard" in the way that object serialization is in Java SE (which isn't available on the BB platform). XML, of course, has a formal standard behind it, as well as parsers and writers built into the BB execution environment. JSON is also standardized and is a much simpler format than XML (in one sense they are competitors). JSON was originally designed for JavaScript (not Java), where JSON is valid JavaScript can just be evaluated to automatically produce a JavaScript value. In Java, JSON requires a parser and/or a writer just like XML. Unlike XML, these do not come built into the BB. Thus the need for something like json-lib. JSON is simple enough that it's easy to write your own parser and writer if you don't like any of the libraries.

 

Of course, nothing is ever good enough, so many libraries define JSON "extensions"--such as being able to specify the class name of the desired Java object(s) in a JSON file and have the parser automatically produce objects of that class. Since this concept is more or less foreign to JavaScript (a prototype-based, rather than object-based language), these extensions are not standard nor portable between JSON libraries. I recommend avoiding the use of these extensions for anything but the most self-contained project.

 

Is everbody using JSON? It's definitely popular, but perhaps not that much. It is much more compact than XML, and does just as good a job as XML for many applications, but it lacks built-in support in most execution environments. So you trade off communication and storage overhead with bulkier software. There's no one answer for everyone.

 

Hope this helps.




Solved? click "Accept as solution". Helpful? give kudos by clicking on the star.