Skip to content

Change CBOR Features defaults for 3.0 #591

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

Merged
merged 8 commits into from
May 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -179,12 +179,10 @@ public boolean canUseSchema(FormatSchema schema) {
return false; // no (mandatory) FormatSchema for cbor
}

// No Reader features yet for CBOR
/*@Override
@Override
public Class<CBORReadFeature> getFormatReadFeatureType() {
return CBORReadFeature.class;
}
*/

@Override
public Class<CBORWriteFeature> getFormatWriteFeatureType() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@ public enum CBORReadFeature implements FormatFeature
* or the legacy Jackson encoding logic (encoding up to Jackson 2.19).
* When enabled, ensures proper encoding of negative values
* (e.g., {@code [0xC3, 0x41, 0x00]} is decoded as -1)
* When disabled, maintains backwards compatibility with existing implementations
* When disabled, maintains compatible behavior to versions prior to 3.0.
* (e.g., {@code [0xC3, 0x41, 0x00]} is decoded as 0).
*<p>
* Note that there is the counterpart
* {@link CBORWriteFeature#ENCODE_USING_STANDARD_NEGATIVE_BIGINT_ENCODING}
* for encoding.
*<p>
* The default value is {@code false} for backwards compatibility.
* The default value is {@code true} in Jackson 3.x (was {@code false} in Jackson 2.x).
*/
DECODE_USING_STANDARD_NEGATIVE_BIGINT_ENCODING(false),
DECODE_USING_STANDARD_NEGATIVE_BIGINT_ENCODING(true),

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

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

private final boolean _defaultState;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,16 @@ public enum CBORWriteFeature implements FormatFeature
* or using legacy Jackson encoding logic (encoding up to Jackson 2.19).
* When enabled, uses CBOR standard specified encoding of negative values
* (e.g., -1 is encoded {@code [0xC3, 0x41, 0x00]}).
* When disabled, maintains backwards compatibility with existing implementations
* When disabled, maintains behavior of versions prior to 3.0.
* (e.g., -1 is encoded {@code [0xC3, 0x41, 0x01]}) and uses legacy Jackson encoding.
*<p>
* Note that there is the counterpart
* {@link CBORReadFeature#DECODE_USING_STANDARD_NEGATIVE_BIGINT_ENCODING}
* for encoding.
*<p>
* Default value is {@code false} for backwards-compatibility.
* The default value is {@code true} in Jackson 3.x (was {@code false} in Jackson 2.x).
*/
ENCODE_USING_STANDARD_NEGATIVE_BIGINT_ENCODING(false),
ENCODE_USING_STANDARD_NEGATIVE_BIGINT_ENCODING(true),

;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,18 @@ public class CBORFactoryPropertiesTest extends CBORTestBase

private final static CBORFactory CBOR_F = new CBORFactory();

@Test
public void testFactoryDefaults() {
CBORFactory f = new CBORFactory();

assertEquals(CBORReadFeature.DECODE_USING_STANDARD_NEGATIVE_BIGINT_ENCODING.enabledByDefault(),
f.isEnabled(CBORReadFeature.DECODE_USING_STANDARD_NEGATIVE_BIGINT_ENCODING));
assertEquals(CBORReadFeature.READ_UNDEFINED_AS_EMBEDDED_OBJECT.enabledByDefault(),
f.isEnabled(CBORReadFeature.READ_UNDEFINED_AS_EMBEDDED_OBJECT));
assertEquals(CBORReadFeature.READ_SIMPLE_VALUE_AS_EMBEDDED_OBJECT.enabledByDefault(),
f.isEnabled(CBORReadFeature.READ_SIMPLE_VALUE_AS_EMBEDDED_OBJECT));
}

@Test
public void testCBORFactorySerializable() throws Exception
{
Expand Down Expand Up @@ -78,7 +90,7 @@ public void testVersions() throws Exception
public void testCapabilities() throws Exception
{
assertTrue(CBOR_F.canHandleBinaryNatively());
assertEquals(null, CBOR_F.getFormatReadFeatureType());
assertEquals(CBORReadFeature.class, CBOR_F.getFormatReadFeatureType());
assertEquals(CBORWriteFeature.class, CBOR_F.getFormatWriteFeatureType());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,7 @@ public void testSimpleNegativeBigInteger() throws Exception {
};

// Test correct decoding
CBORMapper mapper1 = CBORMapper.builder()
.enable(CBORReadFeature.DECODE_USING_STANDARD_NEGATIVE_BIGINT_ENCODING)
.build();
CBORMapper mapper1 = CBORMapper.builder().build();
assertEquals(BigInteger.valueOf(-1),
mapper1.readValue(encodedNegativeOne, BigInteger.class));

Expand Down Expand Up @@ -102,9 +100,7 @@ public void testNegativeBigInteger() throws Exception {
};

// Test correct decoding
CBORMapper mapper1 = CBORMapper.builder()
.enable(CBORReadFeature.DECODE_USING_STANDARD_NEGATIVE_BIGINT_ENCODING)
.build();
CBORMapper mapper1 = CBORMapper.builder().build();
assertEquals(new BigInteger("-340282366920938463463374607431768211456"),
mapper1.readValue(encodedNegative, BigInteger.class));

Expand Down Expand Up @@ -143,9 +139,7 @@ public void testNegativeBigIntegerWithoutLeadingZero() throws Exception {
};

// Test correct decoding
CBORMapper mapper1 = CBORMapper.builder()
.enable(CBORReadFeature.DECODE_USING_STANDARD_NEGATIVE_BIGINT_ENCODING)
.build();
CBORMapper mapper1 = CBORMapper.builder().build();
assertEquals(new BigInteger("-340282366920938463463374607431768211456"),
mapper1.readValue(encodedNegative, BigInteger.class));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,14 @@ public class SimpleValuesTest extends CBORTestBase
@Test
public void testTinySimpleValues() throws Exception
{
CBORFactory f = CBORFactory.builder()
.disable(CBORReadFeature.READ_SIMPLE_VALUE_AS_EMBEDDED_OBJECT)
.build();
// Values 0..19 are unassigned, valid to encounter
for (int v = 0; v <= 19; ++v) {
byte[] doc = new byte[1];
doc[0] = (byte) (CBORConstants.PREFIX_TYPE_MISC + v);
try (JsonParser p = cborParser(doc)) {
try (JsonParser p = cborParser(f, doc)) {
assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
// exposes as `int`, fwtw
assertEquals(NumberType.INT, p.getNumberType());
Expand All @@ -33,14 +36,11 @@ public void testTinySimpleValues() throws Exception
@Test
public void testTinySimpleValuesAsEmbeddedObjectWhenEnabled() throws Exception
{
CBORFactory f = CBORFactory.builder()
.enable(CBORReadFeature.READ_SIMPLE_VALUE_AS_EMBEDDED_OBJECT)
.build();
// Values 0..19 are unassigned, valid to encounter
for (int v = 0; v <= 19; ++v) {
byte[] doc = new byte[1];
doc[0] = (byte) (CBORConstants.PREFIX_TYPE_MISC + v);
try (CBORParser p = cborParser(f, doc)) {
try (CBORParser p = cborParser(doc)) {
assertToken(JsonToken.VALUE_EMBEDDED_OBJECT, p.nextToken());
assertEquals(new CBORSimpleValue(v), p.getEmbeddedObject());
}
Expand All @@ -49,10 +49,13 @@ public void testTinySimpleValuesAsEmbeddedObjectWhenEnabled() throws Exception

@Test
public void testValidByteLengthMinimalValues() throws Exception {
CBORFactory f = CBORFactory.builder()
.disable(CBORReadFeature.READ_SIMPLE_VALUE_AS_EMBEDDED_OBJECT)
.build();
// Values 32..255 are unassigned, valid to encounter
for (int v = 32; v <= 255; ++v) {
byte[] doc = { (byte) (CBORConstants.PREFIX_TYPE_MISC + 24), (byte) v };
try (JsonParser p = cborParser(doc)) {
try (JsonParser p = cborParser(f, doc)) {
assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
// exposes as `int`, fwtw
assertEquals(NumberType.INT, p.getNumberType());
Expand All @@ -64,12 +67,9 @@ public void testValidByteLengthMinimalValues() throws Exception {
@Test
public void testValidByteLengthMinimalValuesAsEmbeddedObjectWhenEnabled() throws Exception {
// Values 32..255 are unassigned, valid to encounter
CBORFactory f = CBORFactory.builder()
.enable(CBORReadFeature.READ_SIMPLE_VALUE_AS_EMBEDDED_OBJECT)
.build();
for (int v = 32; v <= 255; ++v) {
byte[] doc = { (byte) (CBORConstants.PREFIX_TYPE_MISC + 24), (byte) v };
try (CBORParser p = cborParser(f, doc)) {
try (CBORParser p = cborParser( doc)) {
assertToken(JsonToken.VALUE_EMBEDDED_OBJECT, p.nextToken());
assertEquals(new CBORSimpleValue(v), p.getEmbeddedObject());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import org.junit.jupiter.api.Test;

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

private final CBORFactory CBOR_F = cborFactory();
private final CBORFactory CBOR_F = CBORFactory.builder()
.disable(CBORReadFeature.READ_UNDEFINED_AS_EMBEDDED_OBJECT)
.build();

@Test
public void testUndefinedLiteralStreaming() throws Exception
{
try (CBORParser p = cborParser(new byte[] { BYTE_UNDEFINED })) {
try (CBORParser p = cborParser(CBOR_F, new byte[] { BYTE_UNDEFINED })) {
assertEquals(JsonToken.VALUE_NULL, p.nextToken());
assertTrue(p.isUndefined());
assertNull(p.nextToken());
Expand All @@ -34,10 +35,7 @@ public void testUndefinedLiteralStreaming() throws Exception
// @since 2.20 [jackson-dataformats-binary/137]
@Test
public void testUndefinedLiteralAsEmbeddedObject() throws Exception {
CBORFactory f = CBORFactory.builder()
.enable(CBORReadFeature.READ_UNDEFINED_AS_EMBEDDED_OBJECT)
.build();
CBORParser p = cborParser(f, new byte[] { BYTE_UNDEFINED });
CBORParser p = cborParser(new byte[] { BYTE_UNDEFINED });

assertEquals(JsonToken.VALUE_EMBEDDED_OBJECT, p.nextToken());
assertTrue(p.isUndefined());
Expand All @@ -64,15 +62,11 @@ public void testUndefinedInArray() throws Exception
// @since 2.20 [jackson-dataformats-binary/137]
@Test
public void testUndefinedInArrayAsEmbeddedObject() throws Exception {
CBORFactory f = CBORFactory.builder()
.enable(CBORReadFeature.READ_UNDEFINED_AS_EMBEDDED_OBJECT)
.build();

ByteArrayOutputStream out = new ByteArrayOutputStream();
out.write(CBORConstants.BYTE_ARRAY_INDEFINITE);
out.write(BYTE_UNDEFINED);
out.write(CBORConstants.BYTE_BREAK);
CBORParser p = cborParser(f, out.toByteArray());
CBORParser p = cborParser(out.toByteArray());
assertEquals(JsonToken.START_ARRAY, p.nextToken());
assertEquals(JsonToken.VALUE_EMBEDDED_OBJECT, p.nextToken());
assertTrue(p.isUndefined());
Expand Down Expand Up @@ -110,10 +104,6 @@ public void testUndefinedInObject() throws Exception
// @since 2.20 [jackson-dataformats-binary/137]
@Test
public void testUndefinedInObjectAsEmbeddedObject() throws Exception {
CBORFactory f = CBORFactory.builder()
.enable(CBORReadFeature.READ_UNDEFINED_AS_EMBEDDED_OBJECT)
.build();

ByteArrayOutputStream out = new ByteArrayOutputStream();
CBORGenerator g = cborGenerator(out);
g.writeStartObject();
Expand All @@ -126,7 +116,7 @@ public void testUndefinedInObjectAsEmbeddedObject() throws Exception {
// assume we use end marker for Object, so
doc[doc.length - 2] = BYTE_UNDEFINED;

try (CBORParser p = cborParser(f, doc)) {
try (CBORParser p = cborParser(doc)) {
assertEquals(JsonToken.START_OBJECT, p.nextToken());
assertEquals(JsonToken.PROPERTY_NAME, p.nextToken());
assertEquals("bar", p.currentName());
Expand Down
12 changes: 12 additions & 0 deletions release-notes/CREDITS
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,15 @@ binary data formats module, version 3.x

Tatu Saloranta, [email protected]: author

--------------------------------------------------------------------------------
Credits for individual projects, since 3.0.0
--------------------------------------------------------------------------------

Fawzi Essam (@iifawzi)

* Contributed #582: Change defaults for `CBORReadFeature.DECODE_USING_STANDARD_NEGATIVE_BIGINT_ENCODING`
and `CBORWriteFeature.ENCODE_USING_STANDARD_NEGATIVE_BIGINT_ENCODING` to `true` (enabled)
(3.0.0)
* Contribited #591: Change `CBOR` Features defaults for 3.0
(3.0.0)

12 changes: 7 additions & 5 deletions release-notes/VERSION
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,16 @@ implementations)
=== Releases ===
------------------------------------------------------------------------

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

No changes since rc3
#582: Change defaults for `CBORReadFeature.DECODE_USING_STANDARD_NEGATIVE_BIGINT_ENCODING`
and `CBORWriteFeature.ENCODE_USING_STANDARD_NEGATIVE_BIGINT_ENCODING` to `true` (enabled)
(contributed by by Fawzi E)
#591: Change `CBOR` Features defaults for 3.0
(contributed by by Fawzi E)

3.0.0-rc4 (10-May-2025)
3.0.0-rc3 (13-Apr-2025)

- Branch rename "master" -> "3.x" [JSTEP-12]

3.0.0-rc2 (28-Mar-2025)

No changes since rc1.
Expand Down