-
Notifications
You must be signed in to change notification settings - Fork 47
Add mapBlankStringToNull option to JsonNullableModule and JsonNullableJackson3Module #186
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
f90c590
107887a
1de12ad
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,11 +7,31 @@ | |
| public class JsonNullableModule extends Module { | ||
|
|
||
| private final String NAME = "JsonNullableModule"; | ||
| private boolean mapBlankStringToNull = false; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P3: The flag is read only inside setupModule(), which runs once at registration. Calling mapBlankStringToNull() on an already-registered or shared/singleton module silently does nothing, because the deserializers were already constructed with the previous flag. Reserve the setter for pre-registration configuration (e.g. document that it must be called before registerModule) or make the module immutable. Prompt for AI agents |
||
|
|
||
| /** | ||
| * Configures whether blank strings (for example {@code ""} or {@code " "}) deserialized | ||
| * into a non-String {@code JsonNullable} are mapped to {@code JsonNullable.of(null)} | ||
| * instead of {@code JsonNullable.undefined()}. | ||
| * | ||
| * <p>This matters for PATCH semantics: a blank string sent by a client expresses an | ||
| * explicit intent to clear the value, which {@code undefined()} silently swallows. | ||
| * String targets are never affected. | ||
| * | ||
| * <p>Default is {@code false} for backwards compatibility. | ||
| * | ||
| * @param state {@code true} to map blank strings to {@code JsonNullable.of(null)} | ||
| * @return this module, for chaining | ||
| */ | ||
| public JsonNullableModule mapBlankStringToNull(boolean state) { | ||
| this.mapBlankStringToNull = state; | ||
| return this; | ||
| } | ||
|
|
||
| @Override | ||
| public void setupModule(SetupContext context) { | ||
| context.addSerializers(new JsonNullableJackson2Serializers()); | ||
| context.addDeserializers(new JsonNullableJackson2Deserializers()); | ||
| context.addDeserializers(new JsonNullableJackson2Deserializers(mapBlankStringToNull)); | ||
| // Modify type info for JsonNullable | ||
| context.addTypeModifier(new JsonNullableJackson2TypeModifier()); | ||
| context.addBeanSerializerModifier(new JsonNullableJackson2BeanSerializerModifier()); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P2: When enabled, a Unicode whitespace-only string such as
"\u2003"bypasses this return and Jackson attempts to parse it as the target value, causing an exception instead of producing present null. Use a Unicode-aware blank predicate before this branch.Prompt for AI agents