diff options
author | Lamont Granquist <lamont@scriptkiddie.org> | 2014-01-11 15:01:30 -0800 |
---|---|---|
committer | Lamont Granquist <lamont@scriptkiddie.org> | 2014-01-11 15:01:30 -0800 |
commit | 4b2c15cb8773e259c79af8eb51a81c093c8a04d1 (patch) | |
tree | db8cab4b82f3faec8b667808cb0e45357c5e7b92 | |
parent | 006775725816c5c50f51018940e23ed6f73d7e2f (diff) | |
download | ffi-yajl-0.0.2.tar.gz |
convert symbols to strings when encoding0.0.2
-rw-r--r-- | ext/ffi_yajl/ext/encoder/encoder.c | 14 | ||||
-rw-r--r-- | lib/ffi_yajl/ffi/encoder.rb | 9 | ||||
-rw-r--r-- | spec/ffi_yajl/encoder_spec.rb | 10 |
3 files changed, 32 insertions, 1 deletions
diff --git a/ext/ffi_yajl/ext/encoder/encoder.c b/ext/ffi_yajl/ext/encoder/encoder.c index c57cb49..6edbe6a 100644 --- a/ext/ffi_yajl/ext/encoder/encoder.c +++ b/ext/ffi_yajl/ext/encoder/encoder.c @@ -197,7 +197,6 @@ static VALUE rb_cFloat_ffi_yajl(VALUE self, VALUE yajl_gen, VALUE state) { return Qnil; } - static VALUE rb_cString_ffi_yajl(VALUE self, VALUE yajl_gen, VALUE state) { yajl_gen_status status; CHECK_STATUS( @@ -206,6 +205,18 @@ static VALUE rb_cString_ffi_yajl(VALUE self, VALUE yajl_gen, VALUE state) { return Qnil; } +static VALUE rb_cSymbol_ffi_yajl(VALUE self, VALUE yajl_gen, VALUE state) { + yajl_gen_status status; + ID sym_to_s = rb_intern("to_s"); + VALUE str = rb_funcall(self, sym_to_s, 0); + char *cptr = RSTRING_PTR(str); + int len = RSTRING_LEN(str); + CHECK_STATUS( + yajl_gen_string((struct yajl_gen_t *) yajl_gen, (unsigned char *)cptr, len) + ); + return Qnil; +} + static VALUE rb_cObject_ffi_yajl(VALUE self, VALUE yajl_gen, VALUE state) { yajl_gen_status status; ID sym_to_json = rb_intern("to_json"); @@ -235,6 +246,7 @@ void Init_encoder() { rb_define_method(rb_cBignum, "ffi_yajl", rb_cBignum_ffi_yajl, 2); rb_define_method(rb_cFloat, "ffi_yajl", rb_cFloat_ffi_yajl, 2); rb_define_method(rb_cString, "ffi_yajl", rb_cString_ffi_yajl, 2); + rb_define_method(rb_cSymbol, "ffi_yajl", rb_cSymbol_ffi_yajl, 2); rb_define_method(rb_cObject, "ffi_yajl", rb_cObject_ffi_yajl, 2); } diff --git a/lib/ffi_yajl/ffi/encoder.rb b/lib/ffi_yajl/ffi/encoder.rb index 13fd7fb..bbeba27 100644 --- a/lib/ffi_yajl/ffi/encoder.rb +++ b/lib/ffi_yajl/ffi/encoder.rb @@ -155,6 +155,15 @@ class Float end end +class Symbol + def ffi_yajl(yajl_gen, state) + str = self.to_s + if ( status = FFI_Yajl.yajl_gen_string(yajl_gen, str, str.bytesize) ) != 0 + FFI_Yajl::Encoder.raise_error_for_status(status) + end + end +end + class String def ffi_yajl(yajl_gen, state) if ( status = FFI_Yajl.yajl_gen_string(yajl_gen, self, self.bytesize) ) != 0 diff --git a/spec/ffi_yajl/encoder_spec.rb b/spec/ffi_yajl/encoder_spec.rb index 2a7b102..443e987 100644 --- a/spec/ffi_yajl/encoder_spec.rb +++ b/spec/ffi_yajl/encoder_spec.rb @@ -35,5 +35,15 @@ describe "FFI_Yajl::Encoder" do 127.times {|_| a["a"] = {}; a = a["a"] } expect{ encoder.encode(root) }.to raise_error(FFI_Yajl::EncodeError) end + + it "encodes symbols in keys as strings" do + ruby = {:thing => 1} + expect(encoder.encode(ruby)).to eq('{"thing":1}') + end + + it "encodes symbols in values as strings" do + ruby = {"thing" => :one} + expect(encoder.encode(ruby)).to eq('{"thing":"one"}') + end end |