Comments (3)
Now your class members have the exact type and XStream knows what to do.
from xstream.
First of all XStream is from Java to XML and back. So if you look at the XML that is generated by instance if your class, you will recognize, that XStream adds an attribute class with the real class name of the member instance. This is the key information XStream requires for deserialization.
Your class ABC is defined to keep two members of type java.lang.Object. This is what the Java compiler does here with your generic types. Since the actual types of the instances are not equal at runtime, XStream writes the required attribute. Without it, there's absolutely no information what type of object must be generated at deserialization.
The only thing you can do is to write a custom converter for ABC and handle the XML structure for req and resp for yourself. Maybe you can detect at deserialization from your data yourself which instance actually has to be generated. For XStream this will not be possible automagically.
from xstream.
Finally I use inheritance to solve my problem.I extract req and resp in my subclass.It's works.
@Data
@XStreamAlias("ABC")
class ABC {
private String p1;
private String p2;
}
@Data
@EqualsAndHashCode(callSuper = true)
class MyBusiness extend ABC {
private MyBusinessReq req;
private MyBusinessResp resp;
}
public static <T> T readXmlStr(String xml,T businessClazz){
XStream xStream = new XStream() {
//ignore unnecessary properties in xml
@Override
protected MapperWrapper wrapMapper(MapperWrapper next) {
return new MapperWrapper(next) {
@Override
public boolean shouldSerializeMember(Class definedIn, String fieldName) {
if (definedIn == Object.class) {
return false;
}
return super.shouldSerializeMember(definedIn, fieldName);
}
};
}
};
xStream.autodetectAnnotations(true);
xStream.alias("ABC", businessClazz);
xStream.allowTypes(new Class[]{businessClazz});
T business = (T) xStream.fromXML(xml);
sout(JSON.toJSONString(business));
return business;
}
public static void testReadXmlStr(){
String xml = "";
MyBusiness myBusiness = readXmlStr(xml, MyBusiness.class);
}
from xstream.
Related Issues (20)
- Stackoverflow CVE-2022-40151 HOT 6
- XStream cant`t support Inherit JavaBean
- Unable to serialize Oracle DB objects using xstream HOT 3
- XStream Core latest version 1.4.19 is vulnerable HOT 1
- We have requested that one will remain for woodstox (CVE-2022-40152), and that the duplicates (CVE-2022-40153, CVE-2022-40154, CVE-2022-40155 and CVE-2022-40156) will be deleted. Those using Woodstox in Xstream have DTD support enabled by default, at least that's the way how the vulnerability in woodstox was found, see [Xstream fuzz target](https://github.com/google/oss-fuzz/blob/master/projects/xstream/XmlFuzzer.java). HOT 1
- The class `SerializationMembers` can not be used in `native-image`.
- Any plans to support GraalVM Native Image? HOT 4
- Using xStream_1_4_20, Not able to convert AtomicReference atomicRef = new AtomicReference(); with default value. It is giving NullpointerException. HOT 1
- Illegal reflective access by com.thoughtworks.xstream.converters.reflection.FieldDictionary HOT 1
- unable to deserialize to object HOT 2
- Convert in mother class won't work HOT 2
- XStreamOmitField default value HOT 1
- how to remove the class attribute when serializing Java Bean to xml? HOT 1
- `PrettyPrintWriter` cannot write emoji in XML 1.1 mode HOT 1
- How to set byte array not to do conversion?Thanks. HOT 1
- Unmodifiable Lists converter HOT 1
- how both text and sub tags exist HOT 3
- DomReader: slow performance $O(n^2)$ when deserialize on millions of elements to list HOT 3
- B
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from xstream.