XStream 更改通用别名约定
现在XStream使用class.getName()作为别名,但我想使用class.getSimpleName()。是否可以轻松配置它,或者我是否必须手动配置所有类才能使用 simpleName?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是,您的映射应该是双向的,因此您可以根据简单的名称找到该类。既然这不是小事,你应该自己做点什么。我基本上可以看到两种方法可以做到这一点。
1. 注册别名
由于您已经知道您将使用哪些类(因为如果您不知道,您将无法找到带有简单名称的类),因此您可以为所有类注册别名类似
2. 创建自定义映射器
如果您出于某种原因不想注册别名,您可以将映射器包装在您自己的映射器中(例如,请参阅 AbstractXmlFriendlyMapper 以此为基础),但是,您仍然需要某种方法来找出哪个类属于哪个简称。
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
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.