Skip to content

Commit 2434a63

Browse files
committed
add builder() for protobuf mapper
1 parent ed31261 commit 2434a63

File tree

12 files changed

+72
-41
lines changed

12 files changed

+72
-41
lines changed

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import com.fasterxml.jackson.databind.Module;
1414
import com.fasterxml.jackson.databind.ObjectMapper;
1515
import com.fasterxml.jackson.databind.cfg.MapperBuilder;
16-
import com.fasterxml.jackson.dataformat.avro.AvroMapper.Builder;
1716
import com.fasterxml.jackson.dataformat.avro.schema.AvroSchemaGenerator;
1817

1918
/**

protobuf/src/main/java/com/fasterxml/jackson/dataformat/protobuf/ProtobufMapper.java

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@
66
import java.net.URL;
77

88
import com.fasterxml.jackson.core.Version;
9+
910
import com.fasterxml.jackson.databind.JavaType;
1011
import com.fasterxml.jackson.databind.JsonMappingException;
1112
import com.fasterxml.jackson.databind.ObjectMapper;
13+
import com.fasterxml.jackson.databind.cfg.MapperBuilder;
14+
import com.fasterxml.jackson.dataformat.protobuf.ProtobufMapper.Builder;
1215
import com.fasterxml.jackson.dataformat.protobuf.schema.DescriptorLoader;
1316
import com.fasterxml.jackson.dataformat.protobuf.schema.FileDescriptorSet;
1417
import com.fasterxml.jackson.dataformat.protobuf.schema.ProtobufSchema;
@@ -19,6 +22,19 @@ public class ProtobufMapper extends ObjectMapper
1922
{
2023
private static final long serialVersionUID = 1L;
2124

25+
/**
26+
* Base implementation for "Vanilla" {@link ObjectMapper}, used with
27+
* Protobuf backend.
28+
*
29+
* @since 2.10
30+
*/
31+
public static class Builder extends MapperBuilder<ProtobufMapper, Builder>
32+
{
33+
public Builder(ProtobufMapper m) {
34+
super(m);
35+
}
36+
}
37+
2238
protected ProtobufSchemaLoader _schemaLoader = ProtobufSchemaLoader.std;
2339

