-
-
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
base: 3.x
Are you sure you want to change the base?
Changes from 5 commits
b7b55f2
c3b038e
740b399
209859d
79098fd
7d3714b
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 |
|---|---|---|
| @@ -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 | ||
| } | ||
| } | ||
|
|
||
|
|
||
| } |
| 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(); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -58,12 +58,28 @@ public JsonFormat.Value findFormatOverrides(MapperConfig<?> config) { | |
| @Override | ||
| public JsonFormat.Value findPropertyFormat(MapperConfig<?> config, Class<?> baseType) | ||
|
Member
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. 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
Contributor
Author
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. 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?
Contributor
Author
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. I also added a test for this behavior |
||
| { | ||
| JsonFormat.Value v0 = EMPTY_FORMAT.withRadix(config.getDefaultRadix()); | ||
| JsonFormat.Value v1 = config.getDefaultPropertyFormat(baseType); | ||
| JsonFormat.Value v2 = findFormatOverrides(config); | ||
| if (v1 == null) { | ||
| return (v2 == null) ? EMPTY_FORMAT : v2; | ||
| JsonFormat.Value v2 = null; | ||
| AnnotationIntrospector intr = config.getAnnotationIntrospector(); | ||
| if (intr != null) { | ||
| AnnotatedMember member = getMember(); | ||
|
||
| if (member != null) { | ||
| v2 = intr.findFormat(config, member); | ||
| } | ||
| } | ||
|
|
||
| JsonFormat.Value formatValue = EMPTY_FORMAT; | ||
| if (v0 != null) { | ||
|
||
| formatValue = formatValue.withOverrides(v0); | ||
| } | ||
| if (v1 != null) { | ||
| formatValue = formatValue.withOverrides(v1); | ||
| } | ||
| if (v2 != null) { | ||
| formatValue = formatValue.withOverrides(v2); | ||
| } | ||
| return (v2 == null) ? v1 : v1.withOverrides(v2); | ||
| return formatValue; | ||
| } | ||
|
|
||
| @Override | ||
|
|
||
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.
Unnecessary?
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.
Removed.