PDA

View Full Version : GXT + XStream



bboyle18
10 Aug 2010, 12:39 PM
Hi,

I'm trying to convert a simple GXT BaseModel object to XML using XStream.
I am having problems though as my XML is not coming out as I expect. I think this is because BaseModel uses an RcpMap to store the data, rather than assigning the values to member variables as you would expect for a regular POJO.
I have seen a few comments on the forums already of people saying that they are using XStream with GXT, but no examples of how its done.
I'd be grateful if anyone who has used it can let me know and perhaps point me in the right direction for this.

Cheers,

Brian
P.S. below is my code



*/
public class CarModel extends BaseModel {

private String name;
private String colour;
private int numberPlate;
private int age;

public CarModel(String name, String colour, int numberPlate, int age) {
set("name", name);
set("colour", colour);
set("numberPlate", numberPlate);
set("age", age);
}

/**
* @return the name
*/
public String getName() {
return get("name");
}
/**
* @param name the name to set
*/
public void setName(String name) {
set("name", name);
}
/**
* @return the colour
*/
public String getColour() {
return get("colour");
}
/**
* @param colour the colour to set
*/
public void setColour(String colour) {
set("colour", colour);
}
/**
* @return the numberPlate
*/
public int getNumberPlate() {
return get("numberPlate");
}
/**
* @param numberPlate the numberPlate to set
*/
public void setNumberPlate(int numberPlate) {
set("numberPlate", numberPlate);
}
/**
* @return the age
*/
public int getAge() {
return get("age");
}
/**
* @param age the age to set
*/
public void setAge(int age) {
set("age", age);
}



This is my converter class



public class GXTToXMLConverter {


public String convertToXML(CarModel carModel) {
XStream xStream = new XStream();
xStream.alias("car", CarModel.class);
String xml = xStream.toXML(carModel);
System.out.println(xml);
return xml;
}
}


And finally, here is my test


public void testConvertToXML() throws Exception{
CarModel car = new CarModel("Audi", "Silver", 9023423, 9);
String xml = converter.convertToXML(car);
assertNotNull(xml);
}


Result is this



<car>
<map/>
<allowNestedValues>true</allowNestedValues>
<numberPlate>0</numberPlate>
<age>0</age>
</car>

The_Jackal
10 Aug 2010, 5:56 PM
RpcMap stores it's values in a transient FastMap. As it is transient it will not be serialised.

When you send a RpcMap over the GWT RPC then a special serialiser is used to do this - RpcMap_CustomFieldSerializer.

So how do you serialise the values then?

a) I would use a POJO and create BeanModels from that with either a BeanModelMarker or BeanModelTag using the BeanModelFactory

BeanModelLookup.get().getFactory(yourPOJOClass.getClass()).createModel(yourPOJOClass)

Then you can use XStream with the POJOs.

b) You could call XStream with a new HashMap(carModel.getProperties()) to get non-transient map.

You would also have to call carModel.setProperties(yourDeserialisedMap);


public class GXTToXMLConverter {

public String convertToXML(CarModel carModel) {
XStream xStream = new XStream();
xStream.alias("car", CarModel.class);
String xml = xStream.toXML(new HashMap(carModel.getProperties()));
System.out.println(xml);
return xml;
}
}

This writes

<map>
<entry>
<string>a</string>
<string>42</string>
</entry>
</map>

so you may want to create a CarWrapper that just has the map as it's only attribute.

If you are going to do this a lot then consider creating an XStream converter for BaseModels http://xstream.codehaus.org/converter-tutorial.html

c) You could write a custom XStream converter for any BaseModelData that does the same sort of thing as in b) (gets the properties and puts them in a new HashMap).

Writing custom converters takes some getting used to but is relatively straight forward.

Hope that helps,
Carl Pritchett

See also:

http://www.sencha.com/forum/showthread.php?67660-RpcMap-not-serializable-problem&p=499763#post499763