Created
July 19, 2023 13:51
-
-
Save romanbsd/4e241ed527fe9b87255111a8969648a5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.fasterxml.jackson.core.JsonParser; | |
import com.fasterxml.jackson.databind.DeserializationContext; | |
import com.fasterxml.jackson.databind.JsonDeserializer; | |
import com.fasterxml.jackson.databind.module.SimpleModule; | |
import java.io.IOException; | |
public class EmptyStringToNullDeserializer extends JsonDeserializer<String> { | |
@Override | |
public String deserialize(JsonParser jsonParser, DeserializationContext context) throws IOException { | |
String value = jsonParser.getValueAsString(); | |
if (value != null && value.isEmpty()) { | |
return null; | |
} | |
return value; | |
} | |
public static SimpleModule module() { | |
SimpleModule module = new SimpleModule(); | |
module.addDeserializer(String.class, new EmptyStringToNullDeserializer()); | |
return module; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment