Skip to content

test case for serializing custom numbers with config overrides #5198

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: 3.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,9 @@ public ValueSerializer<Object> createSerializer(SerializationContext ctxt, JavaT
beanDescRef = ctxt.lazyIntrospectBeanDescription(type);
}
}
if (formatOverrides == null) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like patching a bug further upstream? I'll have to think about this bit more -- after vacation (going on vacation today), so it'll be some time until I get back, but will go through it then.

formatOverrides = _calculateEffectiveFormat(ctxt, beanDescRef, type.getRawClass(), JsonFormat.Value.empty());
}
// Slight detour: do we have a Converter to consider?
Converter<Object,Object> conv = config.findSerializationConverter(beanDescRef.getClassInfo());
if (conv != null) { // yup, need converter
Expand Down
25 changes: 25 additions & 0 deletions src/test/java/tools/jackson/databind/ser/jdk/NumberSerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import tools.jackson.databind.SerializationContext;
import tools.jackson.databind.ValueSerializer;
import tools.jackson.databind.annotation.JsonSerialize;
import tools.jackson.databind.json.JsonMapper;
import tools.jackson.databind.module.SimpleModule;
import tools.jackson.databind.testutil.DatabindTestUtil;

Expand Down Expand Up @@ -117,6 +118,12 @@ public void serialize(BigDecimal value, JsonGenerator gen, SerializationContext
}
}

static class MyBigDecimal extends BigDecimal {
public MyBigDecimal(String value) {
super(value);
}
}

/*
/**********************************************************
/* Test methods
Expand Down Expand Up @@ -225,6 +232,24 @@ public void testCustomSerializationBigDecimalAsNumber() throws Exception {
assertEquals(a2q("{'value':2.0}"), mapper.writeValueAsString(new BigDecimalHolder("2")));
}

@Test
public void testConfigOverrideJdkNumber() throws Exception {
JsonMapper mapper = jsonMapperBuilder().withConfigOverride(BigDecimal.class,
c -> c.setFormat(JsonFormat.Value.forShape(JsonFormat.Shape.STRING)))
.build();
String value = mapper.writeValueAsString(new BigDecimal("123.456"));
assertEquals(a2q("'123.456'"), value);
}

@Test
public void testConfigOverrideNonJdkNumber() throws Exception {
JsonMapper mapper = jsonMapperBuilder().withConfigOverride(MyBigDecimal.class,
c -> c.setFormat(JsonFormat.Value.forShape(JsonFormat.Shape.STRING)))
.build();
String value = mapper.writeValueAsString(new MyBigDecimal("123.456"));
assertEquals(a2q("'123.456'"), value);
}

// default locale is en_US
static DecimalFormat createDecimalFormatForDefaultLocale(final String pattern) {
return new DecimalFormat(pattern, new DecimalFormatSymbols(Locale.ENGLISH));
Expand Down