I have a class called Lookup
that has two properties:
public class Lookup {
private String surveyName;
private String GUID;
public Lookup(String name, String guid){
this.surveyName = name;
this.GUID = guid;
}
}
In another class, I have a list of Lookup
that I am trying to serialize and save to file. This is how I'm doing it:
List<Lookup> lookup = new ArrayList<Lookup>();
lookup.add(new Lookup("foo","bar"));
XStream serializer = new XStream();
serializer.alias("Lookups",List.class);
String xml = serializer.toXML(lookup);
The XML I end up with is:
<Lookups>
<Lookup>
<GUID>bar</GUID>
</Lookup>
</Lookups>
As you can see, it only serialized the field GUID
but not the field surveyName
. Why is it ignoring that field?
Are you sure that you don't modify Lookup variable somewhere else. This code runs fine
public class Test {
public static void main(String[] args) {
List<Lookup> lookup = new ArrayList<Lookup>();
lookup.add(new Lookup("foo","bar"));
XStream serializer = new XStream();
serializer.alias("Lookups",List.class);
String xml = serializer.toXML(lookup);
System.out.println(xml);
}
}
class Lookup {
private String surveyName;
private String GUID;
public Lookup(String name, String guid){
this.surveyName = name;
this.GUID = guid;
}
}
Output:
<Lookups>
<Lookup>
<surveyName>foo</surveyName>
<GUID>bar</GUID>
</Lookup>
</Lookups>
Silly me, the mistake was completely on my end. The field name
was receiving an empty string, and thus XStream must have been ignoring it.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.