Skip to content

Commit 7a138c7

Browse files
committed
Fix #117
1 parent a7043ba commit 7a138c7

File tree

10 files changed

+130
-59
lines changed

10 files changed

+130
-59
lines changed

afterburner/src/main/java/com/fasterxml/jackson/module/afterburner/ser/SerializerModifier.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import com.fasterxml.jackson.databind.introspect.AnnotatedMember;
99
import com.fasterxml.jackson.databind.introspect.AnnotatedMethod;
1010
import com.fasterxml.jackson.databind.ser.*;
11+
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
1112
import com.fasterxml.jackson.databind.util.ClassUtil;
1213
import com.fasterxml.jackson.module.afterburner.util.MyClassLoader;
1314

@@ -119,7 +120,6 @@ protected PropertyAccessorCollector findProperties(Class<?> beanClass,
119120

120121
if (type.isPrimitive()) {
121122
if (type == Integer.TYPE) {
122-
123123
if (isMethod) {
124124
it.set(collector.addIntGetter(bpw));
125125
} else {
@@ -165,6 +165,14 @@ protected PropertyAccessorCollector findProperties(Class<?> beanClass,
165165
protected boolean isDefaultSerializer(SerializationConfig config,
166166
JsonSerializer<?> ser)
167167
{
168-
return ClassUtil.isJacksonStdImpl(ser);
168+
if (ClassUtil.isJacksonStdImpl(ser)) {
169+
// 20-Nov-2020, tatu: As per [modules-base#117], need to consider
170+
// one standard serializer that should not be replaced...
171+
if (ser instanceof ToStringSerializer) {
172+
return false;
173+
}
174+
return true;
175+
}
176+
return false;
169177
}
170178
}

afterburner/src/test/java/com/fasterxml/jackson/module/afterburner/AfterburnerTestBase.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,11 @@ protected static JsonMapper.Builder mapperBuilder() {
156156
.addModule(new AfterburnerModule());
157157
}
158158

159+
// One withOUT afterburner module
160+
protected static JsonMapper newVanillaJSONMapper() {
161+
return new JsonMapper();
162+
}
163+
159164
@Deprecated
160165
protected ObjectMapper mapperWithModule()
161166
{

afterburner/src/test/java/com/fasterxml/jackson/module/afterburner/deser/jdk/JDKScalarsTest.java renamed to afterburner/src/test/java/com/fasterxml/jackson/module/afterburner/deser/jdk/JDKScalarsDeserTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* Unit tests for verifying handling of simple basic non-structured
1616
* types; primitives (and/or their wrappers), Strings.
1717
*/
18-
public class JDKScalarsTest
18+
public class JDKScalarsDeserTest
1919
extends AfterburnerTestBase
2020
{
2121
final static String NAN_STRING = "NaN";
@@ -103,7 +103,7 @@ static class WrappersBean
103103
public Double doubleValue;
104104
}
105105

106-
private final ObjectMapper MAPPER = new ObjectMapper();
106+
private final ObjectMapper MAPPER = newObjectMapper();
107107

108108
/*
109109
/**********************************************************
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.fasterxml.jackson.module.afterburner.failing;
2+
3+
import com.fasterxml.jackson.annotation.JsonFormat;
4+
5+
import com.fasterxml.jackson.databind.ObjectMapper;
6+
7+
import com.fasterxml.jackson.module.afterburner.AfterburnerTestBase;
8+
9+
public class JsonFormatForSer117Test extends AfterburnerTestBase
10+
{
11+
static class Bean117UsingJsonFormat {
12+
@JsonFormat(shape = JsonFormat.Shape.STRING)
13+
public int value = 42;
14+
}
15+
16+
private final ObjectMapper MAPPER = newObjectMapper();
17+
private final ObjectMapper VANILLA_MAPPER = newVanillaJSONMapper();
18+
19+
public void testIntAsStringWithJsonFormat() throws Exception
20+
{
21+
final String EXP_JSON = "{\"value\":\"42\"}";
22+
final Object input = new Bean117UsingJsonFormat();
23+
assertEquals(EXP_JSON, VANILLA_MAPPER.writeValueAsString(input));
24+
assertEquals(EXP_JSON, MAPPER.writeValueAsString(input));
25+
}
26+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.fasterxml.jackson.module.afterburner.ser;
2+
3+
import com.fasterxml.jackson.databind.ObjectMapper;
4+
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
5+
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
6+
7+
import com.fasterxml.jackson.module.afterburner.AfterburnerTestBase;
8+
9+
// [modules-base#117]
10+
public class JDKScalarsSerTest extends AfterburnerTestBase
11+
{
12+
static class Bean117UsingJsonSerialize {
13+
@JsonSerialize(using = ToStringSerializer.class)
14+
public int getValue() {
15+
return 42;
16+
}
17+
}
18+
19+
private final ObjectMapper MAPPER = newObjectMapper();
20+
private final ObjectMapper VANILLA_MAPPER = newVanillaJSONMapper();
21+
22+
// [modules-base#117]
23+
public void testIntAsStringWithJsonSerialize() throws Exception
24+
{
25+
final String EXP_JSON = "{\"value\":\"42\"}";
26+
final Object input = new Bean117UsingJsonSerialize();
27+
assertEquals(EXP_JSON, VANILLA_MAPPER.writeValueAsString(input));
28+
assertEquals(EXP_JSON, MAPPER.writeValueAsString(input));
29+
}
30+
}

blackbird/src/main/java/com/fasterxml/jackson/module/blackbird/ser/BBSerializerModifier.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import com.fasterxml.jackson.databind.introspect.AnnotatedMember;
1818
import com.fasterxml.jackson.databind.introspect.AnnotatedMethod;
1919
import com.fasterxml.jackson.databind.ser.*;
20+
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
2021
import com.fasterxml.jackson.databind.util.ClassUtil;
2122
import com.fasterxml.jackson.module.blackbird.util.ReflectionHack;
2223
import com.fasterxml.jackson.module.blackbird.util.Unchecked;
@@ -82,7 +83,7 @@ protected void createProperty(ListIterator<BeanPropertyWriter> it, Lookup lookup
8283
// (although, interestingly enough, can seem to access private classes...)
8384

8485
// 30-Jul-2012, tatu: [#6]: Needs to skip custom serializers, if any.
85-
if (bpw.hasSerializer() && !ClassUtil.isJacksonStdImpl(bpw.getSerializer())) {
86+
if (bpw.hasSerializer() && !isDefaultSerializer(config, bpw.getSerializer())) {
8687
return;
8788
}
8889
// [#9]: also skip unwrapping stuff...
@@ -160,4 +161,23 @@ protected void createProperty(ListIterator<BeanPropertyWriter> it, Lookup lookup
160161
}
161162
}
162163
}
164+
165+
/**
166+
* Helper method used to check whether given serializer is the default
167+
* serializer implementation: this is necessary to avoid overriding other
168+
* kinds of serializers.
169+
*/
170+
protected boolean isDefaultSerializer(SerializationConfig config,
171+
JsonSerializer<?> ser)
172+
{
173+
if (ClassUtil.isJacksonStdImpl(ser)) {
174+
// 20-Nov-2020, tatu: As per [modules-base#117], need to consider
175+
// one standard serializer that should not be replaced...
176+
if (ser instanceof ToStringSerializer) {
177+
return false;
178+
}
179+
return true;
180+
}
181+
return false;
182+
}
163183
}

blackbird/src/test/java/com/fasterxml/jackson/module/blackbird/BlackbirdTestBase.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,10 @@ protected static JsonMapper.Builder mapperBuilder() {
157157
.addModule(new BlackbirdModule(MethodHandles::lookup));
158158
}
159159

160+
protected static JsonMapper newVanillaJSONMapper() {
161+
return new JsonMapper();
162+
}
163+
160164
/*
161165
/**********************************************************
162166
/* Helper methods; assertions
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.fasterxml.jackson.module.blackbird.ser;
2+
3+
import com.fasterxml.jackson.databind.ObjectMapper;
4+
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
5+
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
6+
7+
import com.fasterxml.jackson.module.blackbird.BlackbirdTestBase;
8+
9+
// [modules-base#117]
10+
public class JDKScalarsSerTest extends BlackbirdTestBase
11+
{
12+
static class Bean117UsingJsonSerialize {
13+
@JsonSerialize(using = ToStringSerializer.class)
14+
public int getValue() {
15+
return 42;
16+
}
17+
}
18+
19+
private final ObjectMapper MAPPER = newObjectMapper();
20+
private final ObjectMapper VANILLA_MAPPER = newVanillaJSONMapper();
21+
22+
// [modules-base#117]
23+
public void testIntAsStringWithJsonSerialize() throws Exception
24+
{
25+
final String EXP_JSON = "{\"value\":\"42\"}";
26+
final Object input = new Bean117UsingJsonSerialize();
27+
assertEquals(EXP_JSON, VANILLA_MAPPER.writeValueAsString(input));
28+
assertEquals(EXP_JSON, MAPPER.writeValueAsString(input));
29+
}
30+
}

mrbean/src/test/java/com/fasterxml/jackson/module/mrbean/Issue117Test.java

Lines changed: 0 additions & 54 deletions
This file was deleted.

release-notes/VERSION-2.x

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ Modules:
2020
#100: (mrbean) Prevent "double-prefixing" Mr Bean generated classes
2121
#115: (jaxb) Remove ` java.beans.Introspector` dependency from JAXB module (to
2222
get rid of `java.desktop` module dep)
23+
#117: Use of `ToStringSerializer` via `@JsonSerialize` on `int`/`long` property does not work
24+
(reported by wujimin@github)
2325
- Add Gradle Module Metadata (https://blog.gradle.org/alignment-with-gradle-module-metadata)
2426

2527
2.11.3 (02-Oct-2020)

0 commit comments

Comments
 (0)