users@jersey.java.net

Re: JSON problem

From: Jakub Podlesak <Jakub.Podlesak_at_Sun.COM>
Date: Thu, 24 Apr 2008 09:53:43 +0200

Hi Reece,

On Wed, Apr 23, 2008 at 08:13:33PM +0200, Paul Sandoz wrote:
> Hi Reece,
>
> On Apr 23, 2008, at 7:43 PM, Reece Garrett wrote:
>
> >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?
> >
>
> The intent is it should not affect XML serialization. The
> JSONJAXBContext extends JAXBContext and ensures compatibility with
> existing behavior when JSON serialization is not enabled on the
> Marshaller or Unmarshaller obtained from the context,
>
> Have you tried it with XML serialization and encountered a problem?
> If so can you provide more details?

As Paul wrote, there should not be a problem. Please make sure
you use both json and xml mime types in the @ProduceMime annotation:

@ProduceMime({"application/json","application/xml"})

When making the request, make sure you ask for proper mime-type then:

curl -HAccept:application/xml $resource_url

Anyway, if you was able to get XML before you have added the context resolver,
there is no obvious reason, why it should stop working.
If this is the case, i really need some more details (ideally your source code).

Thanks,

~Jakub

>
> Paul.
>
> >-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
> >>
> >
> >
> >---------------------------------------------------------------------
> >To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> >For additional commands, e-mail: users-help_at_jersey.dev.java.net
> >
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>