Skip to content

Commit 8ca3bf3

Browse files
committed
Use a special marker default for radix in JsonFormat (#320)
1 parent 4cffce0 commit 8ca3bf3

File tree

2 files changed

+43
-20
lines changed

2 files changed

+43
-20
lines changed

src/main/java/com/fasterxml/jackson/annotation/JsonFormat.java

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,13 @@
8181
public final static String DEFAULT_TIMEZONE = "##default";
8282

8383
/**
84-
* Value that indicates the default radix(numeric base) to use for outputting {@link java.lang.Number} properties
85-
* when {@link Shape#STRING} is specified.
84+
* Value that indicates the default radix(numeric base) should be used when outputting
85+
* {@link java.lang.Number} properties when {@link Shape#STRING} is specified.
86+
* This is a marker signaling that {@link JsonFormat.Value} with this radix should
87+
* not override the radix of another {@link JsonFormat.Value}.
88+
* @since 2.21
8689
*/
87-
public final static byte DEFAULT_RADIX = 10;
90+
public final static int DEFAULT_RADIX = -1;
8891

8992
/**
9093
* Datatype-specific additional piece of configuration that may be used
@@ -142,7 +145,7 @@
142145
*
143146
* @since 2.21
144147
*/
145-
public byte radix() default DEFAULT_RADIX;
148+
public int radix() default DEFAULT_RADIX;
146149

147150
/**
148151
* Set of {@link JsonFormat.Feature}s to explicitly enable with respect
@@ -539,7 +542,7 @@ public static class Value
539542
/**
540543
* @since 2.21
541544
*/
542-
private final byte _radix;
545+
private final int _radix;
543546

544547
// lazily constructed when created from annotations
545548
private transient TimeZone _timezone;
@@ -557,7 +560,7 @@ public Value(JsonFormat ann) {
557560
* @since 2.21
558561
*/
559562
public Value(String p, Shape sh, String localeStr, String tzStr, Features f,
560-
Boolean lenient, byte radix)
563+
Boolean lenient, int radix)
561564
{
562565
this(p, sh,
563566
(localeStr == null || localeStr.length() == 0 || DEFAULT_LOCALE.equals(localeStr)) ?
@@ -586,7 +589,7 @@ public Value(String p, Shape sh, String localeStr, String tzStr, Features f,
586589
* @since 2.21
587590
*/
588591
public Value(String p, Shape sh, Locale l, TimeZone tz, Features f,
589-
Boolean lenient, byte radix)
592+
Boolean lenient, int radix)
590593
{
591594
_pattern = (p == null) ? "" : p;
592595
_shape = (sh == null) ? Shape.ANY : sh;
@@ -619,7 +622,7 @@ public Value(String p, Shape sh, Locale l, TimeZone tz, Features f,
619622
* @since 2.21
620623
*/
621624
public Value(String p, Shape sh, Locale l, String tzStr, TimeZone tz, Features f,
622-
Boolean lenient, byte radix)
625+
Boolean lenient, int radix)
623626
{
624627
_pattern = (p == null) ? "" : p;
625628
_shape = (sh == null) ? Shape.ANY : sh;
@@ -718,6 +721,10 @@ public final Value withOverrides(Value overrides) {
718721
if (lenient == null) {
719722
lenient = _lenient;
720723
}
724+
int radix = overrides._radix;
725+
if(radix == DEFAULT_RADIX) {
726+
radix = _radix;
727+
}
721728

722729
// timezone not merged, just choose one
723730
String tzStr = overrides._timezoneStr;
@@ -729,7 +736,7 @@ public final Value withOverrides(Value overrides) {
729736
} else {
730737
tz = overrides._timezone;
731738
}
732-
return new Value(p, sh, l, tzStr, tz, f, lenient, overrides._radix);
739+
return new Value(p, sh, l, tzStr, tz, f, lenient, radix);
733740
}
734741

735742
/**
@@ -860,9 +867,11 @@ public Boolean getLenient() {
860867
}
861868

862869
/**
870+
* @return radix to use for serializing subclasses of {@link Number} as strings.
871+
* If set to -1, a custom radix has not been specified.
863872
* @since 2.21
864873
*/
865-
public byte getRadix() { return _radix; }
874+
public int getRadix() { return _radix; }
866875

867876
/**
868877
* Convenience method equivalent to
@@ -940,12 +949,12 @@ public boolean hasLenient() {
940949
}
941950

942951
/**
943-
* Accessor for checking whether non-default (non-10) radix has been specified.
952+
* Accessor for checking whether non-default (neither special default marker -1 nor 10) radix has been specified.
944953
*
945954
* @since 2.21
946955
*/
947956
public boolean hasNonDefaultRadix() {
948-
return _radix != DEFAULT_RADIX;
957+
return _radix != DEFAULT_RADIX && _radix != 10;
949958
}
950959

951960
/**

src/test/java/com/fasterxml/jackson/annotation/JsonFormatTest.java

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -148,14 +148,6 @@ public void testSimpleMerge()
148148
assertFalse(merged.hasLocale());
149149
assertEquals(TEST_SHAPE, merged.getShape());
150150
assertFalse(merged.hasTimeZone());
151-
152-
//radix always overrides
153-
byte binaryRadix = 2;
154-
final JsonFormat.Value v3 = JsonFormat.Value.forRadix(binaryRadix);
155-
merged = EMPTY.withOverrides(v3);
156-
assertEquals(DEFAULT_RADIX, EMPTY.getRadix());
157-
assertEquals(binaryRadix, merged.getRadix());
158-
159151
}
160152

161153
@Test
@@ -270,4 +262,26 @@ public void testFeatures() {
270262
assertEquals(Boolean.FALSE, f4.get(Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY));
271263
assertEquals(Boolean.TRUE, f4.get(Feature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS));
272264
}
265+
266+
@Test
267+
void testRadix() {
268+
//Non-Default radix overrides the default
269+
byte binaryRadix = 2;
270+
final JsonFormat.Value v = JsonFormat.Value.forRadix(binaryRadix);
271+
JsonFormat.Value merged = EMPTY.withOverrides(v);
272+
assertEquals(DEFAULT_RADIX, EMPTY.getRadix());
273+
assertEquals(binaryRadix, merged.getRadix());
274+
275+
//Default does not override
276+
final JsonFormat.Value v2 = JsonFormat.Value.forRadix(binaryRadix);
277+
merged = v2.withOverrides(EMPTY);
278+
assertEquals(binaryRadix, v2.getRadix());
279+
assertEquals(binaryRadix, merged.getRadix());
280+
281+
JsonFormat.Value emptyWithBinaryRadix = EMPTY.withRadix(binaryRadix);
282+
assertEquals(binaryRadix, emptyWithBinaryRadix.getRadix());
283+
284+
JsonFormat.Value forBinaryRadix = JsonFormat.Value.forRadix(binaryRadix);
285+
assertEquals(binaryRadix, forBinaryRadix.getRadix());
286+
}
273287
}

0 commit comments

Comments
 (0)