AlexK
AlexK

Reputation: 138

How to disable Jackson serialization feature WRITE_NUMBERS_AS_STRINGS for certain field?

I'm using Jackson 2.8 for serialization of REST-services responses. To prevent numbers corruptions related to Javascript (which store number as 64-bit IEEE 754 floating point values) in most of the services I want to apply the following JsonGenerator Feature

objectMapper.configure(WRITE_NUMBERS_AS_STRINGS, true);

Values of handful of fields are not prone to IEEE 754 conversion error. But when they are serialized as strings it leads to errors in the client app, refactoring of which (in this case) could be tedious and better to avoid.

I've tried using JsonSerialize annotation

@JsonSerialize(as = int.class)      
int status;

but not succeed. It seems WRITE_NUMBERS_AS_STRINGS has precedence over the annotation.

The question is how to (if possible) configure some exclusions from the rule applied by WRITE_NUMBERS_AS_STRINGS feature?

Upvotes: 0

Views: 1453

Answers (1)

varren
varren

Reputation: 14731

From Jackson 2 annotations documentation:

@JsonRawValue: per-property marker that can be used to specify that the value of property is to be included in serialization ''exactly'' as is, with no escaping or decoration

Looks like @JsonRawValue works for your case even with WRITE_NUMBERS_AS_STRINGS.


Another option is to use custom serializer if you have something like Map<String, Long> field; in your POJO, because you can't reach maps key/value with any default annotation.

Upvotes: 2

Related Questions