summaryrefslogtreecommitdiff
path: root/ext
diff options
context:
space:
mode:
authortyler-ball <tyleraball@gmail.com>2014-10-07 10:54:54 -0700
committertyler-ball <tyleraball@gmail.com>2014-10-07 10:54:54 -0700
commit31df485a023364383353dabb97f9f0609ceddde6 (patch)
treed2b7bb7e620640c02c2ac27a7b634c2b3c8d30af /ext
parent3af1ec06bd861bf0eb01863cbe02dffadbf45503 (diff)
downloadffi-yajl-31df485a023364383353dabb97f9f0609ceddde6.tar.gz
Making necessary C changes to only call to_json if it is present
Diffstat (limited to 'ext')
-rw-r--r--ext/ffi_yajl/ext/encoder/encoder.c20
1 files changed, 13 insertions, 7 deletions
diff --git a/ext/ffi_yajl/ext/encoder/encoder.c b/ext/ffi_yajl/ext/encoder/encoder.c
index 0c917cb..90b125b 100644
--- a/ext/ffi_yajl/ext/encoder/encoder.c
+++ b/ext/ffi_yajl/ext/encoder/encoder.c
@@ -272,14 +272,20 @@ static VALUE rb_cObject_ffi_yajl(VALUE self, VALUE rb_yajl_gen, VALUE state) {
yajl_gen_status status;
ID sym_to_json = rb_intern("to_json");
VALUE str;
- VALUE json_opts = rb_hash_aref(state, rb_str_new2("json_opts"));
- struct yajl_gen_t *yajl_gen;
- Data_Get_Struct(rb_yajl_gen, struct yajl_gen_t, yajl_gen);
- str = rb_funcall(self, sym_to_json, 1, json_opts);
- CHECK_STATUS(
- yajl_gen_number(yajl_gen, (char *)RSTRING_PTR(str), RSTRING_LEN(str))
- );
+ if ( rb_respond_to(self, sym_to_json) ) {
+ VALUE json_opts = rb_hash_aref(state, rb_str_new2("json_opts"));
+ struct yajl_gen_t *yajl_gen;
+ Data_Get_Struct(rb_yajl_gen, struct yajl_gen_t, yajl_gen);
+
+ str = rb_funcall(self, sym_to_json, 1, json_opts);
+ CHECK_STATUS(
+ yajl_gen_number(yajl_gen, (char *)RSTRING_PTR(str), RSTRING_LEN(str))
+ );
+ } else {
+ object_to_s_ffi_yajl(self, rb_yajl_gen, state);
+ }
+
return Qnil;
}