-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Support alternate radixes for numeric values #5445
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
Open
tiger9800
wants to merge
6
commits into
FasterXML:3.x
Choose a base branch
from
tiger9800:3.x-forked
base: 3.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b7b55f2
Support alternate radixes for numeric values
tiger9800 c3b038e
Merge branch '3.x' into 3.x-forked
cowtowncoder 740b399
Merge branch '3.x' into 3.x-forked
cowtowncoder 209859d
Merge branch '3.x' into 3.x-forked
cowtowncoder 79098fd
Merge branch '3.x' into 3.x-forked
cowtowncoder 7d3714b
Fix PR comments
tiger9800 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
52 changes: 52 additions & 0 deletions
52
src/main/java/tools/jackson/databind/deser/std/FromStringWithRadixToNumberDeserializer.java
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| package tools.jackson.databind.deser.std; | ||
|
|
||
| import tools.jackson.core.JsonParser; | ||
| import tools.jackson.core.JsonToken; | ||
| import tools.jackson.databind.DeserializationContext; | ||
|
|
||
| import java.math.BigInteger; | ||
|
|
||
| /** | ||
| * Deserializer used for a string that represents a number in specific radix (base). | ||
| * | ||
| * @since 3.1 | ||
| */ | ||
| public class FromStringWithRadixToNumberDeserializer | ||
| extends StdDeserializer<Number> { | ||
| private final int radix; | ||
|
|
||
| public FromStringWithRadixToNumberDeserializer(StdDeserializer<?> src, int radix) { | ||
| super(src); | ||
| this.radix = radix; | ||
| } | ||
|
|
||
| @Override | ||
| public Number deserialize(JsonParser p, DeserializationContext ctxt) { | ||
| Class<?> handledType = handledType(); | ||
|
|
||
| if (p.currentToken() != JsonToken.VALUE_STRING) { | ||
| ctxt.reportInputMismatch(handledType, | ||
| "Read something other than string when deserializing a value using FromStringWithRadixToNumberDeserializer."); | ||
| } | ||
|
|
||
| String text = p.getString(); | ||
|
|
||
| if (handledType.equals(BigInteger.class)) { | ||
| return new BigInteger(text, radix); | ||
| } else if (handledType.equals(byte.class) || handledType.equals(Byte.class)) { | ||
| return Byte.parseByte(text, radix); | ||
| } else if (handledType.equals(short.class) || handledType.equals(Short.class)) { | ||
| return Short.parseShort(text, radix); | ||
| } else if (handledType.equals(int.class) || handledType.equals(Integer.class)) { | ||
| return Integer.parseInt(text, radix); | ||
| } else if (handledType.equals(long.class) || handledType.equals(Long.class)) { | ||
| return Long.parseLong(text, radix); | ||
| } else { | ||
| ctxt.reportInputMismatch(handledType, | ||
| "Trying to deserialize a non-whole number with NumberToStringWithRadixSerializer"); | ||
| return null;//should not reach here | ||
| } | ||
| } | ||
|
|
||
|
|
||
| } |
40 changes: 40 additions & 0 deletions
40
src/main/java/tools/jackson/databind/deser/std/RadixSerializerCreator.java
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| package tools.jackson.databind.deser.std; | ||
|
|
||
| import com.fasterxml.jackson.annotation.JsonFormat; | ||
| import tools.jackson.databind.BeanProperty; | ||
| import tools.jackson.databind.DeserializationContext; | ||
|
|
||
| import static com.fasterxml.jackson.annotation.JsonFormat.DEFAULT_RADIX; | ||
|
|
||
| /** | ||
| * Factory class for {@link FromStringWithRadixToNumberDeserializer} for deserializers in {@link tools.jackson.databind.deser.jdk.NumberDeserializers} | ||
| * @since 3.1 | ||
| * | ||
| */ | ||
| public class RadixSerializerCreator { | ||
| public static StdDeserializer<? extends Number> createRadixStringDeserializer( | ||
| StdScalarDeserializer<? extends Number> initialDeser, | ||
| DeserializationContext ctxt, BeanProperty property) | ||
| { | ||
| JsonFormat.Value format = initialDeser.findFormatOverrides(ctxt, property, initialDeser.handledType()); | ||
|
|
||
| if (format == null || format.getShape() != JsonFormat.Shape.STRING) { | ||
| return initialDeser; | ||
| } | ||
|
|
||
| if (isSerializeWithRadixOverride(format)) { | ||
| int radix = format.getRadix(); | ||
| return new FromStringWithRadixToNumberDeserializer(initialDeser, radix); | ||
| } | ||
|
|
||
| return initialDeser; | ||
| } | ||
|
|
||
| /** | ||
| * Check if we have a proper {@link JsonFormat} annotation for serializing a number | ||
| * using an alternative radix specified in the annotation. | ||
| */ | ||
| private static boolean isSerializeWithRadixOverride(JsonFormat.Value format) { | ||
| return format.hasNonDefaultRadix(); | ||
| } | ||
| } |
This file contains hidden or 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
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I am bit confused here -- why are changes needed? Shouldn't override handling work for radix just like all other properties? If not, why not (are fixes needed in
jackson-annotations?)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.
The reason is to consider default format out of ConfigOverrides which has the lowest precedence. This way we can make the a given radix apply to all integral types. This is what you meant here, right?
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.
I also added a test for this behavior
DifferentRadixNumberFormatTest#testIntSerializedAsHexStringWithDefaultRadix