diff options
author | lamont-granquist <lamont@scriptkiddie.org> | 2014-08-08 07:47:11 -0700 |
---|---|---|
committer | lamont-granquist <lamont@scriptkiddie.org> | 2014-08-08 07:47:11 -0700 |
commit | 1d7e20d6e87eccc3bef089f98551133ad0b145f2 (patch) | |
tree | 6da74b088d77bd8b95d8fee94706979b051ca4d7 | |
parent | 46239c54700ad1e06a6ac86eaf0fbcb141ce3eb1 (diff) | |
parent | a2b5da35cbeaf953779cf0877c0f212e3f1374d0 (diff) | |
download | ffi-yajl-1d7e20d6e87eccc3bef089f98551133ad0b145f2.tar.gz |
Merge pull request #4 from opscode/lcg/json-opts
Lcg/json opts
-rw-r--r-- | .travis.yml | 2 | ||||
-rw-r--r-- | ext/ffi_yajl/ext/encoder/encoder.c | 6 | ||||
-rw-r--r-- | lib/ffi_yajl/encoder.rb | 2 | ||||
-rw-r--r-- | lib/ffi_yajl/ffi/encoder.rb | 3 | ||||
-rw-r--r-- | lib/ffi_yajl/json_gem.rb | 16 | ||||
-rw-r--r-- | spec/ffi_yajl/json_gem_spec.rb | 11 |
6 files changed, 27 insertions, 13 deletions
diff --git a/.travis.yml b/.travis.yml index 88d0d44..d96b355 100644 --- a/.travis.yml +++ b/.travis.yml @@ -28,6 +28,8 @@ jdk: - openjdk6 matrix: allow_failures: + - rvm: rbx-2.2 + jdk: oraclejdk7 - rvm: ruby-head jdk: oraclejdk7 - rvm: jruby-head diff --git a/ext/ffi_yajl/ext/encoder/encoder.c b/ext/ffi_yajl/ext/encoder/encoder.c index 5576881..26ae368 100644 --- a/ext/ffi_yajl/ext/encoder/encoder.c +++ b/ext/ffi_yajl/ext/encoder/encoder.c @@ -9,7 +9,7 @@ static VALUE cYajl_Gen; #define CHECK_STATUS(call) \ if ((status = (call)) != yajl_gen_status_ok) { rb_funcall(mEncoder2, rb_intern("raise_error_for_status"), 1, INT2FIX(status)); } -static VALUE mEncoder_do_yajl_encode(VALUE self, VALUE obj, VALUE yajl_gen_opts) { +static VALUE mEncoder_do_yajl_encode(VALUE self, VALUE obj, VALUE yajl_gen_opts, VALUE json_opts) { ID sym_ffi_yajl = rb_intern("ffi_yajl"); VALUE sym_yajl_gen_beautify = ID2SYM(rb_intern("yajl_gen_beautify")); VALUE sym_yajl_gen_validate_utf8 = ID2SYM(rb_intern("yajl_gen_validate_utf8")); @@ -42,6 +42,8 @@ static VALUE mEncoder_do_yajl_encode(VALUE self, VALUE obj, VALUE yajl_gen_opts) rb_hash_aset(state, rb_str_new2("processing_key"), Qfalse); + rb_hash_aset(state, rb_str_new2("json_opts"), json_opts); + rb_yajl_gen = Data_Wrap_Struct(cYajl_Gen, NULL, NULL, yajl_gen); rb_funcall(obj, sym_ffi_yajl, 2, rb_yajl_gen, state); @@ -261,7 +263,7 @@ void Init_encoder() { mExt = rb_define_module_under(mFFI_Yajl, "Ext"); mEncoder = rb_define_module_under(mExt, "Encoder"); cYajl_Gen = rb_define_class_under(mEncoder, "YajlGen", rb_cObject); - rb_define_method(mEncoder, "do_yajl_encode", mEncoder_do_yajl_encode, 2); + rb_define_method(mEncoder, "do_yajl_encode", mEncoder_do_yajl_encode, 3); rb_define_method(rb_cHash, "ffi_yajl", rb_cHash_ffi_yajl, 2); rb_define_method(rb_cArray, "ffi_yajl", rb_cArray_ffi_yajl, 2); diff --git a/lib/ffi_yajl/encoder.rb b/lib/ffi_yajl/encoder.rb index 8b5bfcb..bd3ff31 100644 --- a/lib/ffi_yajl/encoder.rb +++ b/lib/ffi_yajl/encoder.rb @@ -18,7 +18,7 @@ module FFI_Yajl end # call either the ext or ffi hook - str = do_yajl_encode(obj, yajl_gen_opts) + str = do_yajl_encode(obj, yajl_gen_opts, opts) str.force_encoding('UTF-8') if defined? Encoding str end diff --git a/lib/ffi_yajl/ffi/encoder.rb b/lib/ffi_yajl/ffi/encoder.rb index bbeba27..3bad69f 100644 --- a/lib/ffi_yajl/ffi/encoder.rb +++ b/lib/ffi_yajl/ffi/encoder.rb @@ -4,8 +4,7 @@ require 'ffi_yajl/ffi' module FFI_Yajl module FFI module Encoder - def do_yajl_encode(obj, yajl_gen_opts = {}) - + def do_yajl_encode(obj, yajl_gen_opts, opts) yajl_gen = FFI_Yajl.yajl_gen_alloc(nil); # configure the yajl encoder diff --git a/lib/ffi_yajl/json_gem.rb b/lib/ffi_yajl/json_gem.rb index 0e64d50..6a3b139 100644 --- a/lib/ffi_yajl/json_gem.rb +++ b/lib/ffi_yajl/json_gem.rb @@ -55,49 +55,49 @@ end class Array def to_json(*opts, &block) - FFI_Yajl::Encoder.encode(self) + FFI_Yajl::Encoder.encode(self, *opts) end end class Hash def to_json(*opts, &block) - FFI_Yajl::Encoder.encode(self) + FFI_Yajl::Encoder.encode(self, *opts) end end class Fixnum def to_json(*opts, &block) - FFI_Yajl::Encoder.encode(self) + FFI_Yajl::Encoder.encode(self, *opts) end end class Float def to_json(*opts, &block) - FFI_Yajl::Encoder.encode(self) + FFI_Yajl::Encoder.encode(self, *opts) end end class String def to_json(*opts, &block) - FFI_Yajl::Encoder.encode(self) + FFI_Yajl::Encoder.encode(self, *opts) end end class TrueClass def to_json(*opts, &block) - FFI_Yajl::Encoder.encode(self) + FFI_Yajl::Encoder.encode(self, *opts) end end class FalseClass def to_json(*opts, &block) - FFI_Yajl::Encoder.encode(self) + FFI_Yajl::Encoder.encode(self, *opts) end end class NilClass def to_json(*opts, &block) - FFI_Yajl::Encoder.encode(self) + FFI_Yajl::Encoder.encode(self, *opts) end end diff --git a/spec/ffi_yajl/json_gem_spec.rb b/spec/ffi_yajl/json_gem_spec.rb index d342fd3..36233eb 100644 --- a/spec/ffi_yajl/json_gem_spec.rb +++ b/spec/ffi_yajl/json_gem_spec.rb @@ -97,6 +97,17 @@ describe "JSON Gem Compat API" do expect(JSON.pretty_generate({'foo' => 1234}, {})).to eql("{\n \"foo\": 1234\n}") end + class Foo + def to_json(*a) + {'foo' => 1234}.to_json(*a) + end + end + + it "JSON#pretty_generate should work with an Object that implements #to_json" do + f = Foo.new + expect(JSON.pretty_generate(f)).to eql("{\n \"foo\": 1234\n}\n") + end + context "when setting symbolize_keys via JSON.default_options" do after(:each) { JSON.default_options[:symbolize_keys] = false } |