2440
/**
@@ -46,7 +62,22 @@ public ProtobufMapper(ProtobufFactory f) {
4662
protected ProtobufMapper(ProtobufMapper src) {
4763
super(src);
4864
}
49-
65+
66+
/**
67+
* @since 2.10
68+
*/
69+
@SuppressWarnings("unchecked")
70+
public static ProtobufMapper.Builder builder() {
71+
return new Builder(new ProtobufMapper());
72+
}
73+
74+
/**
75+
* @since 2.10
76+
*/
77+
public static Builder builder(ProtobufFactory streamFactory) {
78+
return new Builder(new ProtobufMapper(streamFactory));
79+
}
80+
5081
@Override
5182
public ProtobufMapper copy()
5283
{

protobuf/src/test/java/com/fasterxml/jackson/dataformat/protobuf/EnumHandlingTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ static class BigEnumWrapper {
3535
/**********************************************************
3636
*/
3737

38-
final ProtobufMapper MAPPER = new ProtobufMapper();
38+
final ProtobufMapper MAPPER = newObjectMapper();
3939

4040
public void testBigEnum() throws Exception
4141
{

protobuf/src/test/java/com/fasterxml/jackson/dataformat/protobuf/ProtobufTestBase.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,17 @@ && _equals(copyright, other.copyright)
453453
;
454454
}
455455
}
456-
456+
457+
/*
458+
/**********************************************************
459+
/* Factory methods
460+
/**********************************************************
461+
*/
462+
463+
protected ProtobufMapper newObjectMapper() {
464+
return ProtobufMapper.builder().build();
465+
}
466+
457467
/*
458468
/**********************************************************
459469
/* Additional assertion methods

protobuf/src/test/java/com/fasterxml/jackson/dataformat/protobuf/ReadPackedRepeatedTest.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,18 @@
77

88
public class ReadPackedRepeatedTest extends ProtobufTestBase
99
{
10-
final ProtobufMapper MAPPER = new ProtobufMapper();
11-
1210
public void testPacked() throws Exception
1311
{
12+
final ProtobufMapper mapper = newObjectMapper();
1413
final String SCHEMA_STR =
1514
"package mypackage;\n"
1615
+ "message t {\n"
1716
+ " repeated uint32 f = 1 [packed=true];\n"
1817
+ "}";
1918
final byte[] pb = {0xa, 0x3, 0x64, (byte)0xc8, 0x1}; // f = [100, 200]
2019

21-
ProtobufSchema schema = MAPPER.schemaLoader().load(new StringReader(SCHEMA_STR));
22-
JsonNode t = MAPPER.readerFor(JsonNode.class).with(schema).readValue(pb);
20+
ProtobufSchema schema = mapper.schemaLoader().load(new StringReader(SCHEMA_STR));
21+
JsonNode t = mapper.readerFor(JsonNode.class).with(schema).readValue(pb);
2322

2423
assertEquals(2, t.get("f").size());
2524
assertEquals(100, t.get("f").get(0).asInt());

protobuf/src/test/java/com/fasterxml/jackson/dataformat/protobuf/ReadSimpleTest.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public NamedStrings(String n, String... v) {
5555
/**********************************************************
5656
*/
5757

58-
final ObjectMapper MAPPER = new ProtobufMapper();
58+
final ObjectMapper MAPPER = newObjectMapper();
5959

6060
public void testReadPointInt() throws Exception
6161
{
@@ -340,12 +340,11 @@ public void testSearchMessage() throws Exception
340340

341341
public void testSkipUnknown() throws Exception
342342
{
343-
// Important: write Point3, read regular Point
344-
ProtobufMapper mapper = new ProtobufMapper();
345-
346343
ProtobufSchema pointSchema = ProtobufSchemaLoader.std.parse(PROTOC_POINT);
347344
ProtobufSchema point3Schema = ProtobufSchemaLoader.std.parse(PROTOC_POINT3);
348345

346+
// Important: write Point3, read regular Point
347+
ProtobufMapper mapper = newObjectMapper();
349348
mapper.enable(JsonParser.Feature.IGNORE_UNDEFINED);
350349

351350
final Point3 input = new Point3(1, 2, 3);

protobuf/src/test/java/com/fasterxml/jackson/dataformat/protobuf/ReadUnkownFieldsTest.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,21 +64,21 @@ public void setF3(int f3) {
6464
/**********************************************************
6565
*/
6666

67-
final ProtobufMapper MAPPER = new ProtobufMapper();
68-
6967
public void testMultipleUnknown() throws Exception
7068
{
69+
final ProtobufMapper mapper = newObjectMapper();
70+
7171
ThreeField threeField = new ThreeField();
7272
threeField.setF1(1);
7373
threeField.setF2(2);
7474
threeField.setF3(3);
7575

76-
ProtobufSchema schemaWith3 = MAPPER.generateSchemaFor(ThreeField.class);
77-
byte[] in = MAPPER.writer(schemaWith3)
76+
ProtobufSchema schemaWith3 = mapper.generateSchemaFor(ThreeField.class);
77+
byte[] in = mapper.writer(schemaWith3)
7878
.writeValueAsBytes(threeField);
7979

80-
ProtobufSchema schemaWith1 = MAPPER.generateSchemaFor(OneField.class);
81-
OneField oneField = MAPPER.readerFor(OneField.class).with(schemaWith1)
80+
ProtobufSchema schemaWith1 = mapper.generateSchemaFor(OneField.class);
81+
OneField oneField = mapper.readerFor(OneField.class).with(schemaWith1)
8282
// important: skip through unknown
8383
.with(JsonParser.Feature.IGNORE_UNDEFINED)
8484
.readValue(in);

protobuf/src/test/java/com/fasterxml/jackson/dataformat/protobuf/WriteAsMapTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public class WriteAsMapTest extends ProtobufTestBase
3232

3333
public void testWriteAsMap() throws Exception
3434
{
35-
ObjectMapper mapper = new ProtobufMapper();
35+
ObjectMapper mapper = newObjectMapper();
3636

3737
NativeProtobufSchema fileSchema = ProtobufSchemaLoader.std.parseNative(PROTOC);
3838
ProtobufSchema schema = fileSchema.forType("Person");

protobuf/src/test/java/com/fasterxml/jackson/dataformat/protobuf/WriteBinaryTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public Binary(int id, byte[] data, int trailer) {
3333
/**********************************************************
3434
*/
3535

36-
private final ObjectMapper MAPPER = new ProtobufMapper();
36+
private final ObjectMapper MAPPER = newObjectMapper();
3737

3838
public void testSimpleBinary() throws Exception
3939
{

protobuf/src/test/java/com/fasterxml/jackson/dataformat/protobuf/WriteLongString94Test.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public static class TwoStrings {
1818
/**********************************************************
1919
*/
2020

21-
final ProtobufMapper MAPPER = new ProtobufMapper();
21+
final ProtobufMapper MAPPER = newObjectMapper();
2222

2323
// [dataformats-binary#94]
2424
public void testLongerStrings() throws Exception {

0 commit comments

Comments
 (0)