Skip to content

Commit 0517368

Browse files
authored
Fix #619: add mapper.isEnabled(FormatRead/WriteFeature) methods (#621)
1 parent 9135f53 commit 0517368

File tree

12 files changed

+137
-18
lines changed

12 files changed

+137
-18
lines changed

avro/src/main/java/tools/jackson/dataformat/avro/AvroMapper.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,20 @@ public AvroFactory tokenStreamFactory() {
205205
return (AvroFactory) _streamFactory;
206206
}
207207

208+
/*
209+
/**********************************************************************
210+
/* Format-specific
211+
/**********************************************************************
212+
*/
213+
214+
public boolean isEnabled(AvroReadFeature f) {
215+
return _deserializationConfig.hasFormatFeature(f);
216+
}
217+
218+
public boolean isEnabled(AvroWriteFeature f) {
219+
return _serializationConfig.hasFormatFeature(f);
220+
}
221+
208222
/*
209223
/**********************************************************************
210224
/* Schema introspection

avro/src/test/java/tools/jackson/dataformat/avro/MapperConfigTest.java

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import java.io.ByteArrayOutputStream;
44

5+
import org.junit.jupiter.api.Test;
6+
57
import tools.jackson.core.FormatSchema;
68
import tools.jackson.core.StreamReadCapability;
79
import tools.jackson.core.StreamWriteFeature;
@@ -28,6 +30,7 @@ public String getSchemaType() {
2830
/**********************************************************************
2931
*/
3032

33+
@Test
3134
public void testFactoryDefaults() throws Exception
3235
{
3336
assertTrue(MAPPER.tokenStreamFactory().isEnabled(AvroReadFeature.AVRO_BUFFERING));
@@ -38,25 +41,29 @@ public void testFactoryDefaults() throws Exception
3841
assertFalse(MAPPER.tokenStreamFactory().canUseSchema(BOGUS_SCHEMA));
3942
}
4043

44+
@Test
4145
public void testParserDefaults() throws Exception
4246
{
43-
AvroParser p = (AvroParser) MAPPER.createParser(new byte[0]);
44-
assertTrue(p.isEnabled(AvroReadFeature.AVRO_BUFFERING));
45-
p.close();
47+
try (AvroParser p = (AvroParser) MAPPER.createParser(new byte[0])) {
48+
assertTrue(p.isEnabled(AvroReadFeature.AVRO_BUFFERING));
49+
}
4650

4751
AvroMapper mapper = AvroMapper.builder()
4852
.disable(AvroReadFeature.AVRO_BUFFERING)
4953
.build();
50-
p = (AvroParser) mapper.createParser(new byte[0]);
51-
assertFalse(p.isEnabled(AvroReadFeature.AVRO_BUFFERING));
52-
53-
// 15-Jan-2021, tatu: 2.14 added this setting, not enabled in
54-
// default set
55-
assertTrue(p.streamReadCapabilities().isEnabled(StreamReadCapability.EXACT_FLOATS));
54+
try (AvroParser p = (AvroParser) mapper.createParser(new byte[0])) {
55+
assertFalse(p.isEnabled(AvroReadFeature.AVRO_BUFFERING));
56+
57+
// 15-Jan-2021, tatu: 2.14 added this setting, not enabled in
58+
// default set
59+
assertTrue(p.streamReadCapabilities().isEnabled(StreamReadCapability.EXACT_FLOATS));
60+
}
5661

57-
p.close();
58-
}
62+
// [dataformats-binary#619]
63+
assertTrue(MAPPER.isEnabled(AvroReadFeature.AVRO_BUFFERING));
64+
}
5965

66+
@Test
6067
public void testGeneratorDefaults() throws Exception
6168
{
6269
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
@@ -76,6 +83,9 @@ public void testGeneratorDefaults() throws Exception
7683
.createGenerator(bytes);
7784
assertFalse(g.isEnabled(AvroWriteFeature.AVRO_BUFFERING));
7885
g.close();
86+
87+
// [dataformats-binary#619]
88+
assertFalse(MAPPER.isEnabled(AvroWriteFeature.AVRO_FILE_OUTPUT));
7989
}
8090

8191
/*
@@ -84,6 +94,7 @@ public void testGeneratorDefaults() throws Exception
8494
/**********************************************************************
8595
*/
8696

