summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ext/ffi_yajl/ext/encoder/encoder.c6
-rw-r--r--lib/ffi_yajl/encoder.rb2
-rw-r--r--lib/ffi_yajl/ffi/encoder.rb2
-rw-r--r--lib/ffi_yajl/json_gem.rb18
-rw-r--r--spec/ffi_yajl/json_gem_spec.rb11
5 files changed, 27 insertions, 12 deletions
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..8837e12 100644
--- a/lib/ffi_yajl/ffi/encoder.rb
+++ b/lib/ffi_yajl/ffi/encoder.rb
@@ -4,7 +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);
diff --git a/lib/ffi_yajl/json_gem.rb b/lib/ffi_yajl/json_gem.rb
index 0e64d50..4773f7f 100644
--- a/lib/ffi_yajl/json_gem.rb
+++ b/lib/ffi_yajl/json_gem.rb
@@ -25,6 +25,8 @@ module JSON
options_map = {}
options_map[:pretty] = true
options_map[:indent] = opts[:indent] if opts.has_key?(:indent)
+ require 'pp'
+ pp options_map
FFI_Yajl::Encoder.encode(obj, options_map).chomp
rescue FFI_Yajl::EncodeError => e
raise JSON::GeneratorError, e.message
@@ -55,49 +57,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 }