Hi Marcial,

Atienzar Navarro, Marcial wrote:

> Hi Jörg ,
> 
> I'm using this code:
> 
> 
> 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);
> 
> 
> 
> I'm using ID references, because on client I've a JsSerializer to convert
> it to the real reference.
> 
> I'm using REST, and to convert from JSON to Java I'm using Jackson. And to
> convert from Java to Json Xstream.
> 
> This proces is working well, inspect of this case, when I'm using generics
> inside a pojo.
> 
> This is the test code:
> 
>         ResultBean<List<GnNode>> nodos = new ResultBean<>();
> 
>       ResultList<List<GnNode>> nodose = new ResultList<List<GnNode>> ();
> 
>       GnNode nodo1 = new GnNode();
>       nodo1.setNodeId(1);
>       nodo1.setNodeTitle("nodo 1");
>         
>       GnNode nodo2 = new GnNode();
>       nodo2.setNodeId(2);
>       nodo2.setNodeTitle("nodo 2");
>         
>       List<GnNode> nodosl = new ArrayList<>();
>       nodosl.add(nodo1);
>       nodosl.add(nodo2);
>         
>       nodos.setBean(nodosl);
> 
>       // This code fails to return a well formed json
>       System.out.println("Nodos :" + xsJson.toXML(nodos));
>       
>       // This code works ok, because the specification of ResultList :
>       public class  ResultList <E> implements Serializable{ private
>       List<E> lista = new  ArrayList<E>(); System.out.println("Nodos
>       2:"+xsJson.toXML(nodosl));

Actually it has nothing to do with generics in first place. Generics are no 
longer present at runtime. The problem is, that the ReflectionConverter 
(which handles the ResultBean) does provide the type of the field to the 
writer, but not the type of the real instance. Hence it gets "Object" which 
is not a collection type and it does not create a JSON array. You can see 
the same problem here (and that not only lists are affected):

=============== %< ================
class Demo {
  Integer theInt;
  List theList;
  Object anInt;
  Object aList;
};

Demo demo = new Demo();
demo.anInt = demo.theInt = 5;
demo.aList = demo.theList = new List();
demo.theList.add("a");
demo.theList.add("b");

xsJson.setMode(NO_REFERENCES); // just for the demo now

System.out(xsJson.toXML(demo));
=============== %< ================

The first two elements should be formatted properly (as number and as list) 
while the same objects are handled wrong for the the other two elements.

I'll have to do some tests first and look for possible side-effects before I 
can change it.

Cheers,
Jörg


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

    http://xircles.codehaus.org/manage_email


Reply via email to