Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XStream short dynamic aliases

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?

like image 408
IAdapter Avatar asked Dec 10 '22 19:12

IAdapter


1 Answers

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);
    }
}
like image 190
Legna Avatar answered Dec 21 '22 22:12

Legna