Skip to content
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

NumberFormat support #4273

Open
wants to merge 4 commits into
base: 2.17
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import java.lang.reflect.Type;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.DecimalFormat;
import java.text.NumberFormat;

import com.fasterxml.jackson.annotation.JsonFormat;

Expand Down Expand Up @@ -38,13 +40,20 @@ public class NumberSerializer

protected final boolean _isInt;

protected final NumberFormat format;
hurelhuyag marked this conversation as resolved.
Show resolved Hide resolved

/**
* @since 2.5
*/
public NumberSerializer(Class<? extends Number> rawType) {
this(rawType, null);
}

public NumberSerializer(Class<? extends Number> rawType, NumberFormat format) {
super(rawType, false);
// since this will NOT be constructed for Integer or Long, only case is:
_isInt = (rawType == BigInteger.class);
this.format = format;
}

@Override
Expand All @@ -55,6 +64,9 @@ public JsonSerializer<?> createContextual(SerializerProvider prov,
if (format != null) {
switch (format.getShape()) {
case STRING:
if (format.hasPattern()) {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
if (format.hasPattern()) {
// [databind#1114] since 2.17 : Support @JsonFormat for String, numbers, using String.format()
if (format.hasPattern()) {

return new NumberSerializer(handledType(), new DecimalFormat(format.getPattern()));
Copy link
Member

Choose a reason for hiding this comment

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

Probably needs a try-catch block for invalid format (and probably unit test to show handling), re-throw exception (using one of methods from SerializerProvider).

}
// [databind#2264]: Need special handling for `BigDecimal`
if (((Class<?>) handledType()) == BigDecimal.class) {
return bigDecimalAsStringSerializer();
Expand All @@ -69,6 +81,10 @@ public JsonSerializer<?> createContextual(SerializerProvider prov,
@Override
public void serialize(Number value, JsonGenerator g, SerializerProvider provider) throws IOException
{
if (format != null) {
g.writeString(format.format(value));
hurelhuyag marked this conversation as resolved.
Show resolved Hide resolved
return;
}
// should mostly come in as one of these two:
if (value instanceof BigDecimal) {
g.writeNumber((BigDecimal) value);
Expand Down