例如,我想知道如何确保XStream将Map / HashMap恶魔转换为JSON
@XStreamConverter(value = CustomMapJSONConverter.class)
class MyPOJO {
private String name;
private Long amount;
private Map<String,Comparable> properties;
}
因此,它应该输出如下内容:
{
"name" : "John Smith",
"amount" : 123456,
"properties" : {
"someProperty" : "some value"
}
}
在这种情况下可以用作@XStreamConverter
的XStream转换器是什么?
这是我的转换器(但这是完全错误的)
public class CustomMapJSONConverter implements Converter {
@Override
public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context) {
writer.startNode("properties");;
String json = new Gson().toJson(source);
writer.setValue(json);
writer.endNode();
}
@Override
public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
// TODO
}
@Override
public boolean canConvert(Class type) {
if(Map.class.isAssignableFrom(type)) {
return true;
}
return false;
}
}