I want to have short names for classes, now i can do it with aliases
XStream x = new XStream();
x.alias("dic", Dic.class);
but i have to define alias manually for every class, is there any way to configure xstream to do it automatically?
They way I solved is:
1.- When I create xstream I override its wrapmapper method
XStream xstream = new XStream() {
@Override
protected MapperWrapper wrapMapper(MapperWrapper next) {
return new MyClassAliasingMapper(next);
}
};
2.- with
public class MyClassAliasingMapper extends ClassAliasingMapper {
public MyClassAliasingMapper(Mapper wrapped) {
super(wrapped);
}
@Override
public Class realClass(String elementName) {
try {
return <… your own class …>
} catch (Exception e) {
// do nothing we fall back on super's implementation
}
return super.realClass(elementName);
}
@Override
public String serializedClass(Class type) {
try {
return <… your own element name …>
} catch (Exception e) {
// do nothing we fall back on super's implementation
}
return super.serializedClass(type);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With