Saturday, September 14, 2013

Json Mapping tool for Dart

In Java, there is a tool called Jackson which handles a mapping between Java;s object and JavaScript's json data. The idea is similar to XStream for Java and XML mapping.
For Web application, json's concise data representation is more preferable than verbose XML representation, in Dart this type of mapping tools are quite in need.

In particular, if we use a rest full API like CouchDB, we often need to serialize Dart's object object into Json data, and vice versa. but the mapping requires either direct mapping (for simple case) or need to implements an interface and implement serialization method like toJson. This allows to serialize a nested Dart objects.

But if we can avoid these recurring task by delegating a tool like Jackson, it is quite productive.

So I looked for such tool in existing Dart libraries (including third-party libs), but there seems no such tools yet available.
One reason for this status is probably due to the lack of reflection in Dart language.
But it will be soon supported, so eventually it would be possible to have this kind of tool.

But Dart seems already supporting annotation, so it might be possible to support it by adding such annotations in Dart's class definition.
Also Dart syntax for getter/setter is quite easy to spot these property definitions, it would be easy to provide a tool to insert these annotations for given Dart class file.

In this type of annotation feature will  be still useful after reflection are introduced since sometime, we may want to customized handling mapping like JPA for instance.

One also interesting prospect for the Dart implementation is it might be possible to use Dart's new Stream feature in innovative ways.

1 comment:

  1. Doesn't JsonObject fit your needs? https://pub.dartlang.org/packages/json_object

    I'm not familiar with Java or Jackson, so please forgive me if I have missed the point.

    ReplyDelete