0

I tried using a custom serializer but it seems to never even go there when the value is null, so it never does my code to print empty string:

@JsonSerialize(using = NullDateSerializer.class)
    public Timestamp getFinalizedDate() {
        return finalizedDate;
    }

--

public class NullDateSerializer extends JsonSerializer<Date> {

    @Override
    public void serialize(Date value, JsonGenerator jgen, SerializerProvider provider)
            throws IOException, JsonProcessingException {
        if (value == null)
            jgen.writeString("");
        else
            jgen.writeNumber(value.getTime());
    }
}

So what do I do then?

I found several threads on writing nulls across the whole app using some ObjectMapper, and though I prefer to do it explicitly like using @JsonSerialize I tried that but it did not even work. I guess the old solutions dont work anymore. Regardless, I want to do it with a @JsonSerialize.

Using jackson 1.9.3 spring 3.2.2

1
  • P.S. I got a syntax error sorted out to get this solution to compile: stackoverflow.com/questions/12934045/… But it doesnt work. Breakpoint in NullSerializer never hit, json for null dates come out "finalizedDate":null Commented May 8, 2014 at 20:26

1 Answer 1

0

Isn't serialization for converting Object to String? The getter method you wrote seems to be returning a Timestamp (and not a String) ... probably that's why the serializer is not getting invoked.

Sign up to request clarification or add additional context in comments.

2 Comments

It uses the getter to get a value to convert to a JSON string value. The serializer DOES get invoked, but never with a NULL value, even though there is a mix of actual dates and nulls. Jackson seems to avoid calling my serializer when the value is null so my if statement is futile.
If this is practice code, can you put the source code on github or some place similar? It might help to look at the source code in its entirety.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.