-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
NumberFormat support #4273
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
base: 2.17
Are you sure you want to change the base?
NumberFormat support #4273
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -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; | ||||||||
|
||||||||
|
@@ -38,13 +40,20 @@ public class NumberSerializer | |||||||
|
||||||||
protected final boolean _isInt; | ||||||||
|
||||||||
protected final NumberFormat format; | ||||||||
|
||||||||
/** | ||||||||
* @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 | ||||||||
|
@@ -55,6 +64,9 @@ public JsonSerializer<?> createContextual(SerializerProvider prov, | |||||||
if (format != null) { | ||||||||
switch (format.getShape()) { | ||||||||
case STRING: | ||||||||
if (format.hasPattern()) { | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
return new NumberSerializer(handledType(), new DecimalFormat(format.getPattern())); | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||||||||
} | ||||||||
// [databind#2264]: Need special handling for `BigDecimal` | ||||||||
if (((Class<?>) handledType()) == BigDecimal.class) { | ||||||||
return bigDecimalAsStringSerializer(); | ||||||||
|
@@ -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); | ||||||||
|
Uh oh!
There was an error while loading. Please reload this page.