summaryrefslogtreecommitdiff
path: root/lib/ffi_yajl
diff options
context:
space:
mode:
authorLamont Granquist <lamont@scriptkiddie.org>2014-10-07 15:58:18 -0700
committerLamont Granquist <lamont@scriptkiddie.org>2014-10-07 15:58:18 -0700
commit6e19eb693350f198f9b18609c3f1a89b3138b79e (patch)
tree41e85ca3d6b4dfa79b63c0d31f07116b8f141a18 /lib/ffi_yajl
parentc5d0f4c89fa5a493fd9638d232f70088b4d0556e (diff)
parentd713d2681aba154fc83d41b32bbdbb99c2055b61 (diff)
downloadffi-yajl-6e19eb693350f198f9b18609c3f1a89b3138b79e.tar.gz
Merge pull request #25 from tyler-ball/tball/remove_to_json
If an object does not have .to_json, we no longer try to call it
Diffstat (limited to 'lib/ffi_yajl')
-rw-r--r--lib/ffi_yajl/ffi/encoder.rb11
1 files changed, 9 insertions, 2 deletions
diff --git a/lib/ffi_yajl/ffi/encoder.rb b/lib/ffi_yajl/ffi/encoder.rb
index 3df366c..8177f5f 100644
--- a/lib/ffi_yajl/ffi/encoder.rb
+++ b/lib/ffi_yajl/ffi/encoder.rb
@@ -201,8 +201,15 @@ end
# I feel dirty
class Object
def ffi_yajl(yajl_gen, state)
- json = self.to_json(state[:json_opts])
- if ( status = FFI_Yajl.yajl_gen_number(yajl_gen, json, json.bytesize) ) != 0
+ if self.respond_to?(:to_json)
+ json = self.to_json(state[:json_opts])
+ # #yajl_gen_number outputs a string without quotes around it
+ status = FFI_Yajl.yajl_gen_number(yajl_gen, json, json.bytesize)
+ else
+ str = self.to_s
+ status = FFI_Yajl.yajl_gen_string(yajl_gen, str, str.bytesize)
+ end
+ if ( status ) != 0
FFI_Yajl::Encoder.raise_error_for_status(status)
end
end