Skip to content

Commit 2fb7e1d

Browse files
authored
Fix FasterXML#509 (add IonValueMapper.builder() overloads) (FasterXML#510)
1 parent 294051d commit 2fb7e1d

File tree

4 files changed

+85
-8
lines changed

4 files changed

+85
-8
lines changed

ion/src/main/java/com/fasterxml/jackson/dataformat/ion/ionvalue/IonValueMapper.java

+65-6
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
import com.amazon.ion.IonSystem;
2626
import com.amazon.ion.IonValue;
27+
import com.amazon.ion.system.IonSystemBuilder;
2728

2829
/**
2930
* Supports serializing Ion to POJO and back using the Jackson Ion framework.
@@ -52,11 +53,27 @@ public IonValueMapper(IonSystem ionSystem) {
5253
this(ionSystem, null);
5354
}
5455

55-
// @since 2.18: needed for `copy()`
56+
/**
57+
* Needed for `copy()`
58+
*
59+
* @since 2.18
60+
*/
5661
protected IonValueMapper(IonValueMapper src) {
5762
super(src);
5863
}
5964

65+
/**
66+
* Needed for some builders
67+
*
68+
* @since 2.18
69+
*/
70+
protected IonValueMapper(IonFactory f, PropertyNamingStrategy strategy) {
71+
super(f);
72+
this.registerModule(new IonValueModule());
73+
this.registerModule(new EnumAsIonSymbolModule());
74+
this.setPropertyNamingStrategy(strategy);
75+
}
76+
6077
/**
6178
* Constructor that provides an override on the default Constructor for the PropertyNamingStrategy.
6279
*
@@ -66,19 +83,61 @@ protected IonValueMapper(IonValueMapper src) {
6683
* {@link PropertyNamingStrategy}
6784
*/
6885
public IonValueMapper(IonSystem ionSystem, PropertyNamingStrategy strategy) {
69-
super(new IonFactory(null, ionSystem));
70-
this.registerModule(new IonValueModule());
71-
this.registerModule(new EnumAsIonSymbolModule());
72-
this.setPropertyNamingStrategy(strategy);
86+
this(new IonFactory(null, ionSystem), strategy);
7387
}
7488

7589
/*
7690
/**********************************************************************
7791
/* Life-cycle, builders
92+
/*
93+
/* NOTE: must "override" (mask) all static methods from parent class
94+
/* (most of which just call basic `builder()` or `builder(IonSystem)`
7895
/**********************************************************************
7996
*/
8097

81-
// TODO: add overrides
98+
public static Builder builder() {
99+
return builder(IonSystemBuilder.standard().build());
100+
}
101+
102+
public static Builder builder(IonSystem ionSystem) {
103+
return builder(ionSystem, null);
104+
}
105+
106+
/**
107+
* Canonical {@code builder()} method that most other methods
108+
* ultimately call.
109+
*/
110+
public static Builder builder(IonSystem ionSystem, PropertyNamingStrategy strategy) {
111+
return new Builder(new IonValueMapper(ionSystem, strategy));
112+
}
113+
114+
public static Builder builderForBinaryWriters() {
115+
return builderForBinaryWriters(IonSystemBuilder.standard().build());
116+
}
117+
118+
public static Builder builderForBinaryWriters(IonSystem ionSystem) {
119+
return builder(IonFactory.builderForBinaryWriters()
120+
.ionSystem(ionSystem)
121+
.build());
122+
}
123+
124+
public static Builder builderForTextualWriters() {
125+
return builderForTextualWriters(IonSystemBuilder.standard().build());
126+
}
127+
128+
public static Builder builderForTextualWriters(IonSystem ionSystem) {
129+
return builder(IonFactory.builderForTextualWriters()
130+
.ionSystem(ionSystem)
131+
.build());
132+
}
133+
134+
public static Builder builder(IonFactory streamFactory) {
135+
return builder(streamFactory, null);
136+
}
137+
138+
public static Builder builder(IonFactory streamFactory, PropertyNamingStrategy strategy) {
139+
return new Builder(new IonValueMapper(streamFactory, strategy));
140+
}
82141

83142
/*
84143
/**********************************************************************

ion/src/test/java/com/fasterxml/jackson/dataformat/ion/ionvalue/IonValueMapperTest.java

+14-2
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@
5050
*/
5151
public class IonValueMapperTest {
5252
private final IonSystem ionSystem = IonSystemBuilder.standard().build();
53-
private final IonValueMapper ionValueMapper = new IonValueMapper(ionSystem,
54-
PropertyNamingStrategies.SNAKE_CASE);
53+
private final IonValueMapper ionValueMapper = (IonValueMapper) IonValueMapper.builder(ionSystem,
54+
PropertyNamingStrategies.SNAKE_CASE).build();
5555

5656
enum ReturnCode {
5757
Success,
@@ -252,4 +252,16 @@ private void assertRoundTrip(String ion, Class<?> clazz) throws IOException {
252252
IonValue actual = ionValueMapper.serialize(o);
253253
assertEquals(expected, actual);
254254
}
255+
256+
// for [dataformats-binary#509]
257+
@Test
258+
public void testBuilders() throws Exception {
259+
IonObjectMapper mapper = IonValueMapper.builder().build();
260+
assertNotNull(mapper);
261+
assertEquals(IonValueMapper.class, mapper.getClass());
262+
263+
mapper = IonValueMapper.builder(ionSystem).build();
264+
assertNotNull(mapper);
265+
assertEquals(IonValueMapper.class, mapper.getClass());
266+
}
255267
}

release-notes/CREDITS-2.x

+4
Original file line numberDiff line numberDiff line change
@@ -338,3 +338,7 @@ Joachim Lous (@jlous)
338338
* Requested #494: Avro Schema generation: allow mapping Java Enum properties to
339339
Avro String values
340340
(2.18.0)
341+
342+
Robert Noack (@mr-robert)
343+
* Reported #509: IonValueMapper.builder() not implemented, does not register modules
344+
(2.18.0)

release-notes/VERSION-2.x

+2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ Active maintainers:
2424
(contributed by Michal F)
2525
#508: (avro) Ignore `specificData` field on serialization
2626
(contributed by @pjfanning)
27+
#509: IonValueMapper.builder() not implemented, does not register modules
28+
(reported by Robert N)
2729

2830
2.17.3 (not yet released)
2931

0 commit comments

Comments
 (0)