Skip to content

Commit 77818a2

Browse files
authored
Change CBOR Features defaults for 3.0 (#591)
1 parent bbe4298 commit 77818a2

File tree

9 files changed

+65
-57
lines changed

9 files changed

+65
-57
lines changed

cbor/src/main/java/tools/jackson/dataformat/cbor/CBORFactory.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -179,12 +179,10 @@ public boolean canUseSchema(FormatSchema schema) {
179179
return false; // no (mandatory) FormatSchema for cbor
180180
}
181181

182-
// No Reader features yet for CBOR
183-
/*@Override
182+
@Override
184183
public Class<CBORReadFeature> getFormatReadFeatureType() {
185184
return CBORReadFeature.class;
186185
}
187-
*/
188186

189187
@Override
190188
public Class<CBORWriteFeature> getFormatWriteFeatureType() {

cbor/src/main/java/tools/jackson/dataformat/cbor/CBORReadFeature.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,16 @@ public enum CBORReadFeature implements FormatFeature
1616
* or the legacy Jackson encoding logic (encoding up to Jackson 2.19).
1717
* When enabled, ensures proper encoding of negative values
1818
* (e.g., {@code [0xC3, 0x41, 0x00]} is decoded as -1)
19-
* When disabled, maintains backwards compatibility with existing implementations
19+
* When disabled, maintains compatible behavior to versions prior to 3.0.
2020
* (e.g., {@code [0xC3, 0x41, 0x00]} is decoded as 0).
2121
*<p>
2222
* Note that there is the counterpart
2323
* {@link CBORWriteFeature#ENCODE_USING_STANDARD_NEGATIVE_BIGINT_ENCODING}
2424
* for encoding.
2525
*<p>
26-
* The default value is {@code false} for backwards compatibility.
26+
* The default value is {@code true} in Jackson 3.x (was {@code false} in Jackson 2.x).
2727
*/
28-
DECODE_USING_STANDARD_NEGATIVE_BIGINT_ENCODING(false),
28+
DECODE_USING_STANDARD_NEGATIVE_BIGINT_ENCODING(true),
2929

3030
/**
3131
* Feature that determines how an {@code undefined} value ({@code 0xF7}) is exposed
@@ -34,23 +34,23 @@ public enum CBORReadFeature implements FormatFeature
3434
* When enabled, the parser returns {@link JsonToken#VALUE_EMBEDDED_OBJECT} with
3535
* a value of {@code null}, allowing the caller to distinguish {@code undefined} from actual
3636
* {@link JsonToken#VALUE_NULL}.
37-
* When disabled {@code undefined} value is reported as {@link JsonToken#VALUE_NULL}.
37+
* When disabled, {@code undefined} value is reported as simple {@link JsonToken#VALUE_NULL}.
3838
*<p>
39-
* The default value is {@code false} for backwards compatibility (with versions prior to 2.20).
39+
* The default value is {@code true} in Jackson 3.x (was {@code false} in Jackson 2.x).
4040
*/
41-
READ_UNDEFINED_AS_EMBEDDED_OBJECT(false),
41+
READ_UNDEFINED_AS_EMBEDDED_OBJECT(true),
4242

4343
/**
4444
* Feature that determines how a CBOR "simple value" of major type 7 is exposed by parser.
4545
* <p>
4646
* When enabled, the parser returns {@link JsonToken#VALUE_EMBEDDED_OBJECT} with
4747
* an embedded value of type {@link CBORSimpleValue}, allowing the caller to distinguish
4848
* these values from actual {@link JsonToken#VALUE_NUMBER_INT}s.
49-
* When disabled, simple values are returned as {@link JsonToken#VALUE_NUMBER_INT}.
49+
* When disabled, simple values are returned as {@link JsonToken#VALUE_NUMBER_INT}s.
5050
*<p>
51-
* The default value is {@code false} for backwards compatibility (with versions prior to 2.20).
51+
* The default value is {@code true} in Jackson 3.x (was {@code false} in Jackson 2.x).
5252
*/
53-
READ_SIMPLE_VALUE_AS_EMBEDDED_OBJECT(false)
53+
READ_SIMPLE_VALUE_AS_EMBEDDED_OBJECT(true)
5454
;
5555

5656
private final boolean _defaultState;

cbor/src/main/java/tools/jackson/dataformat/cbor/CBORWriteFeature.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,16 +68,16 @@ public enum CBORWriteFeature implements FormatFeature
6868
* or using legacy Jackson encoding logic (encoding up to Jackson 2.19).
6969
* When enabled, uses CBOR standard specified encoding of negative values
7070
* (e.g., -1 is encoded {@code [0xC3, 0x41, 0x00]}).
71-
* When disabled, maintains backwards compatibility with existing implementations
71+
* When disabled, maintains behavior of versions prior to 3.0.
7272
* (e.g., -1 is encoded {@code [0xC3, 0x41, 0x01]}) and uses legacy Jackson encoding.
7373
*<p>
7474
* Note that there is the counterpart
7575
* {@link CBORReadFeature#DECODE_USING_STANDARD_NEGATIVE_BIGINT_ENCODING}
7676
* for encoding.
7777
*<p>
78-
* Default value is {@code false} for backwards-compatibility.
78+
* The default value is {@code true} in Jackson 3.x (was {@code false} in Jackson 2.x).
7979
*/
80-
ENCODE_USING_STANDARD_NEGATIVE_BIGINT_ENCODING(false),
80+
ENCODE_USING_STANDARD_NEGATIVE_BIGINT_ENCODING(true),
8181

8282
;
8383

cbor/src/test/java/tools/jackson/dataformat/cbor/CBORFactoryPropertiesTest.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,18 @@ public class CBORFactoryPropertiesTest extends CBORTestBase
2020

2121
private final static CBORFactory CBOR_F = new CBORFactory();
2222

23+
@Test
24+
public void testFactoryDefaults() {
25+
CBORFactory f = new CBORFactory();
26+
27+
assertEquals(CBORReadFeature.DECODE_USING_STANDARD_NEGATIVE_BIGINT_ENCODING.enabledByDefault(),
28+
f.isEnabled(CBORReadFeature.DECODE_USING_STANDARD_NEGATIVE_BIGINT_ENCODING));
29+
assertEquals(CBORReadFeature.READ_UNDEFINED_AS_EMBEDDED_OBJECT.enabledByDefault(),
30+
f.isEnabled(CBORReadFeature.READ_UNDEFINED_AS_EMBEDDED_OBJECT));
31+
assertEquals(CBORReadFeature.READ_SIMPLE_VALUE_AS_EMBEDDED_OBJECT.enabledByDefault(),
32+
f.isEnabled(CBORReadFeature.READ_SIMPLE_VALUE_AS_EMBEDDED_OBJECT));
33+
}
34+
2335
@Test
2436
public void testCBORFactorySerializable() throws Exception
2537
{
@@ -78,7 +90,7 @@ public void testVersions() throws Exception
7890
public void testCapabilities() throws Exception
7991
{
8092
assertTrue(CBOR_F.canHandleBinaryNatively());
81-
assertEquals(null, CBOR_F.getFormatReadFeatureType());
93+
assertEquals(CBORReadFeature.class, CBOR_F.getFormatReadFeatureType());
8294
assertEquals(CBORWriteFeature.class, CBOR_F.getFormatWriteFeatureType());
8395
}
8496

cbor/src/test/java/tools/jackson/dataformat/cbor/mapper/CBORMapperTest.java

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,7 @@ public void testSimpleNegativeBigInteger() throws Exception {
6060
};
6161

6262
// Test correct decoding
63-
CBORMapper mapper1 = CBORMapper.builder()
64-
.enable(CBORReadFeature.DECODE_USING_STANDARD_NEGATIVE_BIGINT_ENCODING)
65-
.build();
63+
CBORMapper mapper1 = CBORMapper.builder().build();
6664
assertEquals(BigInteger.valueOf(-1),
6765
mapper1.readValue(encodedNegativeOne, BigInteger.class));
6866

@@ -102,9 +100,7 @@ public void testNegativeBigInteger() throws Exception {
102100
};
103101

104102
// Test correct decoding
105-
CBORMapper mapper1 = CBORMapper.builder()
106-
.enable(CBORReadFeature.DECODE_USING_STANDARD_NEGATIVE_BIGINT_ENCODING)
107-
.build();
103+
CBORMapper mapper1 = CBORMapper.builder().build();
108104
assertEquals(new BigInteger("-340282366920938463463374607431768211456"),
109105
mapper1.readValue(encodedNegative, BigInteger.class));
110106

@@ -143,9 +139,7 @@ public void testNegativeBigIntegerWithoutLeadingZero() throws Exception {
143139
};
144140

145141
// Test correct decoding
146-
CBORMapper mapper1 = CBORMapper.builder()
147-
.enable(CBORReadFeature.DECODE_USING_STANDARD_NEGATIVE_BIGINT_ENCODING)
148-
.build();
142+
CBORMapper mapper1 = CBORMapper.builder().build();
149143
assertEquals(new BigInteger("-340282366920938463463374607431768211456"),
150144
mapper1.readValue(encodedNegative, BigInteger.class));
151145

cbor/src/test/java/tools/jackson/dataformat/cbor/parse/SimpleValuesTest.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,14 @@ public class SimpleValuesTest extends CBORTestBase
1717
@Test
1818
public void testTinySimpleValues() throws Exception
1919
{
20+
CBORFactory f = CBORFactory.builder()
21+
.disable(CBORReadFeature.READ_SIMPLE_VALUE_AS_EMBEDDED_OBJECT)
22+
.build();
2023
// Values 0..19 are unassigned, valid to encounter
2124
for (int v = 0; v <= 19; ++v) {
2225
byte[] doc = new byte[1];
2326
doc[0] = (byte) (CBORConstants.PREFIX_TYPE_MISC + v);
24-
try (JsonParser p = cborParser(doc)) {
27+
try (JsonParser p = cborParser(f, doc)) {
2528
assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
2629
// exposes as `int`, fwtw
2730
assertEquals(NumberType.INT, p.getNumberType());
@@ -33,14 +36,11 @@ public void testTinySimpleValues() throws Exception
3336
@Test
3437
public void testTinySimpleValuesAsEmbeddedObjectWhenEnabled() throws Exception
3538
{
36-
CBORFactory f = CBORFactory.builder()
37-
.enable(CBORReadFeature.READ_SIMPLE_VALUE_AS_EMBEDDED_OBJECT)
38-
.build();
3939
// Values 0..19 are unassigned, valid to encounter
4040
for (int v = 0; v <= 19; ++v) {
4141
byte[] doc = new byte[1];
4242
doc[0] = (byte) (CBORConstants.PREFIX_TYPE_MISC + v);
43-
try (CBORParser p = cborParser(f, doc)) {
43+
try (CBORParser p = cborParser(doc)) {
4444
assertToken(JsonToken.VALUE_EMBEDDED_OBJECT, p.nextToken());
4545
assertEquals(new CBORSimpleValue(v), p.getEmbeddedObject());
4646
}
@@ -49,10 +49,13 @@ public void testTinySimpleValuesAsEmbeddedObjectWhenEnabled() throws Exception
4949

5050
@Test
5151
public void testValidByteLengthMinimalValues() throws Exception {
52+
CBORFactory f = CBORFactory.builder()
53+
.disable(CBORReadFeature.READ_SIMPLE_VALUE_AS_EMBEDDED_OBJECT)
54+
.build();
5255
// Values 32..255 are unassigned, valid to encounter
5356
for (int v = 32; v <= 255; ++v) {
5457
byte[] doc = { (byte) (CBORConstants.PREFIX_TYPE_MISC + 24), (byte) v };
55-
try (JsonParser p = cborParser(doc)) {
58+
try (JsonParser p = cborParser(f, doc)) {
5659
assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
5760
// exposes as `int`, fwtw
5861
assertEquals(NumberType.INT, p.getNumberType());
@@ -64,12 +67,9 @@ public void testValidByteLengthMinimalValues() throws Exception {
6467
@Test
6568
public void testValidByteLengthMinimalValuesAsEmbeddedObjectWhenEnabled() throws Exception {
6669
// Values 32..255 are unassigned, valid to encounter
67-
CBORFactory f = CBORFactory.builder()
68-
.enable(CBORReadFeature.READ_SIMPLE_VALUE_AS_EMBEDDED_OBJECT)
69-
.build();
7070
for (int v = 32; v <= 255; ++v) {
7171
byte[] doc = { (byte) (CBORConstants.PREFIX_TYPE_MISC + 24), (byte) v };
72-
try (CBORParser p = cborParser(f, doc)) {
72+
try (CBORParser p = cborParser( doc)) {
7373
assertToken(JsonToken.VALUE_EMBEDDED_OBJECT, p.nextToken());
7474
assertEquals(new CBORSimpleValue(v), p.getEmbeddedObject());
7575
}

cbor/src/test/java/tools/jackson/dataformat/cbor/parse/UndefinedValueTest.java

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
import org.junit.jupiter.api.Test;
66

7-
import tools.jackson.core.JsonParser;
87
import tools.jackson.core.JsonToken;
98
import tools.jackson.dataformat.cbor.*;
109
import tools.jackson.dataformat.cbor.CBORGenerator;
@@ -19,12 +18,14 @@ public class UndefinedValueTest extends CBORTestBase
1918
{
2019
private final static byte BYTE_UNDEFINED = (byte) CBORConstants.SIMPLE_VALUE_UNDEFINED;
2120

22-
private final CBORFactory CBOR_F = cborFactory();
21+
private final CBORFactory CBOR_F = CBORFactory.builder()
22+
.disable(CBORReadFeature.READ_UNDEFINED_AS_EMBEDDED_OBJECT)
23+
.build();
2324

2425
@Test
2526
public void testUndefinedLiteralStreaming() throws Exception
2627
{
27-
try (CBORParser p = cborParser(new byte[] { BYTE_UNDEFINED })) {
28+
try (CBORParser p = cborParser(CBOR_F, new byte[] { BYTE_UNDEFINED })) {
2829
assertEquals(JsonToken.VALUE_NULL, p.nextToken());
2930
assertTrue(p.isUndefined());
3031
assertNull(p.nextToken());
@@ -34,10 +35,7 @@ public void testUndefinedLiteralStreaming() throws Exception
3435
// @since 2.20 [jackson-dataformats-binary/137]
3536
@Test
3637
public void testUndefinedLiteralAsEmbeddedObject() throws Exception {
37-
CBORFactory f = CBORFactory.builder()
38-
.enable(CBORReadFeature.READ_UNDEFINED_AS_EMBEDDED_OBJECT)
39-
.build();
40-
CBORParser p = cborParser(f, new byte[] { BYTE_UNDEFINED });
38+
CBORParser p = cborParser(new byte[] { BYTE_UNDEFINED });
4139

4240
assertEquals(JsonToken.VALUE_EMBEDDED_OBJECT, p.nextToken());
4341
assertTrue(p.isUndefined());
@@ -64,15 +62,11 @@ public void testUndefinedInArray() throws Exception
6462
// @since 2.20 [jackson-dataformats-binary/137]
6563
@Test
6664
public void testUndefinedInArrayAsEmbeddedObject() throws Exception {
67-
CBORFactory f = CBORFactory.builder()
68-
.enable(CBORReadFeature.READ_UNDEFINED_AS_EMBEDDED_OBJECT)
69-
.build();
70-
7165
ByteArrayOutputStream out = new ByteArrayOutputStream();
7266
out.write(CBORConstants.BYTE_ARRAY_INDEFINITE);
7367
out.write(BYTE_UNDEFINED);
7468
out.write(CBORConstants.BYTE_BREAK);
75-
CBORParser p = cborParser(f, out.toByteArray());
69+
CBORParser p = cborParser(out.toByteArray());
7670
assertEquals(JsonToken.START_ARRAY, p.nextToken());
7771
assertEquals(JsonToken.VALUE_EMBEDDED_OBJECT, p.nextToken());
7872
assertTrue(p.isUndefined());
@@ -110,10 +104,6 @@ public void testUndefinedInObject() throws Exception
110104
// @since 2.20 [jackson-dataformats-binary/137]
111105
@Test
112106
public void testUndefinedInObjectAsEmbeddedObject() throws Exception {
113-
CBORFactory f = CBORFactory.builder()
114-
.enable(CBORReadFeature.READ_UNDEFINED_AS_EMBEDDED_OBJECT)
115-
.build();
116-
117107
ByteArrayOutputStream out = new ByteArrayOutputStream();
118108
CBORGenerator g = cborGenerator(out);
119109
g.writeStartObject();
@@ -126,7 +116,7 @@ public void testUndefinedInObjectAsEmbeddedObject() throws Exception {
126116
// assume we use end marker for Object, so
127117
doc[doc.length - 2] = BYTE_UNDEFINED;
128118

129-
try (CBORParser p = cborParser(f, doc)) {
119+
try (CBORParser p = cborParser(doc)) {
130120
assertEquals(JsonToken.START_OBJECT, p.nextToken());
131121
assertEquals(JsonToken.PROPERTY_NAME, p.nextToken());
132122
assertEquals("bar", p.currentName());

release-notes/CREDITS

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,15 @@ binary data formats module, version 3.x
66

77
Tatu Saloranta, [email protected]: author
88

9+
--------------------------------------------------------------------------------
10+
Credits for individual projects, since 3.0.0
11+
--------------------------------------------------------------------------------
12+
13+
Fawzi Essam (@iifawzi)
14+
15+
* Contributed #582: Change defaults for `CBORReadFeature.DECODE_USING_STANDARD_NEGATIVE_BIGINT_ENCODING`
16+
and `CBORWriteFeature.ENCODE_USING_STANDARD_NEGATIVE_BIGINT_ENCODING` to `true` (enabled)
17+
(3.0.0)
18+
* Contribited #591: Change `CBOR` Features defaults for 3.0
19+
(3.0.0)
20+

release-notes/VERSION

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,16 @@ implementations)
1414
=== Releases ===
1515
------------------------------------------------------------------------
1616

17-
3.0.0-rc4 (10-May-2025)
17+
3.0.0-rc5 (not yet released)
1818

19-
No changes since rc3
19+
#582: Change defaults for `CBORReadFeature.DECODE_USING_STANDARD_NEGATIVE_BIGINT_ENCODING`
20+
and `CBORWriteFeature.ENCODE_USING_STANDARD_NEGATIVE_BIGINT_ENCODING` to `true` (enabled)
21+
(contributed by by Fawzi E)
22+
#591: Change `CBOR` Features defaults for 3.0
23+
(contributed by by Fawzi E)
2024

25+
3.0.0-rc4 (10-May-2025)
2126
3.0.0-rc3 (13-Apr-2025)
22-
23-
- Branch rename "master" -> "3.x" [JSTEP-12]
24-
2527
3.0.0-rc2 (28-Mar-2025)
2628

2729
No changes since rc1.

0 commit comments

Comments
 (0)