Search code examples
javaxstream

XStream changing general alias convention


Now XStream uses class.getName() for aliases, but I would like to use class.getSimpleName(). Is it possible to easily config this, or do I have to manually configure all classes to use simpleName?


Solution

  • The thing is, that your mapping should work both ways, so you can find the class based on the simple name. Since this is not trivial, you should do something yourself. I can see basically two ways to do this.

    1. Register aliases

    Since you already know what classes you'll be using (since, if you don't, you wouldn't be able to find the class that goes with a simple name), you could register aliases for all classes with something like

    xtream.register("Simple", net.difficult.package.name.Simple.class);
    

    2. Create a custom mapper

    If you don't feel like registering the aliases for some reason, you can wrap the mapper in a mapper of your own (see for instance the AbstractXmlFriendlyMapper for a basis of this), but still, you will need some way to find out which class belongs to which short name.