Hi Marcial,

Atienzar Navarro, Marcial wrote:

> Hi Jörg,
> 
>     I've test it and it's ok with arrays, but with java types doesn't work
>     (String, Long, Boolean,...). Example:
> 
> ResultBean<String> prueba = new ResultBean<>() ;
> prueba.setBean("Hola mundo");
> System.out.println(xsJson.toXML(prueba));
> 
> {"@id": "1","cod": -1,"msg": "Error","success": false,"bean": {"@class":
> "string","$": "Hola mundo"}}
>
> 
>     But with an object that don't use generics it works great:
> 
> 
> ResultTest prueba2 = new ResultTest();
> prueba2.setBean("hola mundo");
> System.out.println(xsJson.toXML(prueba2));
> 
> 
> {"@id": "1","bean": "hola mundo"}
> 
> 
>     Can I do something to solve it?


I simply assume now, your class is the one of XSTR-696.

Again, this has nothing to do with generics itself. At runtime you store 
your elements in a member of type Object and therefore will XStream generate 
the class property, since the type of the serialized object and the type of 
the member differs. Declare the member directly as Object (without using 
generics) and you have the same effect.

>     I'm using this code to initialize xstream:
> 
> XStream xsJson = new XStream(
> new JsonHierarchicalStreamDriver() {
> 
> @Override
> public HierarchicalStreamWriter createWriter(Writer writer) {
> return new JsonWriter(writer, JsonWriter.DROP_ROOT_MODE, new
> JsonWriter.Format( new char[0],
> new char[0],
> JsonWriter.Format.SPACE_AFTER_LABEL |
> JsonWriter.Format.COMPACT_EMPTY_ELEMENT ));
> }
> });
> 
> xsJson.setMode(XStream.ID_REFERENCES);
> xsJson.autodetectAnnotations(true);

Since you will not use XStream to recreate your objects, you can turn off 
the generation of the system attribute "class" by configuring additionally:

xstream.aliasSystemAttribute(null, "class");

You will have to know now on your own, what types you really have saved. 
This might be OK in your case.

- Jörg


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

    http://xircles.codehaus.org/manage_email


Reply via email to