Jsonification -- JSON for Java that Doesn’t Suck
Ever tried using one of the existing Java JSON libraries? I have. They blow.
Except for mine, Jsonification.
Jsonification is designed to be quicker and easier to use than other libraries. Jackson is a great JSON library, but it’s very enterprise-y. An enterprise JSON library is great if you’re working on a large project that absolutely, positively, has to scale to a million users tomorrow, but if you’re just throwing together a demo, it’s definitely overkill. GSON is another good library, but it makes some demands on the architecture of your software (custom serializers, deserializers, and instance creators, anyone?) and requires a lot of boilerplate code, which can make it difficult to retrofit into existing projects and slow to bootstrap in new software. And sorry, json.org guys, but your default JSON implementation for Java is just bad. Jsonification is designed to fit comfortably into the underserved I-want-JSON-support-and-I-want-it-now niche in the Java world.
What Makes Jsonification So Good?
Why is Jsonification Easy to Use?
JsonInt, and so on) as opposed to native Java classes to store its model, which lets Jsonficiation handle JSON tasks like parsing with a lot of flexibility. This functional approach to JSON in Java — separating a data structure’s JSON model representation from its POJO representation — is a big part of why Jsonification integrates into apps more quickly and easily than other libraries.
As an introduction to Jsonification’s JSON model, the following snippet of Java iterates over a JSON array called
objects and prints the
name attribute of each object contained in the array:
for(JsonValue object : objects) System.out.println(object.asObject().get("name").asString().getValue());
Notice how the
JsonArray object integrates right into the native Java looping construct. And it’s all type-safe and uses checked exceptions to keep things nice and Java-y.
Jsonification is a full-featured JSON implementation that supports integers, floating point number, strings, JSON objects, arrays, and also handles
undefined values in a way that’s actually useful. And in keeping with the JSON-in-10-minutes-or-less principle, parsing a JSON string into these JSON model objects is as easy as calling
Notice that you don’t have to say what you’re parsing ahead of time; you just call parse and Jsonification takes care of the rest using the magic of JSON model objects. Jsonification also makes it as easy to go from POJOs to JSON model objects as it is to parse a JSON string. For example, if you have a
Integers, you can use
Json.toJson to create a JSON representation of that data structure with a single line:
Map<String,Integer> numbers=new HashMap<String,Integer>(); numbers.put("one", 1); numbers.put("two", 2); // ... JsonValue json=Json.toJson(numbers);
Jsonification knows how to serialize
Set objects out of the box. If you want to make another kind of POJO serializable to JSON, then you just need to add a
toJson method to that object and Jsonification will pick up the serialization support automatically. No need to worry about custom serializers, deserializers, and object factories; just implement a method and you’re done.
And, of course, converting JSON model objects to JSON strings is easy, too. Just use
So, converting just about any data structure from its POJO representation to a JSON string is a one-liner:
And jsonification has both minified and pretty-print JSON emit modes, too, so you’ll actually be able to read your application’s JSON output during development. Imagine.
Where Can I Get Jsonification?
And, perhaps best of all, Jsonification is open-sourced under the Apache License 2.0, which basically lets you use it anywhere you want in any way you want. Just don’t tell people you’re the one who wrote it, and you’re good to go.
You’ll find links to download Jsonification below. This is still an early version of the library, so forgive me for the bugs you’re bound to find. If you let me know about them (politely, please), I’ll fix them as soon as I can. Enjoy!
|version 1.0.2 Beta 1 (source)||April 26, 2010||Added missing
|version 1.0.1 Beta 1 (source)||April 26, 2010||Fixed some pretty printing bugs; added support for
|version 1.0 Beta 1 (source)||April 22, 2010||Initial Release.|