users@jersey.java.net

Re: JSON problem

From: Reece Garrett <RGARRET_at_co.pierce.wa.us>
Date: Wed, 23 Apr 2008 10:43:15 -0700

Jakub,

Your solution does work great for JSON but I just realized that using the JSONJAXBContext as you described means I loose the ability to serialize to XML. Any thoughts?

-Reece

>>> Jakub Podlesak <Jakub.Podlesak_at_Sun.COM> 4/23/2008 6:48:36 AM >>>

Hi Reece,

you can get the brackets if you provide your own JAXBContext resolver.
Please look at [1] for details.
For your case, you would need to put "[\"employee\"]" into the JSON_ARRAYS property.

~Jakub

[1]http://blogs.sun.com/japod/entry/missing_brackets_at_json_one


On Tue, Apr 22, 2008 at 05:22:26PM -0700, Reece Garrett wrote:
> Greetings,
>
> The issue I am having is that collections with only one element are serialized to json without brackets while collections with more than one object are serialized with brackets. For consistency shouldn't all collections be serialized with brackets regardless of how many elements they contain. I'll further illustrate the problem:
>
> ---------------------------
>
> public class Employee {
>
> private firstName;
>
> private lastName;
>
> ...setters/getters/constructor
> }
>
> public class Employees {
>
> private List<Employee> employees = new ArrayList<Employee>();
>
> ...setters/getters/constructor
> }
>
> ----------------------
>
> Lets say I have an Employees object and the list of employees contains two Employee objects; the json result is:
>
> --> {"employees" : {"employee" : [{"firstName" : "Mega","lastName" : "Tron"}, {"firstName" : "Optimus","lastName" : "Prime"}]}}
>
> Now lets say I have an Employees object and the list of employees contains only one Employee object; the json result is:
>
> --> {"employees" : {"employee" : {"firstName" : "Mega","lastName" : "Tron"}}}
>
> The json I need from an Employees object with only one Employee object it it's employees list is:
>
> --> {"employees" : {"employee" : [{"firstName" : "Mega","lastName" : "Tron"}]}}
>
> It's just a lot harder to consume the json on the client when you have inconsistencies like that. Also, the folks over at Jettision have already dealt with the exact same issue. They solved it by adding a special serializeAsArray() method that can be used by client code to force appropriate serialization. The issue and it's resolution can be viewed at http://jira.codehaus.org/browse/JETTISON-22.
>
> Thanks,
> -Reece
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>