Thinking about JSON POJO

Posted by david, Wed Jan 30 18:13:00 UTC 2008

Working on a model to map user-defined Java objects to JSON has been quite a challenge. Some of the challenge I'm sure is the mapping between a section of a JSON document and the corresponding Java object - do I know if a object member of "location" : "http://blog.alkaline-solutions.com/" is meant to be a java URI, a Java URL, or a String? In the absence of typed containers, it becomes even harder to determine how to do this than in XML binding cases. For another example: class Foo { Object location; } For serialization, the type of the "location" field's referenced object can be used to determine how to represent it. However, on input you have the same problem as above - and unless you radically enforce a JSON structure that makes everything except the primitives an object and forces a java class 'type' string into every object to determine what is being specified, having a round trip becomes something impossible to do without some local specification. So, for starters I used annotations, defaulting to using a field/'bean accessor' name and a global registry of class/interface type handlers for members. Types like Class and Date translated to simple string types in the JSON output. I still ran into some problems though - for instance, a List member does not at compile time indicate that its members must be strings - so without further annotations, deserialization faced the 'no type' problem above. This got me looking into a framework to base things on, which in turn got me looking at JSON Schema. I'll post more thoughts about JSON schema next.

Filed Under: | Tags:

Comments