Skip to content
Browse files

allowing symbolize_names to be used instead of symbolize_keys for com…

…patibility with the json gem. #94
  • Loading branch information...
1 parent b08b3f7 commit bce57ea7c44bf206fe5bcc779f83bdc8be57d7c3 @srushti srushti committed Jul 30, 2012
Showing with 8 additions and 3 deletions.
  1. +2 −1 ext/yajl/yajl_ext.c
  2. +2 −2 ext/yajl/yajl_ext.h
  3. +4 −0 spec/json_gem_compatibility/compatibility_spec.rb
View
3 ext/yajl/yajl_ext.c
@@ -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;
}
}
@@ -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();
View
4 ext/yajl/yajl_ext.h
@@ -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));
@@ -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();
View
4 spec/json_gem_compatibility/compatibility_spec.rb
@@ -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}\""

0 comments on commit bce57ea

Please sign in to comment.
Something went wrong with that request. Please try again.