On Mon, Feb 13, 2017 at 4:46 AM, Clément Poulain <plon...@gmail.com> wrote:
> Hi Tatu, thanks for the reply.
>
> I wanted to avoid using a wrapper as my REST API is built on top of
> interfaces used for other things, and I would like to keep them untouched.
> I have a large number of calls returning any Collection (Set, List,
> UnmodifiableSet, etc.), so doing it purely on Jackson side would avoid
> modifying all of them. I found it to be elegant to properly separate
> business logic from serialization logic.
>
> I'm curious, why isn't BeanSerializerModifier.modifySerializer() called for
> Collection?


Because `modifyCollectionSerializer()` is called for it; they are type-specific.

-+ Tatu +-

>
> Thanks,
>
> On Tuesday, February 7, 2017 at 8:28:56 PM UTC+1, Tatu Saloranta wrote:
>>
>> There is no functionality to do it the way you suggested (although
>> immutability of classes is not a blocker, you could use mix-in
>> annotations).
>>
>> But wouldn't it be simpler to use a simple wrapper structure to get
>> structure match the output, instead of trying to change the whole
>> serialization logic to produce different structure than natural one?
>>
>> public class Wrapper {
>>     public List<Item> items;
>>
>>    public Wrapper(List<Item> l) { items = l; }
>>
>>     public int getSize() { return items.size(); }
>> }
>>
>> That would produce output like what you are looking for.
>>
>> -+ Tatu +-
>>
>>
>>
>> On Tue, Feb 7, 2017 at 6:26 AM, Clément Poulain <plo...@gmail.com> wrote:
>> > Hi there,
>> >
>> > I'm trying to implement pagination using Jersey and Jackson. I don't
>> > want to
>> > modify my API, so I would like the pagination to take place during
>> > serialization.
>> > To simplify, lets pretend I simply want to add the size of a collection
>> > when
>> > serializing it.
>> >
>> > The current output is:
>> >
>> > [
>> >   {
>> >     pojo1
>> >   }, {
>> >     pojo2
>> >   }, {
>> >     pojo3
>> >   }
>> > ]
>> >
>> > What I try to get is:
>> >
>> > {
>> >   size: 3,
>> >   items: [
>> >     {
>> >       pojo1
>> >     }, {
>> >       pojo2
>> >     }, {
>> >       pojo3
>> >     }
>> >   ]
>> > }
>> >
>> > I don't have control on the collections I receive to serialize, so I
>> > cannot
>> > annotate a class like described in
>> > http://www.cowtowncoder.com/blog/archives/2013/10/entry_482.html.
>> >
>> > I tried to register a `BeanSerializerModifier` to my `ObjectMapper` but
>> > I
>> > have two issues:
>> >
>> > 1. it looks like collections (List, Set...) don't go through my modifier
>> >
>> > MAPPER.registerModule(new SimpleModule().setSerializerModifier(new
>> > BeanSerializerModifier() {
>> >     @Override
>> >     public JsonSerializer<?> modifySerializer(SerializationConfig
>> > config,
>> > BeanDescription desc, JsonSerializer<?> serializer) {
>> >
>> >         System.out.println(desc.getBeanClass()); // <== prints "pojo",
>> > "int", "String" ... but never "Collection" or "Set" or "List" or...
>> >
>> >         return serializer;
>> >     }
>> > }));
>> >
>> >
>> > 2. (let's say we solve 1.) How to make sure this conversion occurs only
>> > for
>> > root collection and not for nested collections inside pojo?
>> > I'm asking because I tried also to use a `StdConverter` instead of a
>> > `BeanSerializerModifier`:
>> >
>> > MAPPER.registerModule(new SimpleModule()
>> >     .addSerializer(Collection.class, new
>> > StdDelegatingSerializer(Collection.class,
>> >         new StdConverter<Collection, PaginationCollection>() {
>> >             @Override
>> >             public PaginationCollection convert(Collection value) {
>> >                 return new PaginationCollection(value);
>> >             }
>> >         }))
>> >
>> > It logically ends up into a `StackOverflowException` as
>> > `PaginationCollection` is also containing a `Collection` (for "items"),
>> > which in turn was converted to `PaginationCollection`, etc, etc.
>> > Even if the final solution does not involve a `Converter`, this will
>> > need to
>> > be handled properly.
>> >
>> > TL;DR: what is the best way to modify serialization for root collection
>> > only?
>> >
>> > Thanks!
>> >
>> > --
>> > You received this message because you are subscribed to the Google
>> > Groups
>> > "jackson-user" group.
>> > To unsubscribe from this group and stop receiving emails from it, send
>> > an
>> > email to jackson-user...@googlegroups.com.
>> > To post to this group, send email to jackso...@googlegroups.com.
>> > For more options, visit https://groups.google.com/d/optout.
>
> --
> You received this message because you are subscribed to the Google Groups
> "jackson-user" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to jackson-user+unsubscr...@googlegroups.com.
> To post to this group, send email to jackson-user@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
"jackson-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to jackson-user+unsubscr...@googlegroups.com.
To post to this group, send email to jackson-user@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to