Ok. Meanwhile, I can use sojo to serialize to json. Lot of thanks,
Marcial
On 20/03/12 15:17, Jörg Schaible wrote:
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
--
Marcial Atiénzar Navarro
Analista de Sistemas de Información
Avda. Del Reial Monestir Ntra. Sra. Poblet, 20
46930-Quart de Poblet (Valencia)
Tel.: 961 849 249 (corto 1249)
Móvil: 629 201 240 (corto 44249)
Fax: 961 525 321
[email protected] <mailto:[email protected]>
www.umivale.es
www.sumaintermutual.es
________________________________
Antes de imprimir cualquier documento, asegúrese de que es necesario.
El medio ambiente está en nuestra mano.
El presente mensaje y los documentos que,en su caso, lleve anexos, pueden
contener información confidencial. Se informa a quién lo reciba por error, que
su uso no autorizado está prohibido legalmente. Comunique dicha situación por
la misma vía o telefónicamente al 902 365 012, no lo remita a terceros y
proceda a su eliminación inmediata.
<<inline: logotipo.png>>
<<inline: efqm.jpg>>
