users@jersey.java.net

[Jersey] Re: Unmarshalling JSON into POJO collections

From: cowwoc <cowwoc_at_bbs.darktech.org>
Date: Tue, 19 Aug 2014 19:23:58 -0400

I also recommend Jackson. It works really well and the author provides
awesome technical support!

https://github.com/FasterXML/jackson-databind/ contains a short
introduction.

Normally Jackson serializes POJO into a JSON map (key-value pairs). If
you want to serialize a POJO directly into an array (as opposed to a map
with a single value) then you can do something like this:

     public class GetUsers
     {
         private final List<URI> users;

         @JsonCreator
         public GetUsers(List<URI> users)
             throws NullPointerException
         {
             this.users = ImmutableList.copyOf(users);
         }

         @JsonValue
         public List<URI> getUsers()
         {
             return users;
         }
     }

@JsonCreator indicates which constructor to use for deserializing from JSON.
@JsonValue indicates that you want to use a single getter method for
serialization as opposed to the default behavior of mapping multiple
getter methods into an associative map.

Good luck! :)

Gili

On 19/08/2014 6:05 PM, Paul O'Fallon wrote:
> You could try switching from MOXy to Jackson (leaving the above code
> the same) and see if that makes a difference. I have code in a
> JerseyTest which looks almost identical to yours and it works (with
> Jackson, anyway). In the past I've had some seemingly trivial
> serialization code that didn't work with MOXy but worked as soon as I
> switched to Jackson.
>
> - Paul
>
>
> On Tue, Aug 19, 2014 at 5:37 PM, John Brooks <iresprite_at_gmail.com
> <mailto:iresprite_at_gmail.com>> wrote:
>
> Honestly, I'm happy to consider other solutions if they work! I
> picked MOXy primarily because the documentation indicated it was
> the default and preferred way of handling JSON. I don't mind
> taking a step back and approaching it via Jackson or JAXB. Could
> either of you point me at some good working examples?
>
> Thanks again!
> John
>
>
> On Tue, Aug 19, 2014 at 5:23 PM, cowwoc <cowwoc_at_bbs.darktech.org
> <mailto:cowwoc_at_bbs.darktech.org>> wrote:
>
> Same with Jackson, but I think John is looking for a
> MOXY-specific solution so this doesn't help him.
>
> Gili
>
>
> On 19/08/2014 5:14 PM, Guy Rouillier wrote:
>
> We are using JAXB to serialize and deserialize annotated
> classes. Works well with single objects as well as list
> of objects. Also works equally well with XML and JSON.
>
> On 8/19/2014 2:29 PM, John Brooks wrote:
>
> I've been banging around on this some more and coming
> up with no
> progress (including posting to StackOverflow
> <http://stackoverflow.com/questions/25310759/deserializing-json-with-jersey-and-moxy-into-a-list-collection>),
>
> so I'm wondering if it's time to come up with a new
> approach. How do
> other folks here deserialize JSON into collections?
>
> Cheers!
> John
>
>
>
>
>
>