diff options
author | Lamont Granquist <lamont@scriptkiddie.org> | 2014-11-24 18:25:35 -0800 |
---|---|---|
committer | Lamont Granquist <lamont@scriptkiddie.org> | 2014-11-24 18:25:35 -0800 |
commit | a502020b5403170c236b7488d56e718df84d537f (patch) | |
tree | 3ce843f82cdc0dc7e7ff3e8efe2b968e48bd653a | |
parent | 9d466797cbeb13a098dbaccf366eec495c842830 (diff) | |
parent | f1042fdb86eeb8be05c8ac2976d15d2235eddb3c (diff) | |
download | ffi-yajl-a502020b5403170c236b7488d56e718df84d537f.tar.gz |
Merge pull request #34 from opscode/lcg/object-as-key
Lcg/object as key
-rw-r--r-- | CHANGELOG.md | 8 | ||||
-rw-r--r-- | ext/ffi_yajl/ext/encoder/encoder.c | 2 | ||||
-rw-r--r-- | lib/ffi_yajl/ffi/encoder.rb | 2 | ||||
-rw-r--r-- | spec/ffi_yajl/encoder_spec.rb | 17 |
4 files changed, 27 insertions, 2 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index eed081a..309e1f3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,14 @@ ## master (unreleased) ### New features +### Changes +### Bugs fixed + +* fixes using Objects as Hash keys + +## 1.3.0 (11/22/2014) + +### New features * [**Jason Potkanski**](https://github.com/electrawn): Cygwin detection diff --git a/ext/ffi_yajl/ext/encoder/encoder.c b/ext/ffi_yajl/ext/encoder/encoder.c index 3c22f8d..b43d02b 100644 --- a/ext/ffi_yajl/ext/encoder/encoder.c +++ b/ext/ffi_yajl/ext/encoder/encoder.c @@ -345,7 +345,7 @@ static VALUE rb_cObject_ffi_yajl(VALUE self, VALUE rb_yajl_gen, VALUE state) { ID sym_to_json = rb_intern("to_json"); VALUE str; - if ( rb_respond_to(self, sym_to_json) ) { + if ( rb_hash_aref(state, rb_str_new2("processing_key")) != Qtrue && 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); diff --git a/lib/ffi_yajl/ffi/encoder.rb b/lib/ffi_yajl/ffi/encoder.rb index add35f6..af1f1a6 100644 --- a/lib/ffi_yajl/ffi/encoder.rb +++ b/lib/ffi_yajl/ffi/encoder.rb @@ -236,7 +236,7 @@ end # I feel dirty class Object def ffi_yajl(yajl_gen, state) - if self.respond_to?(:to_json) + if !state[:processing_key] && 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) diff --git a/spec/ffi_yajl/encoder_spec.rb b/spec/ffi_yajl/encoder_spec.rb index 809f5b5..5c9e6b7 100644 --- a/spec/ffi_yajl/encoder_spec.rb +++ b/spec/ffi_yajl/encoder_spec.rb @@ -47,6 +47,23 @@ describe "FFI_Yajl::Encoder" do expect(encoder.encode(ruby)).to eq('{"12345678901234567890":2}') end + it "encodes objects in keys as strings" do + o = Object.new + ruby = { o => 2 } + expect(encoder.encode(ruby)).to eq(%Q{{"#{o.to_s}":2}}) + end + + it "encodes an object in a key which has a #to_json method as strings" do + class Thing + def to_json(*a) + "{}" + end + end + o = Thing.new + ruby = { o => 2 } + expect(encoder.encode(ruby)).to eq(%Q{{"#{o.to_s}":2}}) + end + # XXX: 127 == YAJL_MAX_DEPTH hardcodedness, zero control for us, it isn't even a twiddleable #define it "raises an exception for deeply nested arrays" do root = [] |