onkami
onkami

Reputation: 9411

Jackson JSON serializer: enable or disable serialisation of certain field in runtime?

I have trivial object serialization via Jackson library:

    ObjectMapper objectMapper = new ObjectMapper();
    String jsonText = objectMapper.writeValueAsString(myComplexObject);

I have one field in myComplexObject that I sometimes do not want to edn up in serialized result. I know that I can avoid serialization completely if I declare a field in myComplexObject with @JsonIgnore, but I need that field sometimes present in the JSON. Can I achieve this effect? I can assign that field to NULL or to some other special value in case I do not need it.

Upvotes: 1

Views: 1175

Answers (1)

StaxMan
StaxMan

Reputation: 116512

Quite a few ways, from simple JSON Views to @JsonFilter, explained at "Every day Jackson usage, part 3: Filtering properties" (and its followup, "Advanced filtering with Jackson, Json Filters")

Upvotes: 1

Related Questions