Skip to content

Commit ed31261

Browse files
committed
Add builder-construction for AvroMapper
1 parent 7f64021 commit ed31261

File tree

9 files changed

+107
-14
lines changed

9 files changed

+107
-14
lines changed

avro/src/main/java/com/fasterxml/jackson/dataformat/avro/AvroMapper.java

Lines changed: 93 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@
77
import org.apache.avro.Schema;
88

99
import com.fasterxml.jackson.core.Version;
10+
1011
import com.fasterxml.jackson.databind.JavaType;
1112
import com.fasterxml.jackson.databind.JsonMappingException;
1213
import com.fasterxml.jackson.databind.Module;
1314
import com.fasterxml.jackson.databind.ObjectMapper;
15+
import com.fasterxml.jackson.databind.cfg.MapperBuilder;
16+
import com.fasterxml.jackson.dataformat.avro.AvroMapper.Builder;
1417
import com.fasterxml.jackson.dataformat.avro.schema.AvroSchemaGenerator;
1518

1619
/**
@@ -22,7 +25,77 @@
2225
*/
2326
public class AvroMapper extends ObjectMapper
2427
{
25-
private static final long serialVersionUID = 1L;
28+
private static final long serialVersionUID = 2L;
29+
30+
/**
31+
* Base implementation for "Vanilla" {@link ObjectMapper}, used with
32+
* Avro backend.
33+
*
34+
* @since 2.10
35+
*/
36+
public static class Builder extends MapperBuilder<AvroMapper, Builder>
37+
{
38+
protected final AvroFactory _streamFactory; // since 2.10
39+
40+
public Builder(AvroMapper m) {
41+
super(m);
42+
_streamFactory = m.getFactory();
43+
}
44+
45+
/*
46+
/******************************************************************
47+
/* Format features
48+
/******************************************************************
49+
*/
50+
51+
public Builder enable(AvroParser.Feature... features) {
52+
for (AvroParser.Feature f : features) {
53+
_streamFactory.enable(f);
54+
}
55+
return this;
56+
}
57+
58+
public Builder disable(AvroParser.Feature... features) {
59+
for (AvroParser.Feature f : features) {
60+
_streamFactory.disable(f);
61+
}
62+
return this;
63+
}
64+
65+
public Builder configure(AvroParser.Feature f, boolean state)
66+
{
67+
if (state) {
68+
_streamFactory.enable(f);
69+
} else {
70+
_streamFactory.disable(f);
71+
}
72+
return this;
73+
}
74+
75+
public Builder enable(AvroGenerator.Feature... features) {
76+
for (AvroGenerator.Feature f : features) {
77+
_streamFactory.enable(f);
78+
}
79+
return this;
80+
}
81+
82+
public Builder disable(AvroGenerator.Feature... features) {
83+
for (AvroGenerator.Feature f : features) {
84+
_streamFactory.disable(f);
85+
}
86+
return this;
87+
}
88+
89+
public Builder configure(AvroGenerator.Feature f, boolean state)
90+
{
91+
if (state) {
92+
_streamFactory.enable(f);
93+
} else {
94+
_streamFactory.disable(f);
95+
}
96+
return this;
97+
}
98+
}
2699

27100
/**
28101
* Constructor that will construct mapper with standard {@link AvroFactory}
@@ -65,6 +138,25 @@ protected AvroMapper(ObjectMapper src) {
65138
super(src);
66139
}
67140

141+
/**
142+
* @since 2.10
143+
*/
144+
public static AvroMapper.Builder xmlBuilder() {
145+
return new AvroMapper.Builder(new AvroMapper());
146+
}
147+
148+
/**
149+
* @since 2.10
150+
*/
151+
@SuppressWarnings("unchecked")
152+
public static AvroMapper.Builder builder() {
153+
return new AvroMapper.Builder(new AvroMapper());
154+
}
155+
156+
public static Builder builder(AvroFactory streamFactory) {
157+
return new AvroMapper.Builder(new AvroMapper(streamFactory));
158+
}
159+
68160
@Override
69161
public AvroMapper copy()
70162
{

avro/src/test/java/com/fasterxml/jackson/dataformat/avro/AvroTestBase.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ protected AvroMapper getMapper() {
290290
}
291291

292292
protected AvroMapper newMapper() {
293-
return new AvroMapper();
293+
return AvroMapper.builder().build();
294294
}
295295

296296
protected byte[] toAvro(Employee empl) throws IOException {

avro/src/test/java/com/fasterxml/jackson/dataformat/avro/BigDecimalTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public NamedAmount(@JsonProperty("name") String name,
2020
}
2121

2222
public void testSerializeBigDecimal() throws Exception {
23-
AvroMapper mapper = new AvroMapper();
23+
AvroMapper mapper = newMapper();
2424
AvroSchema schema = mapper.schemaFor(NamedAmount.class);
2525

2626
byte[] bytes = mapper.writer(schema)

avro/src/test/java/com/fasterxml/jackson/dataformat/avro/BinaryDataTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ public FilePojo(String text) throws IOException {
2626
public long size;
2727
}
2828

29-
private final AvroMapper AVRO_JACKSON_MAPPER = new AvroMapper(new AvroFactory());
30-
private final AvroMapper AVRO_APACHE_MAPPER = new AvroMapper(new ApacheAvroFactory());
29+
private final AvroMapper AVRO_JACKSON_MAPPER = AvroMapper.builder(new AvroFactory()).build();
30+
private final AvroMapper AVRO_APACHE_MAPPER = AvroMapper.builder(new ApacheAvroFactory()).build();
3131

3232
public void testAvroSchemaGenerationWithJackson() throws Exception
3333
{

avro/src/test/java/com/fasterxml/jackson/dataformat/avro/EnumTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ protected static class EmployeeStr {
2121
public String gender;
2222
}
2323

24-
private final AvroMapper MAPPER = new AvroMapper();
24+
private final AvroMapper MAPPER = newMapper();
2525

2626
public void testSimple() throws Exception
2727
{

avro/src/test/java/com/fasterxml/jackson/dataformat/avro/MapperConfigTest.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,11 @@ public void testGeneratorDefaults() throws Exception
8080
// Test to verify that data format affects default state of order-props-alphabetically
8181
public void testDefaultSettingsWithObjectMapper()
8282
{
83-
ObjectMapper mapper = new ObjectMapper(AVRO_F);
83+
ObjectMapper mapper = AvroMapper.builder(AVRO_F).build();
8484
_testAvroMapperDefaults(mapper);
8585

86-
// and even with default mapper, may become so, if configred with AvroFactory
86+
// and even with default mapper, may become so if configured with AvroFactory
87+
// 26-Jun-2018, tatu: NOTE! 2.x only -- 3.x will not allow mix-n-matching
8788
ObjectMapper vanilla = new ObjectMapper();
8889
assertFalse(vanilla.isEnabled(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY));
8990
ObjectReader r = vanilla.reader();
@@ -95,7 +96,7 @@ public void testDefaultSettingsWithObjectMapper()
9596

9697
public void testDefaultSettingsWithAvroMapper()
9798
{
98-
AvroMapper mapper = new AvroMapper();
99+
AvroMapper mapper = AvroMapper.builder().build();
99100
assertNotNull(mapper.version());
100101

101102
_testAvroMapperDefaults(mapper);

avro/src/test/java/com/fasterxml/jackson/dataformat/avro/NumberTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ public Numbers(int i0, long l0,
3737
}
3838
}
3939

40-
private final AvroMapper MAPPER = new AvroMapper();
41-
40+
private final AvroMapper MAPPER = newMapper();
41+
4242
// for [dataformat-avro#41]
4343
public void testNumberType() throws Exception
4444
{

avro/src/test/java/com/fasterxml/jackson/dataformat/avro/OptionalEnumTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ protected static class Employee {
88
public Gender gender;
99
}
1010

11-
private final AvroMapper MAPPER = new AvroMapper();
11+
private final AvroMapper MAPPER = newMapper();
1212

1313
// [dataformat-avro#12]
1414
public void testEnumViaGeneratedSchema() throws Exception

avro/src/test/java/com/fasterxml/jackson/dataformat/avro/SerializeGeneratedTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.fasterxml.jackson.dataformat.avro;
22

33
import com.fasterxml.jackson.databind.ObjectMapper;
4-
import com.fasterxml.jackson.dataformat.avro.AvroMapper;
4+
55
import com.fasterxml.jackson.dataformat.avro.AvroSchema;
66
import com.fasterxml.jackson.dataformat.avro.gen.Event35;
77
import com.fasterxml.jackson.dataformat.avro.gen.Event35Id;
@@ -15,7 +15,7 @@ public void testWriteGeneratedEvent() throws Exception
1515
event.setPlayerCount(100);
1616
Event35Id id = new Event35Id();
1717
id.setFirst(10);
18-
ObjectMapper mapper = new AvroMapper();
18+
ObjectMapper mapper = newMapper();
1919
byte[] bytes = mapper.writer(new AvroSchema(Event35.SCHEMA$)).writeValueAsBytes(event);
2020
assertNotNull(bytes);
2121
}

0 commit comments

Comments
 (0)