 |
Language-neutral data format: XML and JSON
Posted by arungupta on March 01, 2007 at 08:42 AM | Comments (2)
XML and JSON are the two prevalent choices for
language-neutral data format. That means a format used to exchange data between
client and server, independent of the language used on each end. We are familiar
with XML pointy bracket syntax which has served us well so far. With Rich
Internet Applications becoming more common, there is a need to have a
light-weight data interchange format. And so JSON is catching
up (11% for data transfer in 2006).
Basically, JSON is built on two structures:
- A collection of name/value pairs with unique names (associative array)
- An ordered list of values (array)
See message samples formatted in
JSON and equivalent XML. Tim Bray summarizes
when to use which format.
Here is a collection of interesting articles in case you want to dig deeper:
The key advantages of JSON I derived from my reading of the above articles
are:
- Much simpler than XML because it is not a markup language and a natural
representation of data.
- JSON is better data exchange format, XML is a better document exchange
format.
- JSON is easier to read for machines with no/thin client-side library.
- JSON is a natural fit for data consumption by browser clients, for example
Ajax components.
- Ability to represent general data structures: records, lists and trees.
- Wikipedia entry for JSON
reports parsing and generating JSON support in 21 languages.
There are some disadvantages as well:
- JSON format is hard to read for humans; for example complicated-looking
syntax, like the
}}]} at the end of data snippet is frightening
and debugging pain.
- JSON is a newer format so not enough tools to help with authoring &
parsing. Some available are:
- JSON Tools - Java Tools for
the JSON Format (parser, renderer, serializer, mapper, validator)
- JSON-lib - Java library
for transforming beans, maps, collections, java arrays and XML to JSON
and back again to beans.
- JSON in Java - Java APIs from
json.org (see more below)
- JSON-taglib -
JSON-taglib is a JSP 2.0 tag library used to render JSON data from
within JSP code.
- Could not find an editor that would allow me to edit JSON objects.
- JSON does not have a <[CDATA[]]> feature, so it is not well
suited to act as a carrier of sounds or images or other large binary
payloads.
- Unlike XML, JSON does not provide any display capabilities because it is
not a document markup language. JSON was not even intended for that purpose.
- JSON is not extensible - it does not need to be because it's not a
document markup language.
In jMaki, we use JSON in Java.
Here is a sample code to create a JSON object using these APIs:
import org.json.*;
import java.io.*;
public class JSONSample {
public static void main(String[] args) throws Exception {
// basic object creation
JSONObject person = new JSONObject();
person.put("name", "duke");
person.put("age", "10");
System.out.println(person.toString());
// how to create array and write to a "writer"
JSONObject address = new JSONObject();
JSONArray array = new JSONArray();
array.put("4140, Network Circle");
array.put("Santa Clara");
array.put("CA - 95054");
address.append("address", array);
OutputStreamWriter osw = new OutputStreamWriter(System.out);
address.write(osw);
osw.flush();
// XML->JSON conversion
JSONObject likes = XML.toJSONObject("<likes><running/><skiing/></likes>");
System.out.println(likes.toString());
}
}
And here is the corresponding output:
{"age":"10","name":"duke"}
{"address":[["4140, network circle","Santa Clara","CA - 95054"]]}
{"likes":{"skiing":{},"running":{}}}
This API also allows conversion from comma-delimited
text, HTTP, Cookie,
and CookieList
to JSON conversions. The source code for JSON
in Java is freely available
but here are two suggestions for ease-of-use:
- Provide a jar file that is ready to use
- Publish the link to framed
version of javadocs on the main page since that is more useful.
In summary, XML is document-oriented and JSON is data-oriented. So if you
want to deal with highly structured documents that requires a complex structure,
binary data, exact ordering of elements and be able to render itself then use
XML. OTOH, if you are focused on light-weight data exchange then JSON is the way
to go.
Follow the JSON blog and enjoy!
Technorati: XML JSON
DataFormat JavaScript
Web2.0 Ajax
jMaki
Bookmark blog post: del.icio.us Digg DZone Furl Reddit
Comments
Comments are listed in date ascending order (oldest first)
-
we ended up using StAX for some of our WS and with Jettison (JSON impl of StAX) we can just throw an alternate factory at our marshalling logic and output JSON or XML with only one if switch-- so clients can access: /services/xml/ItemLookup or /services/json/ItemLookup-- it works great!
Posted by: jhook on March 01, 2007 at 10:12 AM
-
水晶礼品纪念品 婴儿纪念品专家 婴儿纪念品专家 胎毛绣 胎毛坠 胎毛手链 胎毛笔 胎毛笔 水晶礼品公司 水晶工艺品厂 人体克隆艺术 水晶炫彩晶足印 水晶足印 立体宝宝印 古法琉璃 琉璃工艺品 天然水晶 天然水晶 创业致富项目 创业致富项目 水晶礼品公司 周易 在线免费周易算命 起名 取名 在线免费起名测名 胎毛笔 婴儿纪念品 婴幼儿纪念品 水晶手足印 炫彩晶足印 雅克丽晶足印 脐带印章 胎毛章 起名 测名 改名 爱婴理发 婴儿理发 婴儿摄影 连锁加盟 特许经营 水晶奖杯 水晶奖牌 友情链接 互惠链接 友情连接 互惠连接 友情链接 友情连接
Posted by: hexiang99 on March 05, 2007 at 09:08 AM
|