summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/ffi_yajl/ffi/encoder.rb9
-rw-r--r--spec/ffi_yajl/encoder_spec.rb2
2 files changed, 10 insertions, 1 deletions
diff --git a/lib/ffi_yajl/ffi/encoder.rb b/lib/ffi_yajl/ffi/encoder.rb
index 6e168fe..73a0d11 100644
--- a/lib/ffi_yajl/ffi/encoder.rb
+++ b/lib/ffi_yajl/ffi/encoder.rb
@@ -227,6 +227,15 @@ class String
end
end
+class StringIO
+ def ffi_yajl(yajl_gen, state)
+ str = self.read
+ if ( status = FFI_Yajl.yajl_gen_string(yajl_gen, str, str.bytesize) ) != 0
+ FFI_Yajl::Encoder.raise_error_for_status(status)
+ end
+ end
+end
+
class Date
def ffi_yajl(yajl_gen, state)
str = self.to_s
diff --git a/spec/ffi_yajl/encoder_spec.rb b/spec/ffi_yajl/encoder_spec.rb
index 6fbff9a..0eef23d 100644
--- a/spec/ffi_yajl/encoder_spec.rb
+++ b/spec/ffi_yajl/encoder_spec.rb
@@ -133,7 +133,7 @@ describe "FFI_Yajl::Encoder" do
it "can encode StringIOs" do
ruby = { "foo" => StringIO.new('THING') }
- expect(encoder.encode(ruby)).to eq(nil)
+ expect(encoder.encode(ruby)).to eq("{\"foo\":\"THING\"}")
end
context "when encoding Time objects in UTC timezone" do