From 31df485a023364383353dabb97f9f0609ceddde6 Mon Sep 17 00:00:00 2001 From: tyler-ball Date: Tue, 7 Oct 2014 10:54:54 -0700 Subject: Making necessary C changes to only call to_json if it is present --- ext/ffi_yajl/ext/encoder/encoder.c | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) (limited to 'ext/ffi_yajl/ext') 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; } -- cgit v1.2.1