users@jaxb.java.net

Custom mapping for Maps in JAXB

From: Leo Romanoff <romixlev_at_yahoo.com>
Date: Fri, 4 Jun 2010 05:21:06 -0700 (PDT)

Hi,

I have a Map inside my JAXB annotated class.
This map represents a set of key/value pairs.

The usual JABX serialization of such a map results in something like:
 <myMap>
 <entry>
   <key>key1</key>
   <value>value1</value>
   ...
 </entry>
 ...
 </myMap>

But I'm very interested in representing key/value pairs in a more natural
form, namely like this:
 <myMap>
   <key1>value1</key1>
   ...
 </myMap>

Basically, I'd like each key of my map to become element's tag name and
value should become element's content. Is it possible at all by means of
JAXB?

While this notation may seem not so interesting from XML point of view, it
makes a lot of sense if you use the same annotated classes to produce JSON
using e.g. Jersey. The above mentioned representation is much more natural
for JSON than the usual one produced by JAXB.

And having such a representation is very important e.g. for being able to
formulate proper
queries towards JSON-based document oriented databases like MongoDB .
At the moment, due to JAXB's { key:"mykey", value:"myvalue"}
representation, having queries
of the form (pattern1 AND pattern2 ) is very difficult, if not impossible,
because MongoDB has a special JSON query language syntax and it requires .
  
Some background:
I'd like to use the same set of (JAXB-)annotated classes to produce both XML
and JSON representations of my data model. And JSON representation should
use natural notation like {mykey:myvalue}, where mykey and myvalue are
dynamic values produced at run-time. This most likely means that its XML
counterpart should use tags with custom-names like mykey, which are known
only at run-time.
 
Of course, if I'd use different approaches for XML and JSON serialization I
could easily solve my problem, but then I need to develop/maintain these two
different approaches. Therefore, I'd like to use a common approach, namely
JAXB, to achieve my goal. But is it possible?

Thanks,
  Leo


-- 
View this message in context: http://old.nabble.com/Custom-mapping-for-Maps-in-JAXB-tp28779221p28779221.html
Sent from the java.net - jaxb users mailing list archive at Nabble.com.