Skip to content

Commit accce7c

Browse files
committed
add builder for IonObjectMapper as well, to complete 2.10 binary codec upgrade
1 parent 2434a63 commit accce7c

File tree

5 files changed

+60
-29
lines changed

5 files changed

+60
-29
lines changed

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

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import com.fasterxml.jackson.core.type.TypeReference;
2222
import com.fasterxml.jackson.databind.JavaType;
2323
import com.fasterxml.jackson.databind.ObjectMapper;
24+
import com.fasterxml.jackson.databind.cfg.MapperBuilder;
2425
import com.fasterxml.jackson.databind.module.SimpleModule;
2526

2627
import software.amazon.ion.IonDatagram;
@@ -37,6 +38,26 @@ public class IonObjectMapper extends ObjectMapper
3738
{
3839
private static final long serialVersionUID = 1L;
3940

41+
/**
42+
* Base implementation for "Vanilla" {@link ObjectMapper}, used with
43+
* Avro backend.
44+
*
45+
* @since 2.10
46+
*/
47+
public static class Builder extends MapperBuilder<IonObjectMapper, Builder>
48+
{
49+
50+
public Builder(IonObjectMapper m) {
51+
super(m);
52+
}
53+
}
54+
55+
/*
56+
/**********************************************************************
57+
/* Life-cycle
58+
/**********************************************************************
59+
*/
60+
4061
public IonObjectMapper() {
4162
this(new IonFactory());
4263
}
@@ -57,16 +78,25 @@ protected IonObjectMapper(IonObjectMapper src) {
5778
super(src);
5879
}
5980

60-
public void setCreateBinaryWriters(boolean bin) {
61-
getFactory().setCreateBinaryWriters(bin);
62-
}
81+
@SuppressWarnings("unchecked")
82+
public static Builder builder() {
83+
return new Builder(new IonObjectMapper());
84+
}
85+
86+
public static Builder builder(IonFactory streamFactory) {
87+
return new Builder(new IonObjectMapper(streamFactory));
88+
}
6389

6490
@Override
6591
public ObjectMapper copy() {
6692
_checkInvalidCopy(IonObjectMapper.class);
6793
return new IonObjectMapper(this);
6894
}
6995

96+
public void setCreateBinaryWriters(boolean bin) {
97+
getFactory().setCreateBinaryWriters(bin);
98+
}
99+
70100
@Override
71101
public Version version() {
72102
return PackageVersion.VERSION;

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ static class BeanToo {
4545
@Test
4646
public void testSimple() throws IOException
4747
{
48-
IonObjectMapper m = new IonObjectMapper();
48+
IonObjectMapper m = IonObjectMapper.builder().build();
4949
MyBean bean = m.readValue("{a: \"...\", \"b\" : 39, blob:{{SGVsbG8h}} }", MyBean.class);
5050
assertEquals("...", bean.a);
5151
assertEquals(39, bean.b);
@@ -62,11 +62,11 @@ public void testSimple() throws IOException
6262
assertEquals(2, bean.b);
6363

6464
}
65-
65+
6666
@Test
6767
public void testJsonIgnoreProperty() throws IOException
6868
{
69-
IonObjectMapper m = new IonObjectMapper();
69+
IonObjectMapper m = IonObjectMapper.builder().build();
7070
MyBean bean = m.readValue("{a: \"...\", ignore:{x:\"y\"}, \"b\" : 39 }", MyBean.class);
7171
assertEquals("...", bean.a);
7272
assertEquals(39, bean.b);
@@ -79,7 +79,7 @@ public void testJsonIgnoreProperty() throws IOException
7979
@Test
8080
public void testFromIon() throws IOException
8181
{
82-
IonObjectMapper m = new IonObjectMapper();
82+
IonObjectMapper m = IonObjectMapper.builder().build();
8383
IonSystem ion = IonSystemBuilder.standard().build();
8484
IonValue value = ion.singleValue("{payload: {'a': bc, b : '14' }}");
8585
MyBean bean = m.readValue(((IonStruct) value).get("payload"), MyBean.class);
@@ -94,7 +94,7 @@ public void testFromIon() throws IOException
9494
@Test
9595
public void testBasicTypes() throws IOException
9696
{
97-
IonObjectMapper m = new IonObjectMapper();
97+
IonObjectMapper m = IonObjectMapper.builder().build();
9898
IonSystem ion = IonSystemBuilder.standard().build();
9999
assertNull(m.readValue(ion.newNull(), Object.class));
100100
assertEquals("foo", m.readValue(ion.newString("foo"), String.class));
@@ -108,7 +108,7 @@ public void testBasicTypes() throws IOException
108108
@Test
109109
public void testMultipleReads() throws IOException
110110
{
111-
IonObjectMapper m = new IonObjectMapper();
111+
IonObjectMapper m = IonObjectMapper.builder().build();
112112
IonSystem ion = IonSystemBuilder.standard().build();
113113

114114
IonReader reader = ion.newReader("[foo, bar, baz]");

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,8 @@ public void testSimpleObjectWriteIon() throws Exception
106106
@Test
107107
public void testWriteBasicTypes() throws Exception
108108
{
109-
IonObjectMapper m = new IonObjectMapper(new IonFactory(null, ion));
109+
IonObjectMapper m = IonObjectMapper.builder(new IonFactory(null, ion))
110+
.build();
110111

111112
assertEquals(ion.newString("foo"), m.writeValueAsIonValue("foo"));
112113
assertEquals(ion.newBool(true), m.writeValueAsIonValue(true));
@@ -117,8 +118,8 @@ public void testWriteBasicTypes() throws Exception
117118
@Test
118119
public void testIntArrayWriteText() throws Exception
119120
{
120-
IonObjectMapper m = new IonObjectMapper();
121-
m.setCreateBinaryWriters(false);
121+
IonObjectMapper m = IonObjectMapper.builder(IonFactory.forTextualWriters())
122+
.build();
122123
IonDatagram loadedDatagram = ion.newLoader().load(m.writeValueAsString(new int[] { 1, 2, 3 } ));
123124
assertEquals(expectedArray, loadedDatagram);
124125
}
@@ -136,8 +137,8 @@ public void testIntArrayWriteBinary() throws Exception
136137

137138
private byte[] _writeAsBytes(Object ob) throws IOException
138139
{
139-
IonObjectMapper m = new IonObjectMapper();
140-
m.setCreateBinaryWriters(true);
140+
IonObjectMapper m = IonObjectMapper.builder(IonFactory.forBinaryWriters())
141+
.build();
141142
ByteArrayOutputStream out = new ByteArrayOutputStream();
142143
m.writeValue(out, ob);
143144
return out.toByteArray();

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public class IonGeneratorTest {
6363
public void setUp() throws Exception {
6464
final IonFactory factory = new IonFactory();
6565

66-
this.joiObjectMapper = new IonObjectMapper(factory);
66+
this.joiObjectMapper = IonObjectMapper.builder(factory).build();
6767
this.ionSystem = IonSystemBuilder.standard().build();
6868
this.output = ionSystem.newDatagram();
6969
this.joiGenerator = (IonGenerator) factory.createGenerator(ionSystem.newWriter(this.output));

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

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,11 @@ public class IonTimestampRoundTripTest {
3737
IonSystem ionSystem = IonSystemBuilder.standard().build();
3838

3939
@Test
40-
public void testDateRoundTrip() throws JsonGenerationException, JsonMappingException, IOException {
40+
public void testDateRoundTrip() throws IOException {
4141
Date date = new Date();
42-
IonObjectMapper m = new IonObjectMapper();
43-
m.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
42+
IonObjectMapper m = IonObjectMapper.builder()
43+
.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
44+
.build();
4445

4546
String val = m.writeValueAsString(date);
4647
IonLoader loader = ionSystem.newLoader();
@@ -55,31 +56,31 @@ public void testDateRoundTrip() throws JsonGenerationException, JsonMappingExcep
5556
}
5657

5758
@Test
58-
public void testMillisCompatibility() throws JsonGenerationException, JsonMappingException, IOException {
59+
public void testMillisCompatibility() throws IOException {
5960
Date date = new Date();
60-
IonObjectMapper m = new IonObjectMapper();
61-
62-
m.enable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
61+
IonObjectMapper m = IonObjectMapper.builder()
62+
.enable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
63+
.build();
6364

6465
String val = m.writeValueAsString(date);
6566
Date returned = m.readValue(val, Date.class);
6667
Assert.assertEquals("Date result not the same as serialized value.", date, returned);
67-
6868
}
69-
7069
/**
7170
* Test if the JoiMapper can read all forms of dates generated by non-JoiMappers
7271
* @throws JsonGenerationException
7372
* @throws JsonMappingException
7473
* @throws IOException
7574
*/
7675
@Test
77-
public void testNonJoiCompatibility() throws JsonGenerationException, JsonMappingException, IOException {
76+
public void testNonJoiCompatibility() throws IOException {
7877
Date date = new Date();
7978
ObjectMapper nonJoiMillis = new ObjectMapper();
80-
ObjectMapper nonJoiM = new ObjectMapper();
81-
nonJoiM.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
82-
IonObjectMapper joiM = new IonObjectMapper();
79+
ObjectMapper nonJoiM = ObjectMapper.builder()
80+
.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
81+
.build();
82+
83+
IonObjectMapper joiM = IonObjectMapper.builder().build();
8384

8485
String dateInMillis = nonJoiMillis.writeValueAsString(date);
8586
String dateFormatted = nonJoiM.writeValueAsString(date);
@@ -89,6 +90,5 @@ public void testNonJoiCompatibility() throws JsonGenerationException, JsonMappin
8990

9091
Assert.assertEquals("Date result not the same as serialized value.", date, millisBack);
9192
Assert.assertEquals("Date result not the same as serialized value.", date, formattedBack);
92-
9393
}
9494
}

0 commit comments

Comments
 (0)