JSON is a text-based data representation format, and as in all text-based formats, binary data need to be encoded as text. One approach is to convert the binary data to base 64 encoding, and another approach is to convert the binary data to other text-encoded formats, such as hex values, for example
[\uDEAD, \uBEEF,...].A better solution is to decouple the binary data via a Uniform Resource Locator (URL), where the binary data is represented or pointed to by the URL. In this approach the binary data wouldn't be embedded into the JSON text but would exist on a server on the web. This approach would allow for a separate mechanism for retrieval that can support caching and asynchronous retrieval and local storage.
Choosing the right data interchange format is an important design decision when building any network-aware software. This is especially true when designing mobile and embedded applications, where attributes such as lightweight and efficientare important characteristics to consider. Such characteristics are important because they translate to lower computation requirements and use of power, potentially better performance, and lower costs of operation.
In mobile applications, developers typically rely on home-grown data-interchange formats or on the Extensible Markup Language (XML). The advantage of the former is that it can be tailored to particular situations for the purpose of maximizing performance and/or computational resources. The advantage of the latter, when used over HTTP, is that it is a de facto standard for data interchange. In addition, the text-based/human-readable representation used in XML makes it easier to debug.
Yet these two approaches also have disadvantages, one being proprietary in nature, non-standard, and potentially non-interoperable, while the other one could be considered too heavy and verbose for data representation, again this is especially true for mobile and embedded applications.
|
An alternative to consider is the JavaScript Object Notation or JSON, a lightweight data-interchange format. In this article I introduce JSON for data interchange in the Java Platform, Micro Edition (Java ME).
JSON is defined as part of JavaScript (ECMAScript) scripting language. Being native to JavaScript, JSON is ideal for browser-based applications. But JSON is not limited to JavaScript, and its lightweight characteristics make it very attractive for mobile and embedded applications in general.
JSON uses a very straightforward syntax that is based on two main data structures:
- Collection of name/value pairs, and
- Order list of values.
JSON is text-based, making it human-readable and great for debugging. JSON supports notation for all the basic data-types, and provides the methods to parse back and forth to Java native types.
Less Verbose, yet easy to debug (readable)
Readability of data over the wire is very much important to debug with ease. XML is very good at this, however XML requires two tags per data point, plus extra tags for the root nodes, this adds to size of the document. On the other hand JSON requires only one tag i.e. the name of the elements as such its less verbose. And the good part is that all this comes with sacrificing the readability.
More Speed & Less Bandwidth Utilization
Since the packet size is smaller in JSON compared to XML to convey the same data, the the data transmission between the Client and Server is faster using in turns helps to reduce network traffic as less number of bytes flow over the wire making the Server to get done quickly and the Client be more responsive.
Less Memory Footprint, faster generation & processing
The data structure for JSON is smaller resulting in less memory utilization on both Client and Server esp. at the Client Size, helping the browser to accept larger size JSON documents and parse them quickly with using much of Clients CPU and Memory making the Browser interface more responsive.
Supported in Browsers via Javascript
The other important benefit is you can parse JSON text with JavaScript's eval() function. No special libraries are needed and this works across all the browsers so one don't have to worry about cross-browser issues. As long as the browser has JavaScript enabled and support this eval() function, you will be able to parse JSON data.
