-
Notifications
You must be signed in to change notification settings - Fork 18
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
Allow to deserialize an empty string to null #630
Changes from all commits
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 | ||||
---|---|---|---|---|---|---|
|
@@ -292,6 +292,17 @@ private Boolean decodeBooleanSlow() throws IOException { | |||||
return null; | ||||||
} | ||||||
case START_OBJECT, END_OBJECT, END_ARRAY, FIELD_NAME -> throw unexpectedToken(JsonToken.VALUE_TRUE, t); | ||||||
case VALUE_STRING -> { | ||||||
String string = parser.getText(); | ||||||
if (string.isEmpty()) { | ||||||
return null; | ||||||
} | ||||||
try { | ||||||
return Boolean.parseBoolean(string); | ||||||
} catch (NumberFormatException e) { | ||||||
throw createDeserializationException("Unable to coerce string to boolean", string); | ||||||
} | ||||||
} | ||||||
default -> { | ||||||
return parser.getValueAsBoolean(); | ||||||
} | ||||||
|
@@ -312,6 +323,17 @@ public byte decodeByte() throws IOException { | |||||
public Byte decodeByteNullable() throws IOException { | ||||||
JsonToken t = nextToken(); | ||||||
switch (t) { | ||||||
case VALUE_STRING -> { | ||||||
String string = parser.getText(); | ||||||
if (string.isEmpty()) { | ||||||
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.
Suggested change
|
||||||
return null; | ||||||
} | ||||||
try { | ||||||
return Byte.parseByte(string); | ||||||
} catch (NumberFormatException e) { | ||||||
throw createDeserializationException("Unable to coerce string to byte", string); | ||||||
} | ||||||
} | ||||||
case VALUE_TRUE -> { | ||||||
return 1; | ||||||
} | ||||||
|
@@ -353,6 +375,17 @@ public short decodeShort() throws IOException { | |||||
public Short decodeShortNullable() throws IOException { | ||||||
JsonToken t = nextToken(); | ||||||
switch (t) { | ||||||
case VALUE_STRING -> { | ||||||
String string = parser.getText(); | ||||||
if (string.isEmpty()) { | ||||||
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.
Suggested change
|
||||||
return null; | ||||||
} | ||||||
try { | ||||||
return Short.parseShort(string); | ||||||
} catch (NumberFormatException e) { | ||||||
throw createDeserializationException("Unable to coerce string to short", string); | ||||||
} | ||||||
} | ||||||
case VALUE_TRUE -> { | ||||||
return 1; | ||||||
} | ||||||
|
@@ -407,6 +440,9 @@ public Character decodeCharNullable() throws IOException { | |||||
} | ||||||
case VALUE_STRING -> { | ||||||
String string = parser.getText(); | ||||||
if (string.isEmpty()) { | ||||||
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.
Suggested change
|
||||||
return null; | ||||||
} | ||||||
if (string.length() != 1) { | ||||||
throw createDeserializationException("When decoding char value, must give a single character", string); | ||||||
} | ||||||
|
@@ -460,6 +496,9 @@ private Integer decodeIntSlow() throws IOException { | |||||
} | ||||||
case VALUE_STRING -> { | ||||||
String string = parser.getText(); | ||||||
if (string.isEmpty()) { | ||||||
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.
Suggested change
|
||||||
return null; | ||||||
} | ||||||
try { | ||||||
return Integer.parseInt(string); | ||||||
} catch (NumberFormatException e) { | ||||||
|
@@ -528,13 +567,14 @@ private Long decodeLongSlow() throws IOException { | |||||
} | ||||||
case VALUE_STRING -> { | ||||||
String string = parser.getText(); | ||||||
long value; | ||||||
if (string.isEmpty()) { | ||||||
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.
Suggested change
|
||||||
return null; | ||||||
} | ||||||
try { | ||||||
value = Long.parseLong(string); | ||||||
return Long.parseLong(string); | ||||||
} catch (NumberFormatException e) { | ||||||
throw createDeserializationException("Unable to coerce string to integer", string); | ||||||
} | ||||||
return value; | ||||||
} | ||||||
case VALUE_FALSE -> { | ||||||
return 0L; | ||||||
|
@@ -579,13 +619,14 @@ public Float decodeFloatNullable() throws IOException { | |||||
switch (t) { | ||||||
case VALUE_STRING -> { | ||||||
String string = parser.getText(); | ||||||
float value; | ||||||
if (string.isEmpty()) { | ||||||
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.
Suggested change
|
||||||
return null; | ||||||
} | ||||||
try { | ||||||
value = Float.parseFloat(string); | ||||||
return Float.parseFloat(string); | ||||||
} catch (NumberFormatException e) { | ||||||
throw createDeserializationException("Unable to coerce string to float", string); | ||||||
} | ||||||
return value; | ||||||
} | ||||||
case START_ARRAY -> { | ||||||
if (beginUnwrapArray(t)) { | ||||||
|
@@ -641,6 +682,9 @@ private Double decodeDoubleSlow(JsonToken t) throws IOException { | |||||
} | ||||||
case VALUE_STRING -> { | ||||||
String string = parser.getText(); | ||||||
if (string.isEmpty()) { | ||||||
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.
Suggested change
|
||||||
return null; | ||||||
} | ||||||
try { | ||||||
return Double.parseDouble(string); | ||||||
} catch (NumberFormatException e) { | ||||||
|
@@ -691,6 +735,9 @@ public BigInteger decodeBigIntegerNullable() throws IOException { | |||||
switch (t) { | ||||||
case VALUE_STRING -> { | ||||||
String string = parser.getText(); | ||||||
if (string.isEmpty()) { | ||||||
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.
Suggested change
|
||||||
return null; | ||||||
} | ||||||
try { | ||||||
return new BigInteger(string); | ||||||
} catch (NumberFormatException e) { | ||||||
|
@@ -741,6 +788,9 @@ public BigDecimal decodeBigDecimalNullable() throws IOException { | |||||
switch (t) { | ||||||
case VALUE_STRING -> { | ||||||
String string = parser.getText(); | ||||||
if (string.isEmpty()) { | ||||||
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.
Suggested change
|
||||||
return null; | ||||||
} | ||||||
try { | ||||||
return new BigDecimal(string); | ||||||
} catch (NumberFormatException e) { | ||||||
|
@@ -793,7 +843,8 @@ private Number decodeNumber() throws IOException { | |||||
|
||||||
@Override | ||||||
public boolean decodeNull() throws IOException { | ||||||
if (peekToken() == JsonToken.VALUE_NULL) { | ||||||
JsonToken jsonToken = peekToken(); | ||||||
if (jsonToken == JsonToken.VALUE_NULL || jsonToken == JsonToken.VALUE_STRING && parser.getText().isEmpty()) { | ||||||
nextToken(); | ||||||
return true; | ||||||
} else { | ||||||
|
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.
Should we use?
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.
why?