0

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?

Ayush
  • 41,754
  • 51
  • 164
  • 239

2 Answers2

1

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>
KrHubert
  • 1,030
  • 7
  • 17
  • Yeah, it was my bad. The code I posted above wasn't exactly what I was running. I was passing `name` to the constructor via another variable, which happened to be an empty string. Fixing that fixed the xml output. – Ayush Apr 26 '12 at 21:24
0

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.

Ayush
  • 41,754
  • 51
  • 164
  • 239