AutoBean Deserialization of JSON that can be an Array or single Object

54 views
Skip to first unread message

kurrazyman

unread,
Sep 29, 2011, 5:37:08 PM9/29/11
to Google Web Toolkit
I have a situation where I am deserialiizing a JSON string into POJOs
using AutoBeans, this all works well apart from the scenario below: -

The JSON String sometimes contains objects in an array but may
sometimes just contain a single item (see below): -

Single include object in first group and array of include objects in
second group: -

{"groups":{"group":[{"id":49,"name":"Default Group","include":{"ref":
50,"type":"screen"}},{"id":83,"name":"Test","include":[{"ref":
50,"type":"screen"},{"ref":84,"type":"screen"}]}]}}

My Group Interface for AutoBeans looks like this: -

public interface Group {
public int getId();
public void setId(int groupId);

public String getName();
public void setName(String name);

public List<ScreenRef> getInclude();
public void setInclude(List<ScreenRef> screenRefs);

public TabBar getTabBar();
public void setTabBar(TabBar tabBar);
}

As you can see it is expecting to find a JSArray for the include
variable, but I cannot guarantee the JSON format. I have also
discovered that if the first Group object that is deserialized
contains an array of include objects then any subsequent Group object
can have either a single include object or an array of include objects
(not sure of why this is but I imagine it's something to do with the
way AutoBeans works), the dilemma I have is: -

1). Is this a bug with AutoBeans (should it always be able to convert
a singleton into a list if that's what is defined in the interface -
seems like it is possible in certain situations as explained above)?
2). Should I look to change my JSON server output so that even if a
single object is returned it is still wrapped in an array?
3). Is there a clever trick I am missing with AutoBeans that will
allow me to deal with this variation?

Any help would be much appreciated

-sowdri-

unread,
Sep 30, 2011, 12:56:43 AM9/30/11
to google-we...@googlegroups.com
>> but I cannot guarantee the JSON format.

What library are you using on the server? 

>> Should I look to change my JSON server output so that even if a 
single object is returned it is still wrapped in an array? 

Yes!

Richard Turner

unread,
Sep 30, 2011, 4:23:51 AM9/30/11
to google-we...@googlegroups.com
Thanks for the reply, the server is using JSON-lib to serialize an XML representation of the data (as two REST APIs are provided), I will look into altering the servers JSON output to force arrays where needed.

Thanks again,

Rich

--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To view this discussion on the web visit https://groups.google.com/d/msg/google-web-toolkit/-/sUIzUnQtICkJ.
To post to this group, send email to google-we...@googlegroups.com.
To unsubscribe from this group, send email to google-web-tool...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.

Reply all
Reply to author
Forward
0 new messages