summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFlorian Frank <flori@ping.de>2016-02-25 00:10:42 +0100
committerFlorian Frank <flori@ping.de>2016-02-25 00:10:42 +0100
commit5fd6c4e168005c68995a97ef436725a01c42dfd9 (patch)
tree1da6af113b806ce9a0355da9a737d3b5e387f294
parentd2a22c030a039d3833a13361c8d0cc2f75e94e5c (diff)
downloadjson-5fd6c4e168005c68995a97ef436725a01c42dfd9.tar.gz
Call OBJECT_HANDLER for objects without #to_json
as a last-ditch effort.
-rw-r--r--java/src/json/ext/Generator.java13
1 files changed, 8 insertions, 5 deletions
diff --git a/java/src/json/ext/Generator.java b/java/src/json/ext/Generator.java
index d9be209..bb3a394 100644
--- a/java/src/json/ext/Generator.java
+++ b/java/src/json/ext/Generator.java
@@ -427,11 +427,14 @@ public final class Generator {
new Handler<IRubyObject>() {
@Override
RubyString generateNew(Session session, IRubyObject object) {
- IRubyObject result =
- object.callMethod(session.getContext(), "to_json",
- new IRubyObject[] {session.getState()});
- if (result instanceof RubyString) return (RubyString)result;
- throw session.getRuntime().newTypeError("to_json must return a String");
+ if (object.respondsTo("to_json")) {
+ IRubyObject result = object.callMethod(session.getContext(), "to_json",
+ new IRubyObject[] {session.getState()});
+ if (result instanceof RubyString) return (RubyString)result;
+ throw session.getRuntime().newTypeError("to_json must return a String");
+ } else {
+ return OBJECT_HANDLER.generateNew(session, object);
+ }
}
@Override