Skip to content

Commit 89a2025

Browse files
committed
Fix #2153
1 parent 3f94218 commit 89a2025

File tree

4 files changed

+89
-49
lines changed

4 files changed

+89
-49
lines changed

release-notes/VERSION-2.x

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Project: jackson-databind
1414
#2116: Make NumberSerializers.Base public and its inherited classes not final
1515
(requested by Édouard M)
1616
#2126: `DeserializationContext.instantiationException()` throws `InvalidDefinitionException`
17+
#2153: Add `JsonMapper` to replace generic `ObjectMapper` usage
1718

1819
2.9.7 (not yet released)
1920

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

-46
Original file line numberDiff line numberDiff line change
@@ -128,19 +128,6 @@ public class ObjectMapper
128128
/**********************************************************
129129
*/
130130

131-
/**
132-
* Base implementation for "Vanilla" {@link ObjectMapper}, used with JSON backend
133-
* as well as for some of simpler formats that do not require mapper level overrides.
134-
*
135-
* @since 2.10
136-
*/
137-
public static class Builder extends MapperBuilder<ObjectMapper, Builder>
138-
{
139-
public Builder(JsonFactory tsf) {
140-
super(new ObjectMapper(tsf));
141-
}
142-
}
143-
144131
/**
145132
* Enumeration used with {@link ObjectMapper#enableDefaultTyping()}
146133
* to specify what kind of types (classes) default typing should
@@ -604,39 +591,6 @@ protected ClassIntrospector defaultClassIntrospector() {
604591
return new BasicClassIntrospector();
605592
}
606593

607-
/*
608-
/**********************************************************
609-
/* Builder-based construction (2.10)
610-
/**********************************************************
611-
*/
612-
613-
// 16-Feb-2018, tatu: Arggghh. Due to Java Type Erasure rules, override, even static methods
614-
// are apparently bound to compatibility rules (despite them not being real overrides at all).
615-
// And because there is no "JsonMapper" we need to use odd weird typing here. Instead of simply
616-
// using `MapperBuilder` we already go
617-
618-
/**
619-
* Short-cut for:
620-
*<pre>
621-
* return builder(JsonFactory.builder().build());
622-
*</pre>
623-
*
624-
* @since 2.10
625-
*/
626-
@SuppressWarnings("unchecked")
627-
public static <M extends ObjectMapper, B extends MapperBuilder<M,B>> MapperBuilder<M,B> builder() {
628-
return (MapperBuilder<M,B>) jsonBuilder();
629-
}
630-
631-
// But here we can just use simple typing. Since there are no overloads of any kind.
632-
public static ObjectMapper.Builder jsonBuilder() {
633-
return new ObjectMapper.Builder(new JsonFactory());
634-
}
635-
636-
public static ObjectMapper.Builder builder(JsonFactory streamFactory) {
637-
return new ObjectMapper.Builder(streamFactory);
638-
}
639-
640594
/*
641595
/**********************************************************
642596
/* Methods sub-classes MUST override
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package com.fasterxml.jackson.databind.json;
2+
3+
import com.fasterxml.jackson.core.JsonFactory;
4+
import com.fasterxml.jackson.core.Version;
5+
import com.fasterxml.jackson.databind.ObjectMapper;
6+
import com.fasterxml.jackson.databind.cfg.MapperBuilder;
7+
import com.fasterxml.jackson.databind.cfg.PackageVersion;
8+
9+
/**
10+
*
11+
* @since 2.10
12+
*/
13+
public class JsonMapper extends ObjectMapper
14+
{
15+
private static final long serialVersionUID = 1L;
16+
17+
/**
18+
* Base implementation for "Vanilla" {@link ObjectMapper}, used with
19+
* JSON dataformat backend.
20+
*
21+
* @since 2.10
22+
*/
23+
public static class Builder extends MapperBuilder<JsonMapper, Builder>
24+
{
25+
public Builder(JsonMapper m) {
26+
super(m);
27+
}
28+
}
29+
30+
/*
31+
/**********************************************************
32+
/* Life-cycle, constructors
33+
/**********************************************************
34+
*/
35+
36+
public JsonMapper() {
37+
this(new JsonFactory());
38+
}
39+
40+
public JsonMapper(JsonFactory f) {
41+
super(f);
42+
}
43+
44+
protected JsonMapper(JsonMapper src) {
45+
super(src);
46+
}
47+
48+
@Override
49+
public JsonMapper copy()
50+
{
51+
_checkInvalidCopy(JsonMapper.class);
52+
return new JsonMapper(this);
53+
}
54+
55+
/*
56+
/**********************************************************
57+
/* Life-cycle, builders
58+
/**********************************************************
59+
*/
60+
61+
public static JsonMapper.Builder builder() {
62+
return new Builder(new JsonMapper());
63+
}
64+
65+
public static Builder builder(JsonFactory streamFactory) {
66+
return new Builder(new JsonMapper(streamFactory));
67+
}
68+
69+
/*
70+
/**********************************************************
71+
/* Standard method overrides
72+
/**********************************************************
73+
*/
74+
75+
@Override
76+
public Version version() {
77+
return PackageVersion.VERSION;
78+
}
79+
80+
@Override
81+
public JsonFactory getFactory() {
82+
return _jsonFactory;
83+
}
84+
}

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

+4-3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
import com.fasterxml.jackson.databind.ObjectMapper;
1313
import com.fasterxml.jackson.databind.deser.std.StdScalarDeserializer;
14+
import com.fasterxml.jackson.databind.json.JsonMapper;
1415
import com.fasterxml.jackson.databind.ser.std.StdScalarSerializer;
1516
import com.fasterxml.jackson.databind.type.TypeFactory;
1617

@@ -232,15 +233,15 @@ protected static ObjectMapper newObjectMapper() {
232233
return new ObjectMapper();
233234
}
234235

235-
// @since 2.10
236-
protected static ObjectMapper.Builder objectMapperBuilder() {
236+
// @since 2.10s
237+
protected static JsonMapper.Builder objectMapperBuilder() {
237238
/* 27-Aug-2018, tatu: Now this is weird -- with Java 7, we seem to need
238239
* explicit type parameters, but not with Java 8.
239240
* This need renders builder-approach pretty much useless for Java 7,
240241
* which is ok for 3.x (where baseline is Java 8), but potentially
241242
* problematic for 2.10. Oh well.
242243
*/
243-
return (ObjectMapper.Builder) ObjectMapper.<ObjectMapper,ObjectMapper.Builder>builder();
244+
return JsonMapper.builder();
244245
}
245246

246247
// @since 2.7

0 commit comments

Comments
 (0)