Problem with XStream Marshalling to return xml and json

Posted by MiKu on Stack Overflow See other posts from Stack Overflow or by MiKu
Published on 2010-04-20T14:19:34Z Indexed on 2010/04/20 14:23 UTC
Read the original article Hit count: 475

Filed under:
|
|
|
|

When i use

new XStream().toXml(someObject);

it returns following xml...

<response>
        <status>SUCCESS</status>
        <isOwnershipVerified class="boolean">false</isOwnershipVerified>
</response>

and, when i use

new XStream(new JsonHierarchicalStreamDriver()).toXml(someObject);

it returns following json...

{"response": {
  "status": "SUCCESS",
  "isOwnershipVerified": {
    "@class": "boolean""false"}
}}

Now, since i want to get rid of class attribute altogether (read it not to alias it with anything else, but to remove it) i use following code.

    XStream xStream = new XStream();
    StringWriter writer = new StringWriter();
    xStream.marshal(this, new PrettyPrintWriter(writer) {
        @Override
        public void addAttribute(final String key, final String value)
        {
            if (!key.equals("class"))
            {
                super.addAttribute(key, value);
            }
        }
    });
    return writer.toString();

which gives follwing xml...

<response>
        <status>SUCCESS</status>
        <isOwnershipVerified>false</isOwnershipVerified>
</response>

but, when i pass new JsonHierarchicalStreamDriver() while xStream object creation above, it does NOT return json. it returns the same xml shown above.

What is wrong going on here?

Thanks in advance...

© Stack Overflow or respective owner

Related posts about xstream

Related posts about JSON