Skip to content

Commit

Permalink
Correctly symbolize multibyte characters.
Browse files Browse the repository at this point in the history
  • Loading branch information
Roger Braun committed Aug 7, 2011
1 parent 4621ddf commit eb6877d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
6 changes: 5 additions & 1 deletion ext/yajl/yajl_ext.c
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,11 @@ static int yajl_found_hash_key(void * ctx, const unsigned char * stringVal, unsi
char buf[stringLen+1];
memcpy(buf, stringVal, stringLen);
buf[stringLen] = 0;
yajl_set_static_value(ctx, ID2SYM(rb_intern(buf)));
VALUE stringEncoded = rb_str_new2(buf);
int enc = rb_enc_find_index("UTF-8");
rb_enc_associate_index(stringEncoded, enc);

yajl_set_static_value(ctx, ID2SYM(rb_to_id(stringEncoded)));
} else {
keyStr = rb_str_new((const char *)stringVal, stringLen);
#ifdef HAVE_RUBY_ENCODING_H
Expand Down
8 changes: 6 additions & 2 deletions spec/parsing/one_off_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,14 @@
Yajl::Parser.parse(io).should == {"key" => 1234}
end

it "should parse using it's class method, from an IO with symbolized keys" do
it "should parse using it's class method, from a string with symbolized keys" do
Yajl::Parser.parse('{"key": 1234}', :symbolize_keys => true).should == {:key => 1234}
end

it "should parse using it's class method, from a utf-8 string with multibyte characters, with symbolized keys" do
Yajl::Parser.parse('{"日本語": 1234}', :symbolize_keys => true).should == {:日本語 => 1234}
end

it "should parse using it's class method, from a string" do
Yajl::Parser.parse('{"key": 1234}').should == {"key" => 1234}
end
Expand Down Expand Up @@ -78,4 +82,4 @@
Yajl::Parser.parse('{"key": "value"}').values.first.encoding.should eql(Encoding.default_internal)
end
end
end
end

0 comments on commit eb6877d

Please sign in to comment.