0

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?

xenoterracide
  • 16,274
  • 24
  • 118
  • 243
  • 2
    *"realistically this should be a null value"* So why not fix you model and make it a null value, i.e. apply the business logic where it belongs, i.e. not in the serializer. --- *"There is actually more conditional logic depending on the field/value"* Again, apply that "conditional logic" in your code and make the model reflect the correct values. – Andreas Jun 11 '19 at 18:02
  • You can create a custom serializer to handle your case: https://www.baeldung.com/jackson-custom-serialization – tantalum Jun 11 '19 at 18:05
  • @Andreas because, there's a client, and a server, and we don't want to push the data from the client to the server – xenoterracide Jun 11 '19 at 18:06
  • 1
    Take a look at my question, it's related to your problem: https://stackoverflow.com/q/56493606/9662601 You can use `@JsonInclude` with a custom filter to solve this. – Samuel Philipp Jun 11 '19 at 18:06
  • @xenoterracide Sorry, but I don't see how that comment in any way addresses what I was talking about in my comment. – Andreas Jun 11 '19 at 18:13
  • @Andreas you're right, maybe it's better said I have no control over the business logic in the client (android), just writing a thin marshalling library, but yes, I could do it that way, and I thought about it, and that's probably the fallback. This is not a "model", it's a [Data Transfer Object](https://martinfowler.com/eaaCatalog/dataTransferObject.html) – xenoterracide Jun 11 '19 at 18:23
  • 2
    Possible duplicate of [Jackson serialization ignore negative values](https://stackoverflow.com/questions/56493606/jackson-serialization-ignore-negative-values) – xenoterracide Jun 11 '19 at 19:18

1 Answers1

2

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.

cassiomolin
  • 124,154
  • 35
  • 280
  • 359