diff options
-rw-r--r-- | lib/ffi_yajl/ffi/encoder.rb | 4 | ||||
-rw-r--r-- | lib/ffi_yajl/json_gem.rb | 48 | ||||
-rw-r--r-- | spec/ffi_yajl/json_gem_spec.rb | 36 |
3 files changed, 84 insertions, 4 deletions
diff --git a/lib/ffi_yajl/ffi/encoder.rb b/lib/ffi_yajl/ffi/encoder.rb index 058fc2b..02d5586 100644 --- a/lib/ffi_yajl/ffi/encoder.rb +++ b/lib/ffi_yajl/ffi/encoder.rb @@ -88,7 +88,7 @@ end class TrueClass def ffi_yajl(yajl_gen, state) - if ( status = FFI_Yajl.yajl_gen_bool(yajl_gen, 0) ) != 0 + if ( status = FFI_Yajl.yajl_gen_bool(yajl_gen, 1) ) != 0 FFI_Yajl::Encoder.raise_error_for_status(status) end end @@ -96,7 +96,7 @@ end class FalseClass def ffi_yajl(yajl_gen, state) - if ( status = FFI_Yajl.yajl_gen_bool(yajl_gen, 1) ) != 0 + if ( status = FFI_Yajl.yajl_gen_bool(yajl_gen, 0) ) != 0 FFI_Yajl::Encoder.raise_error_for_status(status) end end diff --git a/lib/ffi_yajl/json_gem.rb b/lib/ffi_yajl/json_gem.rb index f743520..a04e557 100644 --- a/lib/ffi_yajl/json_gem.rb +++ b/lib/ffi_yajl/json_gem.rb @@ -53,6 +53,54 @@ module JSON end end +class Array + def to_json(*opts, &block) + FFI_Yajl::Encoder.encode(self) + end +end + +class Hash + def to_json(*opts, &block) + FFI_Yajl::Encoder.encode(self) + end +end + +class Fixnum + def to_json(*opts, &block) + FFI_Yajl::Encoder.encode(self) + end +end + +class Float + def to_json(*opts, &block) + FFI_Yajl::Encoder.encode(self) + end +end + +class String + def to_json(*opts, &block) + FFI_Yajl::Encoder.encode(self) + end +end + +class TrueClass + def to_json(*opts, &block) + FFI_Yajl::Encoder.encode(self) + end +end + +class FalseClass + def to_json(*opts, &block) + FFI_Yajl::Encoder.encode(self) + end +end + +class NilClass + def to_json(*opts, &block) + FFI_Yajl::Encoder.encode(self) + end +end + module ::Kernel def JSON(object, opts = {}) if object.respond_to? :to_s diff --git a/spec/ffi_yajl/json_gem_spec.rb b/spec/ffi_yajl/json_gem_spec.rb index be35331..5bbf3bd 100644 --- a/spec/ffi_yajl/json_gem_spec.rb +++ b/spec/ffi_yajl/json_gem_spec.rb @@ -183,11 +183,9 @@ describe "JSON Gem Compat API" do shared_examples_for "handling utf8 correctly" do it "should encode the content correctly" do - pending "FIXME" expect(utf8.to_json).to eq(json) end it "should parse the content correctly" do - pending "FIXME" expect(JSON.parse(json)).to eq(utf8) end end @@ -236,6 +234,40 @@ describe "JSON Gem Compat API" do end + context "when encoding basic types with #to_json" do + it "Array#to_json should work" do + expect([ "a", "b", "c" ].to_json).to eq(%Q{["a","b","c"]}) + end + + it "Hash#to_json should work" do + expect({"a"=>"b"}.to_json).to eq(%Q{{"a":"b"}}) + end + + it "Fixnum#to_json should work" do + expect(1.to_json).to eq("1") + end + + it "Float#to_json should work" do + expect(1.1.to_json).to eq("1.1") + end + + it "String#to_json should work" do + expect("foo".to_json).to eq(%Q{"foo"}) + end + + it "TrueClass#to_json should work" do + expect(true.to_json).to eq("true") + end + + it "FalseClass#to_json should work" do + expect(false.to_json).to eq("false") + end + + it "NilClass#to_json should work" do + expect(nil.to_json).to eq("null") + end + end + context "ported tests for generation" do before(:all) do @hash = { |