diff options
author | Lamont Granquist <lamont@scriptkiddie.org> | 2014-08-11 09:07:25 -0700 |
---|---|---|
committer | Lamont Granquist <lamont@scriptkiddie.org> | 2014-08-23 11:16:14 -0700 |
commit | 5d35f31762750169569eb5ae9ce51b9af8a91793 (patch) | |
tree | 1f23d0cd156fc05791bc16d9d105868477c189b2 /ext | |
parent | 3759f4e96d3800260e3d942dd225867c13e78b33 (diff) | |
download | ffi-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 'ext')
-rw-r--r-- | ext/ffi_yajl/ext/encoder/encoder.c | 29 |
1 files changed, 26 insertions, 3 deletions
diff --git a/ext/ffi_yajl/ext/encoder/encoder.c b/ext/ffi_yajl/ext/encoder/encoder.c index 26ae368..371752b 100644 --- a/ext/ffi_yajl/ext/encoder/encoder.c +++ b/ext/ffi_yajl/ext/encoder/encoder.c @@ -2,6 +2,7 @@ #include <yajl/yajl_gen.h> static VALUE mFFI_Yajl, mExt, mEncoder, mEncoder2, cEncodeError; +static VALUE cDate, cTime, cDateTime; static VALUE cYajl_Gen; /* FIXME: the json gem does a whole bunch of indirection around monkeypatching... not sure if we need to as well... */ @@ -62,7 +63,6 @@ int rb_cHash_ffi_yajl_callback(VALUE key, VALUE val, VALUE extra) { VALUE state = rb_hash_aref(extra, rb_str_new2("state")); VALUE rb_yajl_gen = rb_hash_aref(extra, rb_str_new2("yajl_gen")); - rb_hash_aset(state, rb_str_new2("processing_key"), Qtrue); rb_funcall(key, sym_ffi_yajl, 2, rb_yajl_gen, state); rb_hash_aset(state, rb_str_new2("processing_key"), Qfalse); @@ -227,7 +227,8 @@ static VALUE rb_cString_ffi_yajl(VALUE self, VALUE rb_yajl_gen, VALUE state) { return Qnil; } -static VALUE rb_cSymbol_ffi_yajl(VALUE self, VALUE rb_yajl_gen, VALUE state) { +/* calls #to_s on an object to encode it */ +static VALUE object_to_s_ffi_yajl(VALUE self, VALUE rb_yajl_gen, VALUE state) { yajl_gen_status status; ID sym_to_s = rb_intern("to_s"); VALUE str = rb_funcall(self, sym_to_s, 0); @@ -241,6 +242,22 @@ static VALUE rb_cSymbol_ffi_yajl(VALUE self, VALUE rb_yajl_gen, VALUE state) { return Qnil; } +static VALUE rb_cSymbol_ffi_yajl(VALUE self, VALUE rb_yajl_gen, VALUE state) { + return object_to_s_ffi_yajl(self, rb_yajl_gen, state); +} + +static VALUE rb_cDate_ffi_yajl(VALUE self, VALUE rb_yajl_gen, VALUE state) { + return object_to_s_ffi_yajl(self, rb_yajl_gen, state); +} + +static VALUE rb_cTime_ffi_yajl(VALUE self, VALUE rb_yajl_gen, VALUE state) { + return object_to_s_ffi_yajl(self, rb_yajl_gen, state); +} + +static VALUE rb_cDateTime_ffi_yajl(VALUE self, VALUE rb_yajl_gen, VALUE state) { + return object_to_s_ffi_yajl(self, rb_yajl_gen, state); +} + static VALUE rb_cObject_ffi_yajl(VALUE self, VALUE rb_yajl_gen, VALUE state) { yajl_gen_status status; ID sym_to_json = rb_intern("to_json"); @@ -265,6 +282,10 @@ void Init_encoder() { cYajl_Gen = rb_define_class_under(mEncoder, "YajlGen", rb_cObject); rb_define_method(mEncoder, "do_yajl_encode", mEncoder_do_yajl_encode, 3); + cDate = rb_define_class("Date", rb_cObject); + cTime = rb_define_class("Time", rb_cObject); + cDateTime = rb_define_class("DateTime", cDate); + rb_define_method(rb_cHash, "ffi_yajl", rb_cHash_ffi_yajl, 2); rb_define_method(rb_cArray, "ffi_yajl", rb_cArray_ffi_yajl, 2); rb_define_method(rb_cNilClass, "ffi_yajl", rb_cNilClass_ffi_yajl, 2); @@ -275,6 +296,8 @@ void Init_encoder() { rb_define_method(rb_cFloat, "ffi_yajl", rb_cFloat_ffi_yajl, 2); rb_define_method(rb_cString, "ffi_yajl", rb_cString_ffi_yajl, 2); rb_define_method(rb_cSymbol, "ffi_yajl", rb_cSymbol_ffi_yajl, 2); + rb_define_method(cDate, "ffi_yajl", rb_cDate_ffi_yajl, 2); + rb_define_method(cTime, "ffi_yajl", rb_cTime_ffi_yajl, 2); + rb_define_method(cDateTime, "ffi_yajl", rb_cDateTime_ffi_yajl, 2); rb_define_method(rb_cObject, "ffi_yajl", rb_cObject_ffi_yajl, 2); } - |