You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
hi all:
I want to define a class that can deserialize the following XML content and also serialize it into this XML format. But I don't know how to handle the List type anymore.
This class is what I expected to define, but I don't know how to use XStream annotations or other APIs to complete it. Do you have any better suggestions?
@Data@XStreamAlias("stream")
publicstaticclassDemoimplementsSerializable {
privateStringaction;
privateStringuserName;
// TODO how to deal this?privateList<UserAccount> userDataList;
// TODO how to deal this?privateList<User> others;
@DatapublicstaticclassUserAccountimplementsSerializable {
privateStringaccountNo;
}
@DatapublicstaticclassUserimplementsSerializable {
privateStringname;
}
}
The text was updated successfully, but these errors were encountered:
Two comments in advance: XStream has no clue about the Data annotation and you classes don't have to be serializable for XStream.
Then as a general advice: Try to configure the XStream instance so, that it writes the XML you want to read. It is a lot less tedious, if you see the XML that is generated (and then typically also expected when reading).
Then you will have to answer yourself the question how far you want to sacrifice your class types to enable XStream to write the requested structure out of the box? Your XML contains two list elements in stream, i.e. that's something that cannot match the default mechanism that maps from member names to XML elements. The only way to configure XStream to generate or process something like this is the usage of an implicit collection, i.e. your Demo type would not contain two members of type list, but one List of type List of anything (List<List<?>> lists), which the in turn is declared as implicit collection. While this is possible, it is also very nasty when working with the data structure.
The alternative would be to create a custom converter (see XStream tutorials) for Demo that is able to read/write the appropriate member from the name attribute of its list elements and use then a separate instance of the NamedCollectionConverter for each list to write/read the list content.
@joehni It should be noted here that changing this XML structure is not allowed. This means that all collection types must be defined as list node pairs, and collection elements must be defined as row node pairs.
I'm more interested in what you mentioned about using NamedCollectionConverter to do field processing of collection types. If NamedCollectionConverter could not do the conversion of the xml structure I am talking about, is it possible for me to customize the Converter subclass to handle the conversion of collection types?
hi all:
I want to define a class that can deserialize the following XML content and also serialize it into this XML format. But I don't know how to handle the List type anymore.
This class is what I expected to define, but I don't know how to use XStream annotations or other APIs to complete it. Do you have any better suggestions?
The text was updated successfully, but these errors were encountered: