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 [email protected].
To post to this group, send email to [email protected].
For more options, visit https://groups.google.com/d/optout.