summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorLamont Granquist <lamont@scriptkiddie.org>2014-08-11 09:07:25 -0700
committerLamont Granquist <lamont@scriptkiddie.org>2014-08-23 11:16:14 -0700
commit5d35f31762750169569eb5ae9ce51b9af8a91793 (patch)
tree1f23d0cd156fc05791bc16d9d105868477c189b2 /lib
parent3759f4e96d3800260e3d942dd225867c13e78b33 (diff)
downloadffi-yajl-5d35f31762750169569eb5ae9ce51b9af8a91793.tar.gz
add datetime encoding
necessary for ohai json_gem spec complies with JSON gem format. this also arguably fixes issues with yajl-ruby where it does not encode date objects correctly.
Diffstat (limited to 'lib')
-rw-r--r--lib/ffi_yajl/ffi/encoder.rb28
-rw-r--r--lib/ffi_yajl/json_gem.rb19
2 files changed, 45 insertions, 2 deletions
diff --git a/lib/ffi_yajl/ffi/encoder.rb b/lib/ffi_yajl/ffi/encoder.rb
index 3bad69f..b82c0fe 100644
--- a/lib/ffi_yajl/ffi/encoder.rb
+++ b/lib/ffi_yajl/ffi/encoder.rb
@@ -171,6 +171,33 @@ class String
end
end
+class Date
+ def ffi_yajl(yajl_gen, state)
+ str = self.to_s
+ 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 Time
+ def ffi_yajl(yajl_gen, state)
+ str = self.to_s
+ 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 DateTime
+ def ffi_yajl(yajl_gen, state)
+ str = self.to_s
+ if ( status = FFI_Yajl.yajl_gen_string(yajl_gen, str, str.bytesize) ) != 0
+ FFI_Yajl::Encoder.raise_error_for_status(status)
+ end
+ end
+end
+
# I feel dirty
class Object
def ffi_yajl(yajl_gen, state)
@@ -180,4 +207,3 @@ class Object
end
end
end
-
diff --git a/lib/ffi_yajl/json_gem.rb b/lib/ffi_yajl/json_gem.rb
index 6a3b139..47586b6 100644
--- a/lib/ffi_yajl/json_gem.rb
+++ b/lib/ffi_yajl/json_gem.rb
@@ -101,6 +101,24 @@ class NilClass
end
end
+class Date
+ def to_json(*opts, &block)
+ FFI_Yajl::Encoder.encode(self, *opts)
+ end
+end
+
+class Time
+ def to_json(*opts, &block)
+ FFI_Yajl::Encoder.encode(self, *opts)
+ end
+end
+
+class DateTime
+ def to_json(*opts, &block)
+ FFI_Yajl::Encoder.encode(self, *opts)
+ end
+end
+
module ::Kernel
def JSON(object, opts = {})
if object.respond_to? :to_s
@@ -118,4 +136,3 @@ class Object
end
end
end
-