#include "state_lib.h" void rb_json_state_indent(state, json, depth) VALUE state, json; long depth; { VALUE indent = rb_json_state_get_intent(state); char* c_indent = StringValueCStr(indent); // Incrementing the depth char* full_indent; long i; for(i=0; ilen == 0) return Qfalse; return Qtrue; } /** * Creates a State object from _opts_, which ought to be Hash to create a * new State instance configured by opts, something else to create an * unconfigured instance. If _opts_ is a State object, it is just returned. */ VALUE rb_json_from_state(self, opts) VALUE self, opts; { if(rb_obj_is_kind_of(opts, rb_cJsonState)) { return opts; } else if(rb_obj_is_kind_of(opts, rb_cHash)) { return rb_funcall(self, rb_intern("new"), 1, opts); } else { return rb_funcall(self, rb_intern("new"), 0); } } /** * Returns _true_, if _object_ was already seen during this Unparsing run. */ VALUE rb_json_state_seen(self, object) VALUE self, object; { VALUE seen_ivar = rb_ivar_get(self, rb_intern("@seen")); return rb_hash_aref(seen_ivar, rb_obj_id(object)); } /** * Remember _object_, to find out if it was already encountered (to find out * if a cyclic data structure is unparsed). */ VALUE rb_json_state_remember(self, object) VALUE self, object; { VALUE seen_ivar = rb_ivar_get(self, rb_intern("@seen")); rb_hash_aset(seen_ivar, rb_obj_id(object), Qtrue); } /** * Forget _object_ for this Unparsing run. */ VALUE rb_json_state_forget(self, object) VALUE self, object; { VALUE seen_ivar = rb_ivar_get(self, rb_intern("@seen")); rb_hash_delete(seen_ivar, rb_obj_id(object)); } /** Static **/ static VALUE call_to_json(args) VALUE args; { VALUE rb_target = rb_ary_entry(args, 0); VALUE rb_state = rb_ary_entry(args, 1); VALUE rb_indent = rb_ary_entry(args, 2); return rb_funcall(rb_target, rb_intern("to_json"), 2, rb_state, rb_indent); } static VALUE to_json_exception(rb_target, rb_error) VALUE rb_target, rb_error; { VALUE rb_error_class = rb_obj_class(rb_error); if(rb_error_class == rb_eArgError) { return rb_funcall(rb_target, rb_intern("to_json"), 0); } else { rb_exc_raise(rb_error); } }