diff options
-rw-r--r-- | ext/ffi_yajl/ext/encoder/encoder.c | 12 | ||||
-rw-r--r-- | lib/ffi_yajl/ffi/encoder.rb | 20 | ||||
-rw-r--r-- | spec/ffi_yajl/encoder_spec.rb | 24 |
3 files changed, 50 insertions, 6 deletions
diff --git a/ext/ffi_yajl/ext/encoder/encoder.c b/ext/ffi_yajl/ext/encoder/encoder.c index f4999b8..da4ea17 100644 --- a/ext/ffi_yajl/ext/encoder/encoder.c +++ b/ext/ffi_yajl/ext/encoder/encoder.c @@ -136,7 +136,11 @@ static VALUE rb_cBignum_ffi_yajl(VALUE self, VALUE yajl_gen, VALUE state) { if (memcmp(cptr, "NaN", 3) == 0 || memcmp(cptr, "Infinity", 8) == 0 || memcmp(cptr, "-Infinity", 9) == 0) { rb_raise(cEncodeError, "'%s' is an invalid number", cptr); } - yajl_gen_number((struct yajl_gen_t *) yajl_gen, cptr, len); + if ( ((ffi_state_t *)state)->processing_key ) { + yajl_gen_string((struct yajl_gen_t *) yajl_gen, (unsigned char *)cptr, len); + } else { + yajl_gen_number((struct yajl_gen_t *) yajl_gen, cptr, len); + } return Qnil; } @@ -148,7 +152,11 @@ static VALUE rb_cFloat_ffi_yajl(VALUE self, VALUE yajl_gen, VALUE state) { if (memcmp(cptr, "NaN", 3) == 0 || memcmp(cptr, "Infinity", 8) == 0 || memcmp(cptr, "-Infinity", 9) == 0) { rb_raise(cEncodeError, "'%s' is an invalid number", cptr); } - yajl_gen_number((struct yajl_gen_t *) yajl_gen, cptr, len); + if ( ((ffi_state_t *)state)->processing_key ) { + yajl_gen_string((struct yajl_gen_t *) yajl_gen, (unsigned char *)cptr, len); + } else { + yajl_gen_number((struct yajl_gen_t *) yajl_gen, cptr, len); + } return Qnil; } diff --git a/lib/ffi_yajl/ffi/encoder.rb b/lib/ffi_yajl/ffi/encoder.rb index c84dbea..eaf52cd 100644 --- a/lib/ffi_yajl/ffi/encoder.rb +++ b/lib/ffi_yajl/ffi/encoder.rb @@ -120,8 +120,14 @@ end class Bignum def ffi_yajl(yajl_gen, state) str = self.to_s - if ( status = FFI_Yajl.yajl_gen_number(yajl_gen, str, str.bytesize) ) != 0 - FFI_Yajl::Encoder.raise_error_for_status(status) + if state[:processing_key] + if ( status = FFI_Yajl.yajl_gen_string(yajl_gen, str, str.bytesize) ) != 0 + FFI_Yajl::Encoder.raise_error_for_status(status) + end + else + if ( status = FFI_Yajl.yajl_gen_number(yajl_gen, str, str.bytesize) ) != 0 + FFI_Yajl::Encoder.raise_error_for_status(status) + end end end end @@ -129,8 +135,14 @@ end class Float def ffi_yajl(yajl_gen, state) str = self.to_s - if ( status = FFI_Yajl.yajl_gen_number(yajl_gen, str, str.bytesize) ) != 0 - FFI_Yajl::Encoder.raise_error_for_status(status) + if state[:processing_key] + if ( status = FFI_Yajl.yajl_gen_string(yajl_gen, str, str.bytesize) ) != 0 + FFI_Yajl::Encoder.raise_error_for_status(status) + end + else + if ( status = FFI_Yajl.yajl_gen_number(yajl_gen, str, str.bytesize) ) != 0 + FFI_Yajl::Encoder.raise_error_for_status(status) + end end end end diff --git a/spec/ffi_yajl/encoder_spec.rb b/spec/ffi_yajl/encoder_spec.rb new file mode 100644 index 0000000..96dfe1a --- /dev/null +++ b/spec/ffi_yajl/encoder_spec.rb @@ -0,0 +1,24 @@ + + +require 'spec_helper' + +describe "FFI_Yajl::Encoder" do + + let(:encoder) { FFI_Yajl::Encoder.new } + + it "encodes fixnums in keys as strings" do + ruby = {1 => 2} + expect(encoder.encode(ruby)).to eq('{"1":2}') + end + + it "encodes floats in keys as strings" do + ruby = {1.1 => 2} + expect(encoder.encode(ruby)).to eq('{"1.1":2}') + end + + it "encodes bignums in keys as strings" do + ruby = {12345678901234567890 => 2} + expect(encoder.encode(ruby)).to eq('{"12345678901234567890":2}') + end +end + |