Skip to content

Commit eb0b61b

Browse files
mcliedtkeliedtkem
and
liedtkem
authored
Add convenience methods to for constructing IonObjectMapper.Builder (#249)
Co-authored-by: liedtkem <[email protected]>
1 parent 93f1627 commit eb0b61b

File tree

6 files changed

+125
-33
lines changed

6 files changed

+125
-33
lines changed

ion/src/main/java/com/fasterxml/jackson/dataformat/ion/IonObjectMapper.java

Lines changed: 105 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,10 @@
2626

2727
import com.amazon.ion.IonDatagram;
2828
import com.amazon.ion.IonReader;
29+
import com.amazon.ion.IonSystem;
2930
import com.amazon.ion.IonValue;
3031
import com.amazon.ion.IonWriter;
32+
import com.amazon.ion.system.IonSystemBuilder;
3133

3234
/**
3335
* Specialization of {@link ObjectMapper} that will set underlying
@@ -46,8 +48,57 @@ public class IonObjectMapper extends ObjectMapper
4648
*/
4749
public static class Builder extends MapperBuilder<IonObjectMapper, Builder>
4850
{
51+
protected final IonFactory _streamFactory;
52+
4953
public Builder(IonObjectMapper m) {
5054
super(m);
55+
_streamFactory = m.getFactory();
56+
}
57+
58+
public Builder enable(IonParser.Feature... features) {
59+
for (IonParser.Feature feature : features) {
60+
_streamFactory.enable(feature);
61+
}
62+
return this;
63+
}
64+
65+
public Builder disable(IonParser.Feature... features) {
66+
for (IonParser.Feature feature : features) {
67+
_streamFactory.disable(feature);
68+
}
69+
return this;
70+
}
71+
72+
public Builder configure(IonParser.Feature feature, boolean state) {
73+
if (state) {
74+
_streamFactory.enable(feature);
75+
} else {
76+
_streamFactory.disable(feature);
77+
}
78+
return this;
79+
}
80+
81+
public Builder enable(IonGenerator.Feature... features) {
82+
for (IonGenerator.Feature feature : features) {
83+
_streamFactory.enable(feature);
84+
}
85+
return this;
86+
}
87+
88+
public Builder disable(IonGenerator.Feature... features) {
89+
for (IonGenerator.Feature feature : features) {
90+
_streamFactory.disable(feature);
91+
}
92+
return this;
93+
}
94+
95+
public Builder configure(IonGenerator.Feature feature, boolean state) {
96+
if (state) {
97+
_streamFactory.enable(feature);
98+
} else {
99+
_streamFactory.disable(feature);
100+
}
101+
return this;
51102
}
52103
}
53104

@@ -57,10 +108,17 @@ public Builder(IonObjectMapper m) {
57108
/**********************************************************************
58109
*/
59110

111+
/**
112+
* Constructor that will construct the mapper with a standard {@link IonFactory}
113+
* as codec, using textual writers by default.
114+
*/
60115
public IonObjectMapper() {
61116
this(new IonFactory());
62117
}
63118

119+
/**
120+
* Constructor that will construct the mapper with a given {@link IonFactory}.
121+
*/
64122
public IonObjectMapper(IonFactory f) {
65123
super(f);
66124
f.setCodec(this);
@@ -77,8 +135,54 @@ protected IonObjectMapper(IonObjectMapper src) {
77135
super(src);
78136
}
79137

138+
/**
139+
* A builder for a mapper that will use textual writers by default. Same as
140+
* {@link #builderForTextualWriters()}.
141+
*/
80142
public static Builder builder() {
81-
return new Builder(new IonObjectMapper());
143+
return builderForTextualWriters();
144+
}
145+
146+
/**
147+
* A builder for a mapper that will use textual writers by default and the
148+
* provided {@link IonSystem}. Same as {@link #builderForTextualWriters(IonSystem)}.
149+
*/
150+
public static Builder builder(IonSystem ionSystem) {
151+
return builderForTextualWriters(ionSystem);
152+
}
153+
154+
/**
155+
* A builder for a mapper that will use binary writers by default.
156+
*/
157+
public static Builder builderForBinaryWriters() {
158+
return builderForBinaryWriters(IonSystemBuilder.standard().build());
159+
}
160+
161+
/**
162+
* A builder for a mapper that will use binary writers by default and the
163+
* provided {@link IonSystem}
164+
*/
165+
public static Builder builderForBinaryWriters(IonSystem ionSystem) {
166+
return builder(IonFactory.builderForBinaryWriters()
167+
.ionSystem(ionSystem)
168+
.build());
169+
}
170+
171+
/**
172+
* A builder for a mapper that will use textual writers by default.
173+
*/
174+
public static Builder builderForTextualWriters() {
175+
return builderForTextualWriters(IonSystemBuilder.standard().build());
176+
}
177+
178+
/**
179+
* A builder for a mapper that will use textual writers by default and the
180+
* provided {@link IonSystem}.
181+
*/
182+
public static Builder builderForTextualWriters(IonSystem ionSystem) {
183+
return builder(IonFactory.builderForTextualWriters()
184+
.ionSystem(ionSystem)
185+
.build());
82186
}
83187

84188
public static Builder builder(IonFactory streamFactory) {

ion/src/test/java/com/fasterxml/jackson/dataformat/ion/DataBindWriteTest.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,7 @@ public void initializeExpectedArray() {
7676
@Test
7777
public void testSimpleObjectWriteText() throws Exception
7878
{
79-
IonObjectMapper m = new IonObjectMapper();
80-
m.setCreateBinaryWriters(false);
79+
IonObjectMapper m = IonObjectMapper.builderForTextualWriters().build();
8180
// now parse it using IonLoader and compare
8281
IonDatagram loadedDatagram = ion.newLoader().load(m.writeValueAsString(new MyBean()));
8382
assertEquals(expectedMyBean, loadedDatagram);
@@ -151,8 +150,7 @@ public void testReusingTextIonWriter() throws Exception
151150

152151
private byte[] _writeAsBytes(Object ob) throws IOException
153152
{
154-
IonObjectMapper m = IonObjectMapper.builder(IonFactory.forBinaryWriters())
155-
.build();
153+
IonObjectMapper m = IonObjectMapper.builderForBinaryWriters().build();
156154
ByteArrayOutputStream out = new ByteArrayOutputStream();
157155
m.writeValue(out, ob);
158156
return out.toByteArray();

ion/src/test/java/com/fasterxml/jackson/dataformat/ion/EnumAsIonSymbolSerializationTest.java

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,12 @@ public void testUsingToString() throws Exception
5555
}
5656

5757
private static IonObjectMapper newMapper(boolean textual, boolean usingToString) {
58-
final IonFactory f = (textual
59-
? IonFactory.builderForTextualWriters()
60-
: IonFactory.builderForBinaryWriters()
61-
)
62-
.ionSystem(ION_SYSTEM)
63-
.build();
64-
final IonObjectMapper mapper = IonObjectMapper.builder(f)
65-
.configure(SerializationFeature.WRITE_ENUMS_USING_TO_STRING, usingToString)
58+
IonObjectMapper.Builder builder = textual
59+
? IonObjectMapper.builderForTextualWriters(ION_SYSTEM)
60+
: IonObjectMapper.builderForBinaryWriters(ION_SYSTEM);
61+
62+
return builder.configure(SerializationFeature.WRITE_ENUMS_USING_TO_STRING, usingToString)
6663
.addModule(new EnumAsIonSymbolModule())
6764
.build();
68-
return mapper;
6965
}
7066
}

ion/src/test/java/com/fasterxml/jackson/dataformat/ion/GenerateSexpTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818
import java.io.IOException;
1919
import java.util.Arrays;
2020

21+
import org.junit.Assert;
22+
import org.junit.Before;
23+
import org.junit.Test;
24+
2125
import com.amazon.ion.IonSystem;
2226
import com.amazon.ion.IonWriter;
2327
import com.amazon.ion.system.IonSystemBuilder;
@@ -26,10 +30,6 @@
2630
import com.fasterxml.jackson.databind.SerializerProvider;
2731
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
2832

29-
import org.junit.Assert;
30-
import org.junit.Before;
31-
import org.junit.Test;
32-
3333
/**
3434
* End to end test verifying we can serialize sexps
3535
*/
@@ -41,7 +41,7 @@ public class GenerateSexpTest {
4141
@Before
4242
public void setup() {
4343
this.ionSystem = IonSystemBuilder.standard().build();
44-
this.mapper = new IonObjectMapper(new IonFactory(null, ionSystem));
44+
this.mapper = IonObjectMapper.builder(ionSystem).build();
4545
}
4646

4747
@Test

ion/src/test/java/com/fasterxml/jackson/dataformat/ion/failing/PrettyPrintWriteTest.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import org.junit.Test;
55

66
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
7-
import com.fasterxml.jackson.dataformat.ion.IonFactory;
87
import com.fasterxml.jackson.dataformat.ion.IonObjectMapper;
98

109
// For [dataformats-binary#245]: no pretty-printing for textual format
@@ -19,7 +18,7 @@ static class Point {
1918
@Test
2019
public void testBasicPrettyPrintTextual() throws Exception
2120
{
22-
IonObjectMapper mapper = IonObjectMapper.builder(IonFactory.forTextualWriters()).build();
21+
IonObjectMapper mapper = IonObjectMapper.builderForTextualWriters().build();
2322
Assert.assertEquals("{\n x:1,\n y:2\n}",
2423
mapper.writerWithDefaultPrettyPrinter().writeValueAsString(new Point()));
2524
}
@@ -28,7 +27,7 @@ public void testBasicPrettyPrintTextual() throws Exception
2827
@Test
2928
public void testIgnorePrettyPrintForBinary() throws Exception
3029
{
31-
IonObjectMapper mapper = IonObjectMapper.builder(IonFactory.forBinaryWriters()).build();
30+
IonObjectMapper mapper = IonObjectMapper.builderForBinaryWriters().build();
3231
byte[] encoded = mapper.writerWithDefaultPrettyPrinter().writeValueAsBytes(new Point());
3332
Assert.assertNotNull(encoded);
3433
}

ion/src/test/java/com/fasterxml/jackson/dataformat/ion/polymorphism/SerializationAnnotationsTest.java

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import com.amazon.ion.system.IonSystemBuilder;
55
import com.amazon.ion.util.Equivalence;
66
import com.fasterxml.jackson.annotation.JsonTypeInfo;
7-
import com.fasterxml.jackson.dataformat.ion.IonFactory;
87
import com.fasterxml.jackson.dataformat.ion.IonGenerator;
98
import com.fasterxml.jackson.dataformat.ion.IonObjectMapper;
109
import org.junit.Assert;
@@ -53,11 +52,9 @@ public void testNativeTypeIdsEnabledOnWriteByDefault() throws IOException {
5352

5453
@Test
5554
public void mapper() throws IOException {
56-
IonObjectMapper mapper = new IonObjectMapper(
57-
IonFactory.builderForTextualWriters()
58-
.disable(IonGenerator.Feature.USE_NATIVE_TYPE_ID)
59-
.build()
60-
);
55+
IonObjectMapper mapper = IonObjectMapper.builderForTextualWriters()
56+
.disable(IonGenerator.Feature.USE_NATIVE_TYPE_ID)
57+
.build();
6158

6259
IonValue subclassAsIon = mapper.writeValueAsIonValue(subclass);
6360
assertEqualIonValues(SUBCLASS_TYPED_AS_PROPERTY, subclassAsIon);
@@ -75,11 +72,9 @@ public void testNativeTypeIdsDisabledStillReadsNativeTypesSuccessfully() throws
7572

7673
assertEqualIonValues(SUBCLASS_TYPED_BY_ANNOTATION, subclassAsIon);
7774

78-
IonObjectMapper reader = new IonObjectMapper(
79-
IonFactory.builderForTextualWriters()
80-
.disable(IonGenerator.Feature.USE_NATIVE_TYPE_ID)
81-
.build()
82-
);
75+
IonObjectMapper reader = IonObjectMapper.builderForTextualWriters()
76+
.disable(IonGenerator.Feature.USE_NATIVE_TYPE_ID)
77+
.build();
8378

8479
BaseClass roundTripInstance = reader.readValue(subclassAsIon, BaseClass.class);
8580

0 commit comments

Comments
 (0)