File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes XML and Related Technologies and the fly likes XStream dynamically aliase on Set not working Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "XStream dynamically aliase on Set not working" Watch "XStream dynamically aliase on Set not working" New topic

XStream dynamically aliase on Set not working

George Yee

Joined: Jul 17, 2013
Posts: 1
I am trying to dynamically alias object during XML serialization using XStream, here is the utility code:

Weh I pass a Set of int

What I got is

instead of(expected)

If I change this line
xStream.alias(alias, obj.getClass());
xStream.alias(alias, Set.class);

Then it works as expected.

How can dynamically assign alias by object class type?

g tsuji
Ranch Hand

Joined: Jan 18, 2011
Posts: 630
Due to XStream's use of its registry of Converters, you've sometime need to know exactly how it behaves, namely, what class to put as the 2nd arugment in the alias method. This, I think, is arguably troublesome...

In order to make serialization always responsive to an object's getClass()'s response as the proper 2nd argument to the alias method, the idea-conductor is to subclass the ClassAliasingMapper. It can be condensed into the set up of the instantiation of the XStream (as shown) and this is how. (I call the subclass SelfDoc... for the responsiveness to .getClass() always, hence, do not need a priori accepting the behaviour of default converters... you may sure call it anything you like.)

You might have to change this to that according to the exact outcome your like better (such as getSimpleName() here, to getName() or getCanonicalName() etc etc..., choose what you feel more comfortable.)
I agree. Here's the link:
subject: XStream dynamically aliase on Set not working
It's not a secret anymore!