How to choose which field to serialize - Jackson

Asked

Viewed 180 times

2

I’m using Jackson to convert my objects into Json. The fields I don’t want to serialize milestone with the @Jsonignore annotation, but in some cases I wish I could serialize these fields. Is there any way to choose when to serialize them?

  • You can’t know the whole context, but it seems the most viable thing for you here is to use JsonView, then in each case use one. For example, it is very common to serialize little thing in listing (a view for that) and more data in the register/edit (another view for this)

1 answer

2


You have two alternatives:

1 - You create classes to represent the information that must be serialized for each case and populate these classes according to need.

2 - You can create a custom serializer that implements the interface com.fasterxml.jackson.databind.JsonSerializer, register this serializer in your ObjectMapper and make the serialization programmatically according to the need.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.