jamesharr edited this page May 9, 2011 · 14 revisions

JSON-DTO

JSON-DTO is a GWT library for pickling objects into JSON data as well as providing an RPC mechanism very similar to GWT-RPC. It can be used to model existing JSON structures as Java objects to ease interfacing with current services. It can also be used to build new services and model the [JSON] API after Java objects in a way that’s cross-compatible with multiple client and/or browser implementations.

What JSON-DTO will do

  • Help you avoid tedious JSON parsing via GWT’s Java API.
  • Avoid hassles of GWT’s JavaScriptObjects:
    • Catch mis-formed JSON while parsing.
    • Easily see data structures from a debugger.
    • Support for more sophisticated data types and structures.
  • JSON-RPC services. This will look and feel very similar to GWT-RPC.
  • Self-referential classes. You can build linked lists or, but don’t build anything with a cycle. This will confused the pickler and send it on a recursion death-spiral. Support for this will probably never happen since the JSON mapping is not one-to-one.

What JSON-DTO won’t do

  • Polymorphism. If you say you’re pickling a Foo, you’re pickling whatever you gave us as a Foo even if it’s a FooSubClass.
  • Object graphs with cycles (see last bullet on what JSON-DTO will do).
  • Pickle private fields. Sorry, this is a GWT+Java limitation. Use default/protected/public field access.
  • There’s currently no plans to support the pickler using getter/setter methods. You can have them, but the pickler is going straight for your data.

Supported data types

  • Primitive types (and their boxed counterparts)
  • Array – Multi-dimensional
  • Parameterized Generics (keep reading).
  • Lists. Currently uses LinkedList for the implementation. ArrayList support (and possibly others) is planned.
  • Maps. Keys must be Strings currently, values can be any picklable type. Currently uses TreeSet for implementation.
  • Sets. Currently uses TreeSet for the implementation.

Example code snippets.

Bug me about this and I’ll spit out a few easily digestible samples. Until then, check out the unit tests:

Other slick stuff

I threw in some field annotations to make dealing with corner-cases and API version differences a little easier as well.

  • @Optional – Leave a field as-is if the JSON field isn’t there. Use this with a Map field in an object.
  • @IgnoreNull – If the data in an object is present, but is null, don’t set the field to null.

When running in GWT’s dev mode, I think I’m going to include support for proxying JSON-RPC calls through Jetty to avoid problems with cross-domain requests.

Release

When will this be ready? Not entirely sure. Most of the pickling code is written, functional, and passes the tests (the tests that I’ve thought of anyway). I prototyped the project December 2010 and picked it back up April 2011. Since then I’ve made a lot of progress. If personal interest and a little bit of luck is on my side, a beta will be out mid-summer 2011.

Clone this wiki locally
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.