insert calculated field during serialization

835 views
Skip to first unread message

Marcin Biegan

unread,
Nov 24, 2014, 1:30:59 PM11/24/14
to jackso...@googlegroups.com
Hi,

I'm rewriting a library which decouples model from serialized view definition and I want to do that with Jackson (as it's already used for other stuff). It all looks great - filters and mixins allow to easily include just a subset of fields. What I have trouble with is adding a new field, computed before/during serialization (just like additional getter).

I've seen that it was mentioned multiple times all over (e.g. http://stackoverflow.com/questions/20907488/injecting-additional-fields-in-serialization or http://stackoverflow.com/questions/19434888/inject-a-field-in-json-serialization), but all these solutions look like workarounds or require defining custom serializers.

Has anything changed recently in Jackson which would allow to achieve that in a prettier way? I actually think that when serializing object of A class, a decent approach would be to define a wrapper inheriting from A with additional attributes and serialize it, but sometimes it won't be feasible (e.g. final class).
What's currently the best approach to achieve this?

Thanks,
Marcin Biegan

Tatu Saloranta

unread,
Nov 24, 2014, 1:50:04 PM11/24/14
to jackso...@googlegroups.com
Unfortunately this belongs to a list of (really-)nice-to-have features that have not yet been implemented. There is a github issue this, although I don't remember off-hand the id.

My idea is to allow decorator of sorts, possibly added via `@JsonSerialize`; but one question is as to where to allow addition of entries. Given streaming nature of data-binding, choices would really be before and/or after; perhaps callback would be called twice, once after START_OBJECT (and possibly type id), once right before END_OBJECT.

In the meantime, use of custom serializers is probably the way to go: use of delegating-serializer might help. You could, for example, use 'ObjectMapper.valueToTree()' to get a JsonNode that represents input value, then append fields, and serialize that. In case of delegating serializer, this would mean using `JsonNode` as the delegate value.

It should be possible to make this work in somewhat generic fashion by adding `BeanSerializerModifier`, taking original serializer, and maybe using that to serialize into `TokenBuffer`, then reading as tree... but it does get
unnecessarily complicated.

Come to think of this; another way to use `BeanSerializerModifier` would be to create and custom `BeanPropertyWriter` instances. This would probably be the cleanest way, as the property/-ies added would behave much like any other property. Quite a bit of work, but could work really well.

-+ Tatu +-


--
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.

Marcin Biegan

unread,
Dec 1, 2014, 5:42:40 PM12/1/14
to jackso...@googlegroups.com
I think I'll go with BeanSerializerModifier, looks like the best approach.

Thanks,
Marcin
Reply all
Reply to author
Forward
0 new messages