Reputation: 16865
So I have
class Foo {
double bar = -30000.0
double baz = 200
}
-30000.0
is not actually a valid value, it's the default value emitted by the hardware, realistically this should be a null value. I don't want to serialize this value, instead I'd like to omit the key completely if it's this value (the same way if I'd set skip nulls). problem is, baz can also be be -30000.0 but there it's valid. There is actually more conditional logic depending on the field/value, but this is the simplest example.
I feel like I want to write this
class Foo {
@Skip30k
double bar = -30000.0
double baz = -30000.0
}
the output of this should be
"{"baz":-30000.0}"
I've read baeldung's post and other things, but it seems like my options are on a custom type (this is a primitive) or global. How can I achieve this custom serialization?
Upvotes: 0
Views: 1180
Reputation: 131117
You may want to try @JsonInclude
:
@JsonInclude(value = JsonInclude.Include.CUSTOM,
valueFilter = Skip30kFilter.class)
private double bar;
public class Skip30kFilter {
@Override
public boolean equals(Object other) {
double value = (Double) other;
return value > -30000.0;
}
}
You also may consider a custom annotation and a serializer, as described in this answer.
Upvotes: 3