Skip to content

Commit

Permalink
allowing symbolize_names to be used instead of symbolize_keys for com…
Browse files Browse the repository at this point in the history
…patibility with the json gem. brianmario#94
  • Loading branch information
srushti committed Jul 30, 2012
1 parent b08b3f7 commit bce57ea
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 3 deletions.
3 changes: 2 additions & 1 deletion ext/yajl/yajl_ext.c
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ static VALUE rb_yajl_parser_new(int argc, VALUE * argv, VALUE klass) {
if (rb_hash_aref(opts, sym_check_utf8) == Qfalse) {
checkUTF8 = 0;
}
if (rb_hash_aref(opts, sym_symbolize_keys) == Qtrue) {
if (rb_hash_aref(opts, sym_symbolize_keys) == Qtrue || rb_hash_aref(opts, sym_symbolize_names) == Qtrue) {
symbolizeKeys = 1;
}
}
Expand Down Expand Up @@ -898,6 +898,7 @@ void Init_yajl() {
sym_html_safe = ID2SYM(rb_intern("html_safe"));
sym_terminator = ID2SYM(rb_intern("terminator"));
sym_symbolize_keys = ID2SYM(rb_intern("symbolize_keys"));
sym_symbolize_names = ID2SYM(rb_intern("symbolize_names"));

#ifdef HAVE_RUBY_ENCODING_H
utf8Encoding = rb_utf8_encoding();
Expand Down
4 changes: 2 additions & 2 deletions ext/yajl/yajl_ext.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ static rb_encoding *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_as_json;
static ID sym_allow_comments, sym_check_utf8, sym_pretty, sym_indent, sym_terminator, sym_symbolize_keys, sym_html_safe;
static ID sym_allow_comments, sym_check_utf8, sym_pretty, sym_indent, sym_terminator, sym_symbolize_keys, sym_symbolize_names, sym_html_safe;

#define GetParser(obj, sval) (sval = (yajl_parser_wrapper*)DATA_PTR(obj));
#define GetEncoder(obj, sval) (sval = (yajl_encoder_wrapper*)DATA_PTR(obj));
Expand Down Expand Up @@ -132,4 +132,4 @@ static VALUE rb_yajl_json_ext_false_to_json(int argc, VALUE * argv, VALUE self);
static VALUE rb_yajl_json_ext_nil_to_json(int argc, VALUE * argv, VALUE self);
static VALUE rb_yajl_encoder_enable_json_gem_ext(VALUE klass);

void Init_yajl();
void Init_yajl();
4 changes: 4 additions & 0 deletions spec/json_gem_compatibility/compatibility_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ class Dummy; end
JSON.default_options[:symbolize_keys] = default # ensure the rest of the test cases expect the default
end

it "should also allow the json gem's symbolize_names key" do
JSON.parse('{"foo": 1234}', :symbolize_names => true).should === {:foo => 1234}
end

it "should encode arbitrary classes via their default to_json method" do
d = Dummy.new
d.to_json.should == "\"#{d.to_s}\""
Expand Down

0 comments on commit bce57ea

Please sign in to comment.