There are a couple of ways to achieve custom null value serialising:

  1. If you want to serialise a null as an empty String, try using this annotation on a property, or setter:

     @JsonSetter(nulls=Nulls.AS_EMPTY)
    

or the same for specific mapper:

MAPPER.configOverride(String.class).setSetterInfo(JsonSetter.Value.forValueNulls(Nulls.AS_EMPTY));
  1. You can initialise properties with default values on the declaration or in the getter.
  2. As you've already mentioned, by providing a custom serialiser.

I did try your code, and that serialised null value as expected when using an ObjectMapper instead of JodaMapper. Is there any particular reason for using a JodaMapper?

Answer from Dmytro Rybachok on Stack Overflow
🌐
Google Groups
groups.google.com › g › dropwizard-user › c › 9RACX-_SrUU
Serialize null return values to empty string and not 'null'
In this case I would like the 'hole' ... it looks like the best way to do this is by implementing a custom serializer for null values http://wiki.fasterxml.com/JacksonHowToCustomSerializers and registering this with the ObjectMapper....
🌐
GitHub
github.com › FasterXML › jackson-module-kotlin › issues › 440
Kotlin FeatureRequest: Serialize null string to empty string · Issue #440 · FasterXML/jackson-module-kotlin
May 5, 2021 - Describe the solution you'd like If a kotlin data class has a null string, please convert it to "" in the json. (We use nullable strings over empty strings, because the null handling is easier, than the empty string handling.
Author: FasterXML
🌐
Baeldung
baeldung.com › home › json › jackson › jackson – working with maps and nulls
Jackson – Working With Maps and Nulls
May 2, 2023 - class MyDtoNullKeySerializer extends StdSerializer<Object> { public MyDtoNullKeySerializer() { this(null); } public MyDtoNullKeySerializer(Class<Object> t) { super(t); } @Override public void serialize(Object nullKey, JsonGenerator jsonGenerator, SerializerProvider unused) throws IOException, JsonProcessingException { jsonGenerator.writeFieldName(""); } } Now the Map with the null key will work just fine – and the null key will be written as an empty String: @Test public void givenAllowingMapObjectWithNullKey_whenWriting_thenCorrect() throws JsonProcessingException { ObjectMapper mapper = ne
🌐
Java Guides
javaguides.net › 2019 › 04 › jackson-ignore-null-and-empty-fields-on-serialization.html
Jackson Ignore Null and Empty Fields on Serialization
April 24, 2019 - ObjectMapper mapper = new ObjectMapper(); mapper.setSerializationInclusion(Include.NON_NULL); mapper.setSerializationInclusion(Include.NON_EMPTY); mapper.enable(SerializationFeature.INDENT_OUTPUT); Employee employee = new Employee(10, null, ""); String result = mapper.writeValueAsString(employee); System.out.println(result); } } ... The source code of this article available on my GitHub repository at https://github.com/RameshMF/jackson-json-tutorial
🌐
GitHub
github.com › micronaut-projects › micronaut-core › issues › 5445
Jackson serialization treat empty string as null · Issue #5445 · micronaut-projects/micronaut-core
May 18, 2021 - Error decoding HTTP response body: Error decoding stream for type [class com.johnowl.Response]: Instantiation of [simple type, class com.johnowl.Response] value failed for JSON property value2 due to missing (therefore NULL) value for creator parameter value2 which is a non-nullable type at [Source: (byte[])"{"value1":"11111"}"; line: 1, column: 18] (through reference chain: com.johnowl.Response["value2"]) io.micronaut.http.client.exceptions.HttpClientResponseException: Error decoding HTTP response body: Error decoding stream for type [class com.johnowl.Response]: Instantiation of [simple type
Author: micronaut-projects
Find elsewhere
🌐
Java By Examples
javabyexamples.com › control-how-jackson-serializes-null-values
Control How Jackson Serializes Null Values
As a result, all serialization operations should discard null values. objectMapper.setSerializationInclusion(Include.NON_NULL); Lastly, the Include enum also contains other values which we'll list here for reference: public enum Include { ALWAYS, NON_NULL, NON_ABSENT, NON_EMPTY, NON_DEFAULT, USE_DEFAULTS; } In this tutorial, we've looked at how we can ignore null values during serialization using Jackson.
🌐
GitHub
github.com › FasterXML › jackson-dataformats-text › issues › 130
Empty String deserialized as `null` instead of empty string · Issue #130 · FasterXML/jackson-dataformats-text
April 25, 2019 - I have inherited a large codebase where I am trying to migrate from jackson 2.6.4 to 2.9.8. The project contains multiple YAML files which are deserialized using com.fasterxml.jackson.dataformat.yaml.YAMLMapper. Most of these files contain empty fields e.g. ... With version 2.6.4 these fields were being decoded to empty strings, while after migrating to 2.9.8 the field evaluated to null.
Author: FasterXML
Top answer
1 of 2
6

After looking through documentation I found that Jackson uses nullsUsing parameter in JsonSerialize. So for example:

@JsonSerialize(nullsUsing = NullMapSerializer.class)
private Map<String, String> map;

I defined different serializers like so:

public class NullMapSerializer extends JsonSerializer<Map> {
    @Override
    public void serialize(
            final Map value,
            final JsonGenerator jsonGenerator,
            final SerializerProvider serializerProvider
    ) throws IOException {
        jsonGenerator.writeStartObject();
        jsonGenerator.writeEndObject();
    }
}

public class NullListSerializer extends JsonSerializer<List> {
    @Override
    public void serialize(
            final List list,
            final JsonGenerator jsonGenerator,
            final SerializerProvider serializerProvider
    ) throws IOException {
        jsonGenerator.writeStartArray();
        jsonGenerator.writeEndArray();
    }
}

public class NullStringSerializer extends JsonSerializer<String> {

    @Override
    public void serialize(
            final String value,
            final JsonGenerator jsonGenerator,
            final SerializerProvider serializerProvider
    ) throws IOException {
        jsonGenerator.writeString(StringUtils.EMPTY);
    }
}

This serializes fields into {}, [], or "" whenever values are null, and normally otherwise.

2 of 2
1

If you want to have a general method for any object, there can be used the following:

public class NullObjectSerializer extends JsonSerializer<Object> {

    @Override
    public void serialize(Object action, JsonGenerator generator, SerializerProvider provider) throws IOException {
        generator.writeString(StringUtils.EMPTY);
    }
}

And the field will be marked:

@JsonSerialize(nullsUsing = NullObjectSerializer.class)
private MyEnum enumField;
🌐
GitHub
github.com › FasterXML › jackson-databind › issues › 3333
`@JsonSetter(nulls=...)` not working for serialization? · Issue #3333 · FasterXML/jackson-databind
November 24, 2021 - I would like to achieve a result when all null string should be converted into empty string in a serialization. I have tried: using configOverride · objectMapper.configOverride(String.class).setSetterInfo(JsonSetter.Value.forValueNulls(Nulls.AS_EMPTY)); Using @JsonSetter directly to the field. @JsonSetter(nulls=Nulls.AS_EMPTY) None of them are working. I even tried to used other Nulls value, eg. Nulls.SKIP, but it is still not working. FYI, I use Jackson module v2.11.2.
Author: FasterXML
🌐
GitHub
github.com › schmittjoh › serializer › issues › 566
Serialize null values as empty string · Issue #566 · schmittjoh/serializer
March 31, 2016 - Hi, I would like to force the serialization of null values as empty strings '' instead of null. I set the option setSerializeNull(true) to force serialization of null values, but how to tra...
Author: schmittjoh
🌐
GitHub
github.com › FasterXML › jackson-databind › issues › 1885
Add configurability to make Jackson deserialize empty objects (`{ }`) as `null`s · Issue #1885 · FasterXML/jackson-databind
January 11, 2018 - This is breaking a lot of functional pieces in our code, which were earlier relying on a NULL value being sent for a property, which was sent as an empty Object in API request · We figured out that Jackson Deserialization already offers the following two configurations for Arrays and Strings, where empty values gets unmarshalled as NULL.
Author: FasterXML
🌐
GitHub
github.com › FasterXML › jackson-databind › issues › 768
Add `DeserializationFeature` for converting empty String ("") into `null` on deserialization · Issue #768 · FasterXML/jackson-databind
April 23, 2015 - Hi Jackson community! I often here from our front-end guys, that it's required additional coding to pass null value if user does not specify value for a specific field on HTML form (especially using AngularJS). Empty string passed instead.
Author: FasterXML
🌐
HowToDoInJava
howtodoinjava.com › home › jackson › jackson – ignoring null, empty and absent values
Jackson - Ignoring Null, Empty and Absent Values - HowToDoInJava
September 1, 2022 - Empty strings of length 0. Empty containers such as arrays/collections of size 0. To ignore null and empty values, we use one of the values present in the @JsonInclude.Include enum: ... When applying @JsonInclude at class level, all the fields with NULL values will be ignored during serialization.