I'm using Jackson to convert my objects to Json . The fields I do not want to serialize markup with @JsonIgnore annotation, but in some cases I wanted to serialize those fields. Is there any way to choose when to serialize them?
I'm using Jackson to convert my objects to Json . The fields I do not want to serialize markup with @JsonIgnore annotation, but in some cases I wanted to serialize those fields. Is there any way to choose when to serialize them?
You have two alternatives:
1 - You create classes to represent information that should be serialized for each case and populate those classes as needed.
2 - You can create a custom serializer that implements the com.fasterxml.jackson.databind.JsonSerializer
interface, register that serializer in your ObjectMapper
, and serialize it programmatically as needed.