As far as I know, both JSON and XML can represent objects (!) as
elements with the object name as an XML tag or JSON name, and the
object value as the XML element's content or JSON value.
If JSON expresses maps the same way it does with objects, then
(although it simplifies the representation syntax) it loses the
representation of the data structure and burdens the communication
partners with interpreting a name : value pair the way they ought to
be.
Although plain XML syntax is capable of representing a JSON file, XML
is (I think) intended to express data structure as well - to say
nothing about the loss of possibilities provided by adherence to an
XML schema.
I have been programming a lot in Perl where there is no natural way of
expressing "records" (or objects with a fixed set of fields), and
while the flexibility of a hash is great, there is also the constant
danger of not getting your field name right...
-W
On 4 June 2010 14:21, Leo Romanoff <romixlev_at_yahoo.com> wrote:
>
> 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.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jaxb.dev.java.net
> For additional commands, e-mail: users-help_at_jaxb.dev.java.net
>
>