Skip to content

Commit 0ee64b4

Browse files
committed
Update release notes, minor tweaks for #3212
1 parent 59f0de5 commit 0ee64b4

File tree

4 files changed

+40
-14
lines changed

4 files changed

+40
-14
lines changed

release-notes/CREDITS-2.x

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1495,6 +1495,10 @@ Jordi Ortolá Ankum (Tomasito665@github)
14951495
* Contributed #3613: Implement `float` and `boolean` to `String` coercion config
14961496
(2.14.0)
14971497
1498+
Felix Vaughan (FelixVaughan01@github)
1499+
* Contributed #3212: Add method `ObjectMapper.copyWith(JsonFactory)`
1500+
(2.14.0)
1501+
14981502
Arnaud Solé (Bluexin@github)
14991503
* Contributed #3505: Fix deduction deserializer with DefaultTypeResolverBuilder
15001504
(2.14.0)

release-notes/VERSION-2.x

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ Project: jackson-databind
66

77
Not yet released:
88

9+
#3212: Add method `ObjectMapper.copyWith(JsonFactory)`
10+
(requested by @quaff)
11+
(contributed by Felix V)
912
#3624: Legacy `ALLOW_COERCION_OF_SCALARS` interacts poorly with Integer to
1013
Float coercion
1114
(contributed by Carter K)

src/main/java/com/fasterxml/jackson/databind/ObjectMapper.java

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -595,9 +595,15 @@ protected ObjectMapper(ObjectMapper src)
595595
this(src, null);
596596
}
597597

598+
/**
599+
* Copy constructor with {@link JsonFactory} override: mostly needed
600+
* to support {@link #copyWith(JsonFactory)} method.
601+
*
602+
* @since 2.14
603+
*/
598604
protected ObjectMapper(ObjectMapper src, JsonFactory factory)
599605
{
600-
_jsonFactory = factory != null ? factory : src._jsonFactory.copy();
606+
_jsonFactory = (factory != null) ? factory : src._jsonFactory.copy();
601607
_jsonFactory.setCodec(this);
602608
_subtypeResolver = src._subtypeResolver.copy();
603609
_typeFactory = src._typeFactory;
@@ -608,10 +614,10 @@ protected ObjectMapper(ObjectMapper src, JsonFactory factory)
608614

609615
RootNameLookup rootNames = new RootNameLookup();
610616
_serializationConfig = new SerializationConfig(src._serializationConfig,
611-
_subtypeResolver, _mixIns, rootNames, _configOverrides);
617+
_subtypeResolver, _mixIns, rootNames, _configOverrides);
612618
_deserializationConfig = new DeserializationConfig(src._deserializationConfig,
613-
_subtypeResolver, _mixIns, rootNames, _configOverrides,
614-
_coercionConfigs);
619+
_subtypeResolver, _mixIns, rootNames, _configOverrides,
620+
_coercionConfigs);
615621
_serializerProvider = src._serializerProvider.copy();
616622
_deserializationContext = src._deserializationContext.copy();
617623

@@ -663,12 +669,12 @@ public ObjectMapper(JsonFactory jf,
663669
_configOverrides = new ConfigOverrides();
664670
_coercionConfigs = new CoercionConfigs();
665671
_serializationConfig = new SerializationConfig(base,
666-
_subtypeResolver, mixins, rootNames, _configOverrides,
667-
DatatypeFeatures.defaultFeatures());
672+
_subtypeResolver, mixins, rootNames, _configOverrides,
673+
DatatypeFeatures.defaultFeatures());
668674
_deserializationConfig = new DeserializationConfig(base,
669-
_subtypeResolver, mixins, rootNames, _configOverrides,
670-
_coercionConfigs,
671-
DatatypeFeatures.defaultFeatures());
675+
_subtypeResolver, mixins, rootNames, _configOverrides,
676+
_coercionConfigs,
677+
DatatypeFeatures.defaultFeatures());
672678

673679
// Some overrides we may need
674680
final boolean needOrder = _jsonFactory.requiresPropertyOrdering();
@@ -720,6 +726,20 @@ public ObjectMapper copy() {
720726
return new ObjectMapper(this);
721727
}
722728

729+
/**
730+
* Method for creating a new {@link ObjectMapper} instance that
731+
* has same initial configuration as this instance (similar to how
732+
* {@link #copy()} works, with one additional difference: that of
733+
* alternate underlying {@link JsonFactory} to use.
734+
* instance.
735+
*
736+
* @since 2.14
737+
*
738+
* @param factory Token stream factory to use for low-level streaming
739+
* reading (parsing) and writing (generation)
740+
*
741+
* @return Newly created mapper instance
742+
*/
723743
public ObjectMapper copyWith(JsonFactory factory) {
724744
_checkInvalidCopy(ObjectMapper.class);
725745
return new ObjectMapper(this, factory);
@@ -732,8 +752,8 @@ protected void _checkInvalidCopy(Class<?> exp)
732752
{
733753
if (getClass() != exp) {
734754
// 10-Nov-2016, tatu: could almost use `ClassUtil.verifyMustOverride()` but not quite
735-
throw new IllegalStateException("Failed copy(): "+getClass().getName()
736-
+" (version: "+version()+") does not override copy(); it has to");
755+
throw new IllegalStateException("Failed copy()/copyWith(): "+getClass().getName()
756+
+" (version: "+version()+") does not override copy()/copyWith(); it has to");
737757
}
738758
}
739759

src/test/java/com/fasterxml/jackson/databind/ObjectMapperTest.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -217,15 +217,14 @@ public void testCopyWith() throws JsonProcessingException {
217217
ObjectMapper copiedMapper = mapper.copyWith(newFactory);
218218
String json = "{ \"color\" : \"Black\", \"free\" : \"true\", \"pages\" : \"204.04\" }";
219219
JsonNode readResult = copiedMapper.readTree(json);
220-
//validate functionality
220+
// validate functionality
221221
assertEquals("Black", readResult.get("color").asText());
222222
assertEquals(true, readResult.get("free").asBoolean());
223223
assertEquals(204, readResult.get("pages").asInt());
224224
String readResultAsString = "{\n \"color\" : \"Black\",\n \"free\" : \"true\",\n \"pages\" : \"204.04\"\n}";
225-
System.out.println(mapper.writeValueAsString(readResult));
226225
assertEquals(readResultAsString, mapper.writeValueAsString(readResult));
227226

228-
//validate properties
227+
// validate properties
229228
Boolean mapperConfig1 = mapper._deserializationConfig.isEnabled(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES);
230229
Boolean copiedMapperConfig1 = copiedMapper._deserializationConfig.isEnabled(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES);
231230
Boolean mapperConfig2 = mapper._deserializationConfig.isEnabled(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL);

0 commit comments

Comments
 (0)