Thank you for your help Jörg.  I have written some converters in the past with 
no problem, but I wasn't picking up on the fact that it was a Map that I'd need 
to do in this case.  Makes sense now that I think about it.  I appreciate your 
guidance.  

Lynn

-----Original Message-----
From: Jörg Schaible [mailto:[email protected]] 
Sent: Wednesday, May 02, 2012 5:04 AM
To: [email protected]
Subject: [xstream-user] Re: deserializing json with numbered object names

Hi Lynn,

Walton, Lynn wrote:

> Hello all,
> 
> I'm hoping to use XStream with the JettisonMappedXmlDriver to deserialize
> some 3rd party json output.
> 
> The json output includes numbers  as object names so in my example below
> propertyC actually has 3 nested objects but uses a number "1", "2", or "3"
> as the object name.  In my real world the json usually contains between
> 100 and 200 of these (it varies).
> 
> For instance:
> 
> { "root" : { "propertyA " : "valueA",
>       "propertyB" : "valueB",
>       "propertyC" : { "1" : { "propertyC1" : "propertyC1_1_Value",
>               "propertyC2" : "propertyC2_1_Value",
>               "propertyC3" : "propertyC3_1_Value"
>             },
>           "2" : { "propertyC1" : "propertyC1_2_Value",
>               "propertyC2" : "propertyC2_2_Value",
>               "propertyC3" : "propertyC3_2_Value"
>             },
>           "3" : { "propertyC1" : "propertyC1_3_Value",
>               "propertyC2" : "propertyC2_3_Value",
>               "propertyC3" : "propertyC3_3_Value"
>             }
>         },
>       "propertyD" : "valueD"
>     } }
> 
> Can someone point me in the direction of what I'd have to do to make this
> work so that I could have the json deserialized into Java objects.  I
> thought I'd have the  Root object with it's simple properties/fields for
> propertyA, propertyB, and propertyD and a collection of propertyC but then
> I don't know how to configure a converter and/or aliases to handle the
> "1", "2","3" , etc all as the same object.  I'd like to build 3 objects of
> the same type and where it has an extra property that really represents
> the object name (the 1, 2 or 3, etc).
> 
> Are there some advance aliasing, converter  and/or other tricks I could
> use to make this reasonable with xstream and jettison or should I look
> into other json converting utilities?  I don't want to "preprocess" the
> json string to change its format.

first let me state that it is already an annoying process to write 
converters to match an incoming XML, it is even more so for JSON, especially 
since this conversion is even more limited due to some heuristics the 
Jettison parser has to make. Therefore as first recommendation: Try to write 
this JSON format using XStream, then you have a much better chance that it 
can be read. And it is much easier to tweak the output if it does not match 
the required format instead of not knowing why half of the information got 
lost at deserialization ;-)

Additionally you have to understand how XStream/JSON works in this case. 
Jettison is a backend for the StAX API i.e. XStream does not really know 
that it produces JSON, this is completely hidden by the Jettison 
writer/parser which uses some heuristics to detect XML structures that are 
better represented as JSON arrays.

If all what you want is more or less a nested HashMap<String,Object> then 
you can easily write a simple converter for it (see the converter tutorial, 
it's really easy) that handles a HashMap. At marshalling time iterate over 
all entries of the map, write the key as element name. If the value is a 
String, write it as text. If it is a HashMap, call the context to convert it 
(see tutorial). During unmarshalling after reading the element with the key 
name you will have to ask the reader if there are children. If yes, ask the 
context to unmarshal it, otherwise read its text.

Register this converter for XStream and set an alias for HashMap to be 
"root". Maybe you are inclined to use a LinkedHashMap instead, but then you 
will have to additionally define that the LinkedHashMap is the default 
implementation for Map. However, this supports a false assumption about the 
sequence of the properties, because JSON specification explicitly defines no 
guarantee about the sequence of the properties and a parser may return them 
in arbitrary order.

Hope this helps,
Jörg


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email




This message and any attachments are intended only for the use of the addressee 
and may contain information that is privileged and confidential. If the reader 
of the message is not the intended recipient or an authorized representative of 
the intended recipient, you are hereby notified that any dissemination of this 
communication is strictly prohibited. If you have received this communication 
in error, please notify us immediately by e-mail and delete the message and any 
attachments from your system.

Reply via email to