97+
@Test
8798
public void testDefaultSettingsWithAvroMapper()
8899
{
89100
AvroMapper mapper = new AvroMapper();

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,9 @@ protected Object readResolve() {
110110
}
111111

112112
/*
113-
/**********************************************************
113+
/**********************************************************************
114114
/* Life-cycle
115-
/**********************************************************
115+
/**********************************************************************
116116
*/
117117

118118
public CBORMapper() {
@@ -173,6 +173,20 @@ public CBORFactory tokenStreamFactory() {
173173
return (CBORFactory) _streamFactory;
174174
}
175175

176+
/*
177+
/**********************************************************************
178+
/* Format-specific
179+
/**********************************************************************
180+
*/
181+
182+
public boolean isEnabled(CBORReadFeature f) {
183+
return _deserializationConfig.hasFormatFeature(f);
184+
}
185+
186+
public boolean isEnabled(CBORWriteFeature f) {
187+
return _serializationConfig.hasFormatFeature(f);
188+
}
189+
176190
/*
177191
/**********************************************************
178192
/* Helper class(es)

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ public enum CBORWriteFeature implements FormatFeature
1515
* representation that is enough to retain value; if false, will use
1616
* length indicated by argument type (4-byte for <code>int</code>,
1717
* 8-byte for <code>long</code> and so on).
18+
*<p>
19+
* Default value is {@code true} meaning that minimal representation is used
20+
* when encoding.
1821
*/
1922
WRITE_MINIMAL_INTS(true),
2023

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,4 +151,12 @@ public void testNegativeBigIntegerWithoutLeadingZero() throws Exception {
151151
assertEquals(BigInteger.ONE,
152152
mapper2.readValue(encodedNegative, BigInteger.class));
153153
}
154+
155+
// [dataformats-binary#619]
156+
@Test
157+
void testFormatFeatureDefaults() {
158+
CBORMapper mapper = CBORMapper.shared();
159+
assertTrue(mapper.isEnabled(CBORReadFeature.DECODE_USING_STANDARD_NEGATIVE_BIGINT_ENCODING));
160+
assertTrue(mapper.isEnabled(CBORWriteFeature.WRITE_MINIMAL_INTS));
161+
}
154162
}

ion/src/main/java/tools/jackson/dataformat/ion/IonObjectMapper.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import tools.jackson.databind.deser.DeserializationContextExt;
2828
import tools.jackson.databind.module.SimpleModule;
2929
import tools.jackson.databind.ser.SerializationContextExt;
30+
3031
import tools.jackson.dataformat.ion.ionvalue.IonValueModule;
3132

3233
import com.amazon.ion.IonDatagram;
@@ -276,6 +277,21 @@ public IonFactory tokenStreamFactory() {
276277
return (IonFactory) _streamFactory;
277278
}
278279

280+
/*
281+
/**********************************************************************
282+
/* Format-specific
283+
/**********************************************************************
284+
*/
285+
286+
public boolean isEnabled(IonReadFeature f) {
287+
return _deserializationConfig.hasFormatFeature(f);
288+
}
289+
290+
public boolean isEnabled(IonWriteFeature f) {
291+
return _serializationConfig.hasFormatFeature(f);
292+
}
293+
294+
279295
/*
280296
/************************************************************************
281297
/* Additional convenience factory methods for parsers, generators
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package tools.jackson.dataformat.ion.misc;
2+
3+
import static org.junit.jupiter.api.Assertions.assertTrue;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
import tools.jackson.dataformat.ion.*;
8+
9+
public class FeatureDefaultsTest
10+
{
11+
// [dataformats-binary#619]
12+
@Test
13+
void testFormatFeatureDefaults() {
14+
IonObjectMapper mapper = IonObjectMapper.shared();
15+
assertTrue(mapper.isEnabled(IonReadFeature.USE_NATIVE_TYPE_ID));
16+
assertTrue(mapper.isEnabled(IonWriteFeature.USE_NATIVE_TYPE_ID));
17+
}
18+
}

release-notes/CREDITS

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ Fawzi Essam (@iifawzi)
1515
* Contributed #582: Change defaults for `CBORReadFeature.DECODE_USING_STANDARD_NEGATIVE_BIGINT_ENCODING`
1616
and `CBORWriteFeature.ENCODE_USING_STANDARD_NEGATIVE_BIGINT_ENCODING` to `true` (enabled)
1717
(3.0.0)
18-
* Contribited #591: Change `CBOR` Features defaults for 3.0
18+
* Contributed #591: Change `CBOR` Features defaults for 3.0
1919
(3.0.0)
2020

21+
Andy Wilkinson (@wilkinsona)
22+
23+
* Requested #619: (avro, cbor, ion, smile) Add `isEnabled()` methods for format-specific features
24+
(like `CBORReadFeature` and `CBORWriteFeature`) to mappers
25+
(3.1.0)

release-notes/VERSION

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ implementations)
1414
=== Releases ===
1515
------------------------------------------------------------------------
1616

17+
3.1.0 (not yet released)
18+
19+
#619: (avro, cbor, ion, smile) Add `isEnabled()` methods for format-specific features
20+
(like `CBORReadFeature` and `CBORWriteFeature`) to mappers
21+
(requested by Andy W)
22+
1723
3.0.1 (21-Oct-2025)
1824

1925
No changes since 3.0.0

smile/src/main/java/tools/jackson/dataformat/smile/SmileMapper.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,20 @@ public SmileFactory tokenStreamFactory() {
173173
return (SmileFactory) _streamFactory;
174174
}
175175

176+
/*
177+
/**********************************************************************
178+
/* Format-specific
179+
/**********************************************************************
180+
*/
181+
182+
public boolean isEnabled(SmileReadFeature f) {
183+
return _deserializationConfig.hasFormatFeature(f);
184+
}
185+
186+
public boolean isEnabled(SmileWriteFeature f) {
187+
return _serializationConfig.hasFormatFeature(f);
188+
}
189+
176190
/*
177191
/**********************************************************************
178192
/* Helper class(es)

0 commit comments

Comments
 (0)