Deserialize JSON String

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Deserialize JSON String

Xybrek
I'm trying to deserialize a JSON string with XStream like this:
private <T> T createTObjectWithXStream(Class<T> clazz){
XStream xstream = new XStreamGae(new JettisonMappedXmlDriver());
T obj = null;
try {
String json = this.toJSONString();
obj = (T) xstream.fromXML(json);
} catch (Exception e) {
LOG.log(Level.SEVERE, "XStream cannot deserialize this object: " + e.getMessage());
}
return obj;
}

I need to deserialize the whole String as a type of clazz. However from the examples I need to do like:

xstream.alias("product", Product.class);

However, this seems for deserializing specific node, what should be done for working on the whole json string? Like

xstream.alias("the_whole_json_stuff", clazz);

Also the result of e.getMessage is 'contentType' what's that suppose to mean?
Reply | Threaded
Open this post in threaded view
|

Re: Deserialize JSON String

Jörg Schaible-2
Hi,

xybrek wrote:

> I'm trying to deserialize a JSON string with XStream like this:
> private <T> T createTObjectWithXStream(Class<T> clazz){
> XStream xstream = new XStreamGae(new JettisonMappedXmlDriver());
> T obj = null;
> try {
> String json = this.toJSONString();
> obj = (T) xstream.fromXML(json);
> } catch (Exception e) {
> LOG.log(Level.SEVERE, "XStream cannot deserialize this object: " +
> e.getMessage());
> }
> return obj;
> }
>
> I need to deserialize the whole String as a type of clazz. However from
> the examples I need to do like:
>
> xstream.alias("product", Product.class);
>
> However, this seems for deserializing specific node, what should be done
> for working on the whole json string? Like
>
> xstream.alias("the_whole_json_stuff", clazz);
>
> Also the result of e.getMessage is 'contentType' what's that suppose to
> mean?


So, what is 'this'? What is 'json'? What do you mean with
"the_whole_json_stuff"?

Sorry, but I don't get a clue what you try to ask.

- Jörg


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

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: Deserialize JSON String

Xybrek
this.toJSONString returns a JSON string, a well-formatted json string. 

"the_whole_json_stuff" was just my way of  asking to actually how to get the deserialize the whole json string without actually specifying a node in the string like:

xstream.alias("product", Product.class);

So how do you deserialize a JSON string with XStream?


On Wed, May 15, 2013 at 6:41 AM, Jörg Schaible <[hidden email]> wrote:
Hi,

xybrek wrote:

> I'm trying to deserialize a JSON string with XStream like this:
> private <T> T createTObjectWithXStream(Class<T> clazz){
> XStream xstream = new XStreamGae(new JettisonMappedXmlDriver());
> T obj = null;
> try {
> String json = this.toJSONString();
> obj = (T) xstream.fromXML(json);
> } catch (Exception e) {
> LOG.log(Level.SEVERE, "XStream cannot deserialize this object: " +
> e.getMessage());
> }
> return obj;
> }
>
> I need to deserialize the whole String as a type of clazz. However from
> the examples I need to do like:
>
> xstream.alias("product", Product.class);
>
> However, this seems for deserializing specific node, what should be done
> for working on the whole json string? Like
>
> xstream.alias("the_whole_json_stuff", clazz);
>
> Also the result of e.getMessage is 'contentType' what's that suppose to
> mean?


So, what is 'this'? What is 'json'? What do you mean with
"the_whole_json_stuff"?

Sorry, but I don't get a clue what you try to ask.

- Jörg


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

    http://xircles.codehaus.org/manage_email



Reply | Threaded
Open this post in threaded view
|

Re: Re: Deserialize JSON String

Jörg Schaible-2
Hi,

xybrek wrote:

> this.toJSONString returns a JSON string, a well-formatted json string.

that's what I already guessed, but I don't understand what the method
actually does and what it produces. So, how do you create this JSON (with
XStream) and how does it look like?

> "the_whole_json_stuff" was just my way of  asking to actually how to get
> the deserialize the whole json string without actually specifying a node
> in the string like:
>
> xstream.alias("product", Product.class);

This is the part, where I don't understand your question.

> So how do you deserialize a JSON string with XStream?

If you question actually is, how to deserialize from an arbitrary JSOn
string to Java object, that the answer is simple: You don't!

Even with XML, XStream will deserialize what it writes. If the written
format does not match your requirements, you have to setup the XStream
instance until it matches.

The same applies for JSON with the additional limitations.

- Jörg


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

    http://xircles.codehaus.org/manage_email