views:

111

answers:

2

I'm writing a RIM BlackBerry client app. BlackBerry uses a simplified version of Java (no generics, no annotations, limited collections support, etc.; roughly a Java 1.3 dialect). My client will be speaking JSON to a server. We have a bunch of JAXB-generated POJOs, but they're heavily annotated, and they use various classes that aren't available on this platform (ArrayList, BigDecimal, XMLGregorianCalendar). We also have the XSD used by the JAXB-XJC compiler to generate those source files.

Being the lazy programmer that I am, I'd really rather not manually translate the existing source files to Java 1.3-compatible JSON-marshalling classes. I already tried JAXB 1.0.6 xjc. Unfortunately, it doesn't understand the XSD file well enough to emit proper classes.

Do you know of a tool that will take JAXB 2.0 XSD files and emit Java 1.3 classes? And do you know of a JSON marshalling library that works with old Java?

I think I am doomed because JSON arrived around 2006, and Java 5 was released in late 2004, meaning that people probably wouldn't be writing JSON-parsing code for old versions of Java.

However, it seems that there must be good JSON libraries for J2ME, which is why I'm holding out hope.

+1  A: 

For the first part good luck but I really don't think you're going to find a better solution than to modify the code yourself. However, there is a good J2ME JSON library you can find a link to the mirror here.

Jonathan
A: 

I ended up using apt (annotation processing tool) to run over the 1.5 sources and emit new 1.3-friendly source. Actually turned out to be a pretty nice solution!

I still haven't figured out an elegant way to do the actual JSON marshalling, but the apt tool can probably help write the rote code that interfaces with a JSON library like the one Jonathan pointed out.

sowbug
I extended my APT code to emit a custom ObjectMapper class mirroring the nice Jackson API (http://jackson.codehaus.org/). For each of my POJOs, the processor spits out two ObjectMapper methods: ObjectName_toJSON() and ObjectName_fromJSON(). Then after scanning all the POJOs, the processor maps the readValue/writeValue calls to appropriate methods. Given that there's no introspection available in this particular version of Java, I'm not sure there's a solution possible that doesn't involve source code that explicitly calls each POJO getter/setter.
sowbug