Skip to content

Commit

Permalink
Fix #2153
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Oct 10, 2018
1 parent 3f94218 commit 89a2025
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 49 deletions.
1 change: 1 addition & 0 deletions release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
46 changes: 0 additions & 46 deletions src/main/java/com/fasterxml/jackson/databind/ObjectMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<ObjectMapper, Builder>
{
public Builder(JsonFactory tsf) {
super(new ObjectMapper(tsf));
}
}

/**
* Enumeration used with {@link ObjectMapper#enableDefaultTyping()}
* to specify what kind of types (classes) default typing should
Expand Down Expand Up @@ -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:
*<pre>
* return builder(JsonFactory.builder().build());
*</pre>
*
* @since 2.10
*/
@SuppressWarnings("unchecked")
public static <M extends ObjectMapper, B extends MapperBuilder<M,B>> MapperBuilder<M,B> builder() {
return (MapperBuilder<M,B>) 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
Expand Down
84 changes: 84 additions & 0 deletions src/main/java/com/fasterxml/jackson/databind/json/JsonMapper.java
Original file line number Diff line number Diff line change
@@ -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<JsonMapper, Builder>
{
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;
}
}
7 changes: 4 additions & 3 deletions src/test/java/com/fasterxml/jackson/databind/BaseMapTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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.<ObjectMapper,ObjectMapper.Builder>builder();
return JsonMapper.builder();
}

// @since 2.7
Expand Down

0 comments on commit 89a2025

Please sign in to comment.