#include #include static VALUE mFFI_Yajl, mExt, mEncoder, mEncoder2, cEncodeError; static VALUE cDate, cTime, cDateTime, cStringIO; static VALUE cYajl_Gen; /* FIXME: the json gem does a whole bunch of indirection around monkeypatching... not sure if we need to as well... */ #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, 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")); VALUE sym_yajl_gen_indent_string = ID2SYM(rb_intern("yajl_gen_indent_string")); yajl_gen yajl_gen; const unsigned char *buf; size_t len; VALUE state; VALUE ret; VALUE indent_string; VALUE rb_yajl_gen; yajl_gen = yajl_gen_alloc(NULL); if ( rb_hash_aref(yajl_gen_opts, sym_yajl_gen_beautify) == Qtrue ) { yajl_gen_config(yajl_gen, yajl_gen_beautify, 1); } if ( rb_hash_aref(yajl_gen_opts, sym_yajl_gen_validate_utf8) == Qtrue ) { yajl_gen_config(yajl_gen, yajl_gen_validate_utf8, 1); } indent_string = rb_hash_aref(yajl_gen_opts, sym_yajl_gen_indent_string); if (indent_string != Qnil) { yajl_gen_config(yajl_gen, yajl_gen_indent_string, RSTRING_PTR(indent_string)); } else { yajl_gen_config(yajl_gen, yajl_gen_indent_string, " "); } state = rb_hash_new(); 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); yajl_gen_get_buf(yajl_gen, &buf, &len); ret = rb_str_new2((char *)buf); yajl_gen_free(yajl_gen); return ret; } int rb_cHash_ffi_yajl_callback(VALUE key, VALUE val, VALUE extra) { ID sym_ffi_yajl = rb_intern("ffi_yajl"); 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); rb_funcall(val, sym_ffi_yajl, 2, rb_yajl_gen, state); return 0; } static VALUE rb_cHash_ffi_yajl(VALUE self, VALUE rb_yajl_gen, VALUE state) { yajl_gen_status status; VALUE extra; struct yajl_gen_t *yajl_gen; Data_Get_Struct(rb_yajl_gen, struct yajl_gen_t, yajl_gen); if ( rb_hash_aref(state, rb_str_new2("processing_key")) == Qtrue ) { ID sym_to_s = rb_intern("to_s"); VALUE str = rb_funcall(self, sym_to_s, 0); char *cptr = RSTRING_PTR(str); int len = RSTRING_LEN(str); CHECK_STATUS( yajl_gen_string(yajl_gen, (unsigned char *)cptr, len) ); } else { extra = rb_hash_new(); /* FIXME: reduce garbage */ rb_hash_aset(extra, rb_str_new2("yajl_gen"), rb_yajl_gen); rb_hash_aset(extra, rb_str_new2("state"), state); CHECK_STATUS( yajl_gen_map_open(yajl_gen) ); rb_hash_foreach(self, rb_cHash_ffi_yajl_callback, extra); CHECK_STATUS( yajl_gen_map_close(yajl_gen) ); } return Qnil; } static VALUE rb_cArray_ffi_yajl(VALUE self, VALUE rb_yajl_gen, VALUE state) { yajl_gen_status status; ID sym_ffi_yajl = rb_intern("ffi_yajl"); long i; VALUE val; struct yajl_gen_t *yajl_gen; Data_Get_Struct(rb_yajl_gen, struct yajl_gen_t, yajl_gen); if ( rb_hash_aref(state, rb_str_new2("processing_key")) == Qtrue ) { ID sym_to_s = rb_intern("to_s"); VALUE str = rb_funcall(self, sym_to_s, 0); char *cptr = RSTRING_PTR(str); int len = RSTRING_LEN(str); CHECK_STATUS( yajl_gen_string(yajl_gen, (unsigned char *)cptr, len) ); } else { CHECK_STATUS( yajl_gen_array_open(yajl_gen) ); for(i=0; i