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