Giter Site home page Giter Site logo

Comments (3)

joehni avatar joehni commented on August 16, 2024 1

Now your class members have the exact type and XStream knows what to do.

from xstream.

joehni avatar joehni commented on August 16, 2024

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.

kiritokun07 avatar kiritokun07 commented on August 16, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.