Skip to content

Commit

Permalink
Fix guyboertje#59 Add an option to serialize BigDecimal as String
Browse files Browse the repository at this point in the history
  • Loading branch information
frankwong15 committed Jan 3, 2020
1 parent d2144d6 commit 5704a80
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/main/java/com/jrjackson/JrJacksonBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public static IRubyObject generate(ThreadContext context, IRubyObject self, IRub
jgen.useDefaultPrettyPrinter();
}

Boolean stringifyBigDecimal = flagged(options, RubyUtils.rubySymbol(_ruby, "stringify_bigdecimal"));
SerializerProvider provider;
if (format != null) {
SimpleDateFormat simpleFormat = new SimpleDateFormat(format);
Expand All @@ -67,7 +68,7 @@ public static IRubyObject generate(ThreadContext context, IRubyObject self, IRub
}

try {
RubyAnySerializer.instance.serialize(args[0], jgen, provider);
new RubyAnySerializer(stringifyBigDecimal).serialize(args[0], jgen, provider);
jgen.close();
ByteList bl = new ByteList(baos.toByteArray(),
UTF8Encoding.INSTANCE);
Expand Down
13 changes: 11 additions & 2 deletions src/main/java/com/jrjackson/RubyAnySerializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,14 @@ public class RubyAnySerializer extends JsonSerializer<IRubyObject> {

private static final RUBYCLASS[] CLASS_NAMES = RUBYCLASS.values();

private final Boolean stringifyBigDecimal;

public RubyAnySerializer() {
// super(IRubyObject.class);
this(false);
}

public RubyAnySerializer(Boolean stringifyBigDecimal) {
this.stringifyBigDecimal = stringifyBigDecimal;
}

private void serializeUnknownRubyObject(ThreadContext ctx, IRubyObject rubyObject, JsonGenerator jgen, SerializerProvider provider)
Expand Down Expand Up @@ -190,7 +195,11 @@ public void serialize(IRubyObject value, JsonGenerator jgen, SerializerProvider
}
break;
case BigDecimal:
jgen.writeNumber(((RubyBigDecimal) value).getBigDecimalValue());
if (this.stringifyBigDecimal) {
jgen.writeString(value.callMethod(value.getRuntime().getCurrentContext(), "to_s", value.getRuntime().newString("F")).toString());
} else {
jgen.writeNumber(((RubyBigDecimal) value).getBigDecimalValue());
}
break;
case Time:
serializeTime((RubyTime) value, jgen, provider);
Expand Down
10 changes: 10 additions & 0 deletions test/jrjackson_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,16 @@ def test_concurrent_dump

# -----------------------------

def test_can_serialize_bigdecimal_as_string
object = {"foo" => BigDecimal.new('0.12345678901234567890123456789')}

actual = JrJackson::Json.dump(object)
assert_equal "{\"foo\":0.12345678901234567890123456789}", actual

actual = JrJackson::Json.dump(object, :stringify_bigdecimal => true)
assert_equal "{\"foo\":\"0.12345678901234567890123456789\"}", actual
end

def assert_bigdecimal_equal(expected, actual)
assert_equal expected, actual
assert_equal expected.class, actual.class
Expand Down

0 comments on commit 5704a80

Please sign in to comment.