Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a :sort_keys option to Encoder #136

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion ext/yajl/yajl_ext.c
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,9 @@ void yajl_encode_part(void * wrapper, VALUE obj, VALUE io) {

/* TODO: itterate through keys in the hash */
keys = rb_funcall(obj, intern_keys, 0);
if (w->sortKeys) {
keys = rb_funcall(keys, intern_sort, 0);
}
for(idx=0; idx<RARRAY_LEN(keys); idx++) {
entry = rb_ary_entry(keys, idx);
keyStr = rb_funcall(entry, intern_to_s, 0); /* key must be a string */
Expand Down Expand Up @@ -590,7 +593,7 @@ static VALUE rb_yajl_encoder_new(int argc, VALUE * argv, VALUE klass) {
yajl_gen_config cfg;
VALUE opts, obj, indent;
unsigned char *indentString = NULL, *actualIndent = NULL;
int beautify = 0, htmlSafe = 0;
int beautify = 0, htmlSafe = 0, sortKeys = 0;

/* Scan off config vars */
if (rb_scan_args(argc, argv, "01", &opts) == 1) {
Expand All @@ -613,6 +616,9 @@ static VALUE rb_yajl_encoder_new(int argc, VALUE * argv, VALUE klass) {
if (rb_hash_aref(opts, sym_html_safe) == Qtrue) {
htmlSafe = 1;
}
if (rb_hash_aref(opts, sym_sort_keys) == Qtrue) {
sortKeys = 1;
}
}
if (!indentString) {
indentString = defaultIndentString;
Expand All @@ -623,6 +629,7 @@ static VALUE rb_yajl_encoder_new(int argc, VALUE * argv, VALUE klass) {
wrapper->indentString = actualIndent;
wrapper->encoder = yajl_gen_alloc(&cfg, NULL);
wrapper->on_progress_callback = Qnil;
wrapper->sortKeys = sortKeys;
if (opts != Qnil && rb_funcall(opts, intern_has_key, 1, sym_terminator) == Qtrue) {
wrapper->terminator = rb_hash_aref(opts, sym_terminator);
#ifdef HAVE_RUBY_ENCODING_H
Expand Down Expand Up @@ -922,10 +929,12 @@ void Init_yajl() {
intern_to_sym = rb_intern("to_sym");
intern_has_key = rb_intern("has_key?");
intern_as_json = rb_intern("as_json");
intern_sort = rb_intern("sort");

sym_allow_comments = ID2SYM(rb_intern("allow_comments"));
sym_check_utf8 = ID2SYM(rb_intern("check_utf8"));
sym_pretty = ID2SYM(rb_intern("pretty"));
sym_sort_keys = ID2SYM(rb_intern("sort_keys"));
sym_indent = ID2SYM(rb_intern("indent"));
sym_html_safe = ID2SYM(rb_intern("html_safe"));
sym_terminator = ID2SYM(rb_intern("terminator"));
Expand Down
5 changes: 3 additions & 2 deletions ext/yajl/yajl_ext.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ 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_symbolize_names, sym_html_safe;
intern_to_json, intern_has_key, intern_to_sym, intern_as_json, intern_sort;
static ID sym_allow_comments, sym_check_utf8, sym_pretty, sym_sort_keys, sym_indent, sym_terminator, sym_symbolize_keys, sym_symbolize_names, sym_html_safe;

#define GetParser(obj, sval) Data_Get_Struct(obj, yajl_parser_wrapper, sval);
#define GetEncoder(obj, sval) Data_Get_Struct(obj, yajl_encoder_wrapper, sval);
Expand Down Expand Up @@ -105,6 +105,7 @@ typedef struct {
VALUE terminator;
yajl_gen encoder;
unsigned char *indentString;
int sortKeys;
} yajl_encoder_wrapper;

static VALUE rb_yajl_parser_new(int argc, VALUE * argv, VALUE self);
Expand Down
4 changes: 3 additions & 1 deletion lib/yajl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ class Encoder
#
# :indent accepts a string and will be used as the indent character(s) during the pretty print process
#
# :sort_keys accepts a boolean and will cause the keys of an object to be output in sorted order
#
# If a block is passed, it will be used as (and work the same as) the +on_progress+ callback
def self.encode(obj, *args, &block)
# TODO: this code smells, any ideas?
Expand All @@ -73,4 +75,4 @@ def self.encode(obj, *args, &block)
new(options).encode(obj, io, &block)
end
end
end
end
28 changes: 27 additions & 1 deletion spec/encoding/encoding_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -312,4 +312,30 @@ def to_s
Yajl::Encoder.encode(root)
}.should raise_error(Yajl::EncodeError)
end
end

it "should sort keys when asked to" do
a = {}
a["z"] = 1
a["a"] = 2

Yajl::Encoder.encode(a, :sort_keys=> true).should eql(%({"a":2,"z":1}))
end

it "should not sort keys when not asked to" do
a = {}
a["z"] = 1
a["a"] = 2

Yajl::Encoder.encode(a).should eql(%({"z":1,"a":2}))
end

it "should sort keys without error when the keys are various crazy things" do
a = {}
a[:hello] = 1
a[0] = 2
a[Yajl] = 3

Yajl::Encoder.encode(a, :sort_keys=>true).should eql(%({"0":2,"Yajl":3,"hello":1}))
end

end