diff --git a/release-notes/VERSION-2.x b/release-notes/VERSION-2.x index 766ff8599d..48fa34780b 100644 --- a/release-notes/VERSION-2.x +++ b/release-notes/VERSION-2.x @@ -14,6 +14,7 @@ Project: jackson-databind #2116: Make NumberSerializers.Base public and its inherited classes not final (requested by Édouard M) #2126: `DeserializationContext.instantiationException()` throws `InvalidDefinitionException` +#2153: Add `JsonMapper` to replace generic `ObjectMapper` usage 2.9.7 (not yet released) diff --git a/src/main/java/com/fasterxml/jackson/databind/ObjectMapper.java b/src/main/java/com/fasterxml/jackson/databind/ObjectMapper.java index 1b05f57462..318da9f670 100644 --- a/src/main/java/com/fasterxml/jackson/databind/ObjectMapper.java +++ b/src/main/java/com/fasterxml/jackson/databind/ObjectMapper.java @@ -128,19 +128,6 @@ public class ObjectMapper /********************************************************** */ - /** - * Base implementation for "Vanilla" {@link ObjectMapper}, used with JSON backend - * as well as for some of simpler formats that do not require mapper level overrides. - * - * @since 2.10 - */ - public static class Builder extends MapperBuilder - { - public Builder(JsonFactory tsf) { - super(new ObjectMapper(tsf)); - } - } - /** * Enumeration used with {@link ObjectMapper#enableDefaultTyping()} * to specify what kind of types (classes) default typing should @@ -604,39 +591,6 @@ protected ClassIntrospector defaultClassIntrospector() { return new BasicClassIntrospector(); } - /* - /********************************************************** - /* Builder-based construction (2.10) - /********************************************************** - */ - - // 16-Feb-2018, tatu: Arggghh. Due to Java Type Erasure rules, override, even static methods - // are apparently bound to compatibility rules (despite them not being real overrides at all). - // And because there is no "JsonMapper" we need to use odd weird typing here. Instead of simply - // using `MapperBuilder` we already go - - /** - * Short-cut for: - *
-     *   return builder(JsonFactory.builder().build());
-     *
- * - * @since 2.10 - */ - @SuppressWarnings("unchecked") - public static > MapperBuilder builder() { - return (MapperBuilder) jsonBuilder(); - } - - // But here we can just use simple typing. Since there are no overloads of any kind. - public static ObjectMapper.Builder jsonBuilder() { - return new ObjectMapper.Builder(new JsonFactory()); - } - - public static ObjectMapper.Builder builder(JsonFactory streamFactory) { - return new ObjectMapper.Builder(streamFactory); - } - /* /********************************************************** /* Methods sub-classes MUST override diff --git a/src/main/java/com/fasterxml/jackson/databind/json/JsonMapper.java b/src/main/java/com/fasterxml/jackson/databind/json/JsonMapper.java new file mode 100644 index 0000000000..18ab72bdfc --- /dev/null +++ b/src/main/java/com/fasterxml/jackson/databind/json/JsonMapper.java @@ -0,0 +1,84 @@ +package com.fasterxml.jackson.databind.json; + +import com.fasterxml.jackson.core.JsonFactory; +import com.fasterxml.jackson.core.Version; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.cfg.MapperBuilder; +import com.fasterxml.jackson.databind.cfg.PackageVersion; + +/** + * + * @since 2.10 + */ +public class JsonMapper extends ObjectMapper +{ + private static final long serialVersionUID = 1L; + + /** + * Base implementation for "Vanilla" {@link ObjectMapper}, used with + * JSON dataformat backend. + * + * @since 2.10 + */ + public static class Builder extends MapperBuilder + { + public Builder(JsonMapper m) { + super(m); + } + } + + /* + /********************************************************** + /* Life-cycle, constructors + /********************************************************** + */ + + public JsonMapper() { + this(new JsonFactory()); + } + + public JsonMapper(JsonFactory f) { + super(f); + } + + protected JsonMapper(JsonMapper src) { + super(src); + } + + @Override + public JsonMapper copy() + { + _checkInvalidCopy(JsonMapper.class); + return new JsonMapper(this); + } + + /* + /********************************************************** + /* Life-cycle, builders + /********************************************************** + */ + + public static JsonMapper.Builder builder() { + return new Builder(new JsonMapper()); + } + + public static Builder builder(JsonFactory streamFactory) { + return new Builder(new JsonMapper(streamFactory)); + } + + /* + /********************************************************** + /* Standard method overrides + /********************************************************** + */ + + @Override + public Version version() { + return PackageVersion.VERSION; + } + + @Override + public JsonFactory getFactory() { + return _jsonFactory; + } +} diff --git a/src/test/java/com/fasterxml/jackson/databind/BaseMapTest.java b/src/test/java/com/fasterxml/jackson/databind/BaseMapTest.java index fe59c77b1b..b699a380fc 100644 --- a/src/test/java/com/fasterxml/jackson/databind/BaseMapTest.java +++ b/src/test/java/com/fasterxml/jackson/databind/BaseMapTest.java @@ -11,6 +11,7 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.deser.std.StdScalarDeserializer; +import com.fasterxml.jackson.databind.json.JsonMapper; import com.fasterxml.jackson.databind.ser.std.StdScalarSerializer; import com.fasterxml.jackson.databind.type.TypeFactory; @@ -232,15 +233,15 @@ protected static ObjectMapper newObjectMapper() { return new ObjectMapper(); } - // @since 2.10 - protected static ObjectMapper.Builder objectMapperBuilder() { + // @since 2.10s + protected static JsonMapper.Builder objectMapperBuilder() { /* 27-Aug-2018, tatu: Now this is weird -- with Java 7, we seem to need * explicit type parameters, but not with Java 8. * This need renders builder-approach pretty much useless for Java 7, * which is ok for 3.x (where baseline is Java 8), but potentially * problematic for 2.10. Oh well. */ - return (ObjectMapper.Builder) ObjectMapper.builder(); + return JsonMapper.builder(); } // @since 2.7