Skip to content

Commit

Permalink
Fixed #118
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Nov 24, 2020
1 parent 145631f commit d91d21d
Show file tree
Hide file tree
Showing 10 changed files with 146 additions and 85 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
import com.fasterxml.jackson.databind.ser.BeanPropertyWriter;
import com.fasterxml.jackson.databind.util.ClassUtil;

/**
* Intermediate base class that is used for concrete
Expand Down Expand Up @@ -77,7 +76,7 @@ public void assignSerializer(JsonSerializer<Object> ser) {
}
// 04-Oct-2015, tatu: To fix [module-afterburner#59], need to disable use of
// fully optimized variant
if (!isDefaultSerializer(ser)) {
if (!SerializerUtil.isDefaultSerializer(ser)) {
broken = true;
}
}
Expand Down Expand Up @@ -130,14 +129,4 @@ protected void _reportProblem(Object bean, Throwable e)
e.getClass().getName(), e.getMessage());
Logger.getLogger(OptimizedBeanPropertyWriter.class.getName()).log(Level.WARNING, msg, e);
}

/**
* Helper method used to check whether given serializer is the default
* serializer implementation: this is necessary to avoid overriding other
* kinds of deserializers.
*/
protected boolean isDefaultSerializer(JsonSerializer<?> ser)
{
return (ser == null) || ClassUtil.isJacksonStdImpl(ser);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
import com.fasterxml.jackson.databind.introspect.AnnotatedMember;
import com.fasterxml.jackson.databind.introspect.AnnotatedMethod;
import com.fasterxml.jackson.databind.ser.*;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.fasterxml.jackson.databind.util.ClassUtil;

import com.fasterxml.jackson.module.afterburner.util.MyClassLoader;

public class SerializerModifier extends BeanSerializerModifier
Expand Down Expand Up @@ -94,7 +93,7 @@ protected PropertyAccessorCollector findProperties(Class<?> beanClass,

// 30-Jul-2012, tatu: [#6]: Needs to skip custom serializers, if any.
if (bpw.hasSerializer()) {
if (!isDefaultSerializer(config, bpw.getSerializer())) {
if (!SerializerUtil.isDefaultSerializer(bpw.getSerializer())) {
continue;
}
}
Expand Down Expand Up @@ -156,23 +155,4 @@ protected PropertyAccessorCollector findProperties(Class<?> beanClass,
}
return collector;
}

/**
* Helper method used to check whether given serializer is the default
* serializer implementation: this is necessary to avoid overriding other
* kinds of serializers.
*/
protected boolean isDefaultSerializer(SerializationConfig config,
JsonSerializer<?> ser)
{
if (ClassUtil.isJacksonStdImpl(ser)) {
// 20-Nov-2020, tatu: As per [modules-base#117], need to consider
// one standard serializer that should not be replaced...
if (ser instanceof ToStringSerializer) {
return false;
}
return true;
}
return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.fasterxml.jackson.module.afterburner.ser;

import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.fasterxml.jackson.databind.util.ClassUtil;

/**
* Helper class that contains utility methods needed by various other classes
* in this package.
*
* @since 2.12
*/
class SerializerUtil
{
/**
* Helper method used to check whether given serializer is the default
* serializer implementation: this is necessary to avoid overriding other
* kinds of serializers.
*/
public static boolean isDefaultSerializer(JsonSerializer<?> ser)
{
if (ser == null) {
return true;
}
if (ClassUtil.isJacksonStdImpl(ser)) {
// 20-Nov-2020, tatu: As per [modules-base#117], need to consider
// one standard serializer that should not be replaced...
if (ser instanceof ToStringSerializer) {
return false;
}
return true;
}
return false;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,21 +1,34 @@
package com.fasterxml.jackson.module.afterburner.ser;

import com.fasterxml.jackson.annotation.JsonFormat;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;

import com.fasterxml.jackson.module.afterburner.AfterburnerTestBase;

// [modules-base#117]
public class JDKScalarsSerTest extends AfterburnerTestBase
{
// [modules-base#117]
static class Bean117UsingJsonSerialize {
@JsonSerialize(using = ToStringSerializer.class)
public int getValue() {
return 42;
}
}

// [modules-base#118]
static class Bean118IntUsingJsonFormat {
@JsonFormat(shape = JsonFormat.Shape.STRING)
public int value = 42;
}

static class Bean118LongUsingJsonFormat {
@JsonFormat(shape = JsonFormat.Shape.STRING)
public long value = -137L;
}

private final ObjectMapper MAPPER = newObjectMapper();
private final ObjectMapper VANILLA_MAPPER = newVanillaJSONMapper();

Expand All @@ -27,4 +40,22 @@ public void testIntAsStringWithJsonSerialize() throws Exception
assertEquals(EXP_JSON, VANILLA_MAPPER.writeValueAsString(input));
assertEquals(EXP_JSON, MAPPER.writeValueAsString(input));
}

// [modules-base#118]
public void testIntAsStringWithJsonFormat() throws Exception
{
final String EXP_JSON = "{\"value\":\"42\"}";
final Object input = new Bean118IntUsingJsonFormat();
assertEquals(EXP_JSON, VANILLA_MAPPER.writeValueAsString(input));
assertEquals(EXP_JSON, MAPPER.writeValueAsString(input));
}

// [modules-base#118]
public void testLongAsStringWithJsonFormat() throws Exception
{
final String EXP_JSON = "{\"value\":\"-137\"}";
final Object input = new Bean118LongUsingJsonFormat();
assertEquals(EXP_JSON, VANILLA_MAPPER.writeValueAsString(input));
assertEquals(EXP_JSON, MAPPER.writeValueAsString(input));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@
import com.fasterxml.jackson.databind.introspect.AnnotatedMember;
import com.fasterxml.jackson.databind.introspect.AnnotatedMethod;
import com.fasterxml.jackson.databind.ser.*;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.fasterxml.jackson.databind.util.ClassUtil;

import com.fasterxml.jackson.module.blackbird.util.ReflectionHack;
import com.fasterxml.jackson.module.blackbird.util.Unchecked;

Expand Down Expand Up @@ -83,7 +82,7 @@ protected void createProperty(ListIterator<BeanPropertyWriter> it, Lookup lookup
// (although, interestingly enough, can seem to access private classes...)

// 30-Jul-2012, tatu: [#6]: Needs to skip custom serializers, if any.
if (bpw.hasSerializer() && !isDefaultSerializer(config, bpw.getSerializer())) {
if (bpw.hasSerializer() && !SerializerUtil.isDefaultSerializer(bpw.getSerializer())) {
return;
}
// [#9]: also skip unwrapping stuff...
Expand Down Expand Up @@ -161,23 +160,4 @@ protected void createProperty(ListIterator<BeanPropertyWriter> it, Lookup lookup
}
}
}

/**
* Helper method used to check whether given serializer is the default
* serializer implementation: this is necessary to avoid overriding other
* kinds of serializers.
*/
protected boolean isDefaultSerializer(SerializationConfig config,
JsonSerializer<?> ser)
{
if (ClassUtil.isJacksonStdImpl(ser)) {
// 20-Nov-2020, tatu: As per [modules-base#117], need to consider
// one standard serializer that should not be replaced...
if (ser instanceof ToStringSerializer) {
return false;
}
return true;
}
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public void assignSerializer(JsonSerializer<Object> ser) {
}
// 04-Oct-2015, tatu: To fix [module-afterburner#59], need to disable use of
// fully optimized variant
if (!isDefaultSerializer(ser)) {
if (!SerializerUtil.isDefaultSerializer(ser)) {
broken = true;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.fasterxml.jackson.module.blackbird.ser;

import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.fasterxml.jackson.databind.util.ClassUtil;

/**
* Helper class that contains utility methods needed by various other classes
* in this package.
*
* @since 2.12
*/
class SerializerUtil
{
/**
* Helper method used to check whether given serializer is the default
* serializer implementation: this is necessary to avoid overriding other
* kinds of serializers.
*/
public static boolean isDefaultSerializer(JsonSerializer<?> ser)
{
if (ser == null) {
return true;
}
if (ClassUtil.isJacksonStdImpl(ser)) {
// 20-Nov-2020, tatu: As per [modules-base#117], need to consider
// one standard serializer that should not be replaced...
if (ser instanceof ToStringSerializer) {
return false;
}
return true;
}
return false;
}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,38 @@
package com.fasterxml.jackson.module.blackbird.ser;

import com.fasterxml.jackson.annotation.JsonFormat;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;

import com.fasterxml.jackson.module.blackbird.BlackbirdTestBase;

// [modules-base#117]
public class JDKScalarsSerTest extends BlackbirdTestBase
{
// [modules-base#117]
static class Bean117UsingJsonSerialize {
@JsonSerialize(using = ToStringSerializer.class)
public int getValue() {
return 42;
}
}

// [modules-base#118]
static class Bean118IntUsingJsonFormat {
@JsonFormat(shape = JsonFormat.Shape.STRING)
public int getValue() {
return 42;
}
}

static class Bean118LongUsingJsonFormat {
@JsonFormat(shape = JsonFormat.Shape.STRING)
public long getValue() {
return -137L;
}
}

private final ObjectMapper MAPPER = newObjectMapper();
private final ObjectMapper VANILLA_MAPPER = newVanillaJSONMapper();

Expand All @@ -27,4 +44,22 @@ public void testIntAsStringWithJsonSerialize() throws Exception
assertEquals(EXP_JSON, VANILLA_MAPPER.writeValueAsString(input));
assertEquals(EXP_JSON, MAPPER.writeValueAsString(input));
}

// [modules-base#118]
public void testIntAsStringWithJsonFormat() throws Exception
{
final String EXP_JSON = "{\"value\":\"42\"}";
final Object input = new Bean118IntUsingJsonFormat();
assertEquals(EXP_JSON, VANILLA_MAPPER.writeValueAsString(input));
assertEquals(EXP_JSON, MAPPER.writeValueAsString(input));
}

// [modules-base#118]
public void testLongAsStringWithJsonFormat() throws Exception
{
final String EXP_JSON = "{\"value\":\"-137\"}";
final Object input = new Bean118LongUsingJsonFormat();
assertEquals(EXP_JSON, VANILLA_MAPPER.writeValueAsString(input));
assertEquals(EXP_JSON, MAPPER.writeValueAsString(input));
}
}
2 changes: 2 additions & 0 deletions release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ Modules:
#117: (afterburner) Use of `ToStringSerializer` via `@JsonSerialize` on `int`/`long`
property does not work
(reported by wujimin@github)
#118: (afterburner) Using `@JsonFormat(shape = JsonFormat.Shape.STRING)` on `int`,
`long` properties not working
- Add Gradle Module Metadata (https://blog.gradle.org/alignment-with-gradle-module-metadata)

2.11.3 (02-Oct-2020)
Expand Down

0 comments on commit d91d21d

Please sign in to comment.