tags:

views:

24

answers:

1

We are using XStream for our restful services. However, Xstream gives us varying results for fields with the same values. assume it Book object:

public class Book{
 public String name "myName";
 public Listauthors = new List();
 public String subject "mySubject";

 public Book(){
 }
}

The json for this is:

{"Book":{"name":"myName", "authors":"", "subject":["mySubject"]}}

however, if I add authors to the collection I get a different result.

{"Book":{"name":"myName", "authors":["author1","author2","author3"],"subject":"mySubject"}}

Has anyone run into this issue and know of a solution?

A: 

In the first place, your Book instance above contains errors. Here's what I suppose it should look like:

public class Book{
    public String name = "myName";
    public List authors = new ArrayList();
    public String subject = "mySubject";

    public Book(){
    }
}

Now:

{"Book":{"name":"myName", "authors":"", "subject":["mySubject"]}}

Are you sure this is what xstream is returning for the Book object listed above? This doesn't seems right, since the subject property is a String and not a String[] or other type of collection. The JSON encoding for the first example you give (book without authors) should be:

{"Book":{"name":"myName", "authors":"", "subject":"mySubject"}}

Unless your Book looked something like this:

public class Book{
    public String name = "myName";
    public List authors = new List();
    public String[] subject = {"mySubject"};

    public Book(){
    }
}

Bottom line: make sure you are not declaring your subject as a collection.

As a bonus tip, try to post working code on your questions. It's easier that way to get meaningful answers. So my guess is that your Book class is declaring subject to be some kind of collection

Cesar
Sorry the pojo for the typo.Yes I am sure the subject is a simple string. Actually the real class has a few colections and this same issue will happen with other combinations of fields. This is causing the UI Javascript to get bloated with all the if statements to manage these crazy results.
Sean