Skip to content

Commit

Permalink
check for existence of and use the #as_json method to serialize an ob…
Browse files Browse the repository at this point in the history
…ject (should speed up encoding in rails apps)
  • Loading branch information
brianmario committed Mar 10, 2010
1 parent 2220848 commit 9ebf80f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
6 changes: 5 additions & 1 deletion ext/yajl_ext.c
Expand Up @@ -173,7 +173,10 @@ void yajl_encode_part(void * wrapper, VALUE obj, VALUE io) {
status = yajl_gen_string(w->encoder, (const unsigned char *)cptr, len);
break;
default:
if (rb_respond_to(obj, intern_to_json)) {
if (rb_respond_to(obj, intern_as_json)) {
obj = rb_funcall(obj, intern_as_json, 0);
yajl_encode_part(w, obj, io);
} else if (rb_respond_to(obj, intern_to_json)) {
str = rb_funcall(obj, intern_to_json, 0);
cptr = RSTRING_PTR(str);
len = RSTRING_LEN(str);
Expand Down Expand Up @@ -886,6 +889,7 @@ void Init_yajl_ext() {
intern_to_json = rb_intern("to_json");
intern_to_sym = rb_intern("to_sym");
intern_has_key = rb_intern("has_key?");
intern_as_json = rb_intern("as_json");

sym_allow_comments = ID2SYM(rb_intern("allow_comments"));
sym_check_utf8 = ID2SYM(rb_intern("check_utf8"));
Expand Down
2 changes: 1 addition & 1 deletion ext/yajl_ext.h
Expand Up @@ -49,7 +49,7 @@ int utf8Encoding;

static VALUE cParseError, cEncodeError, mYajl, cParser, cEncoder;
static ID intern_io_read, intern_call, intern_keys, intern_to_s,
intern_to_json, intern_has_key, intern_to_sym;
intern_to_json, intern_has_key, intern_to_sym, intern_as_json;
static ID sym_allow_comments, sym_check_utf8, sym_pretty, sym_indent, sym_terminator, sym_symbolize_keys;

#define GetParser(obj, sval) (sval = (yajl_parser_wrapper*)DATA_PTR(obj));
Expand Down
15 changes: 13 additions & 2 deletions spec/encoding/encoding_spec.rb
Expand Up @@ -3,7 +3,13 @@

class Dummy2
def to_json
"hawtness".dump
"{\"hawtness\":true}"
end
end

class Dummy3
def as_json
{:hawtness => true}
end
end

Expand Down Expand Up @@ -152,7 +158,12 @@ def to_json

it "should check for and call #to_json if it exists on custom objects" do
d = Dummy2.new
Yajl::Encoder.encode({:foo => d}).should eql('{"foo":"hawtness"}')
Yajl::Encoder.encode({:foo => d}).should eql('{"foo":{"hawtness":true}}')
end

it "should check for and call #as_json if it exists on custom objects" do
d = Dummy3.new
Yajl::Encoder.encode(d).should eql('{"hawtness":true}')
end

it "should encode a hash where the key and value can be symbols" do
Expand Down

0 comments on commit 9ebf80f

Please sign in to comment.