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 an optional create additions callback #182

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
155 changes: 83 additions & 72 deletions ext/json/ext/parser/parser.c

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion ext/json/ext/parser/parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ typedef struct JSON_ParserStruct {
int quirks_mode;
VALUE object_class;
VALUE array_class;
int create_additions;
VALUE create_additions;
VALUE match_string;
FBuffer *fbuffer;
} JSON_Parser;
Expand Down
17 changes: 14 additions & 3 deletions ext/json/ext/parser/parser.rl
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ static VALUE CNaN, CInfinity, CMinusInfinity;
static ID i_json_creatable_p, i_json_create, i_create_id, i_create_additions,
i_chr, i_max_nesting, i_allow_nan, i_symbolize_names, i_quirks_mode,
i_object_class, i_array_class, i_key_p, i_deep_const_get, i_match,
i_match_string, i_aset, i_aref, i_leftshift;
i_match_string, i_aset, i_aref, i_leftshift, i_call;

%%{
machine JSON_common;
Expand Down Expand Up @@ -174,7 +174,12 @@ static char *JSON_parse_object(JSON_Parser *json, char *p, char *pe, VALUE *resu
klassname = rb_funcall(*result, i_aref, 1, json->create_id);
}
if (!NIL_P(klassname)) {
VALUE klass = rb_funcall(mJSON, i_deep_const_get, 1, klassname);
VALUE klass = NULL;
if(rb_class_of(json->create_additions) == rb_cProc)
klass = rb_funcall(json->create_additions, i_call, 1, klassname);
else
klass = rb_funcall(mJSON, i_deep_const_get, 1, klassname);

if (RTEST(rb_funcall(klass, i_json_creatable_p, 0))) {
*result = rb_funcall(klass, i_json_create, 1, *result);
}
Expand Down Expand Up @@ -662,7 +667,12 @@ static VALUE cParser_initialize(int argc, VALUE *argv, VALUE self)
}
tmp = ID2SYM(i_create_additions);
if (option_given_p(opts, tmp)) {
json->create_additions = RTEST(rb_hash_aref(opts, tmp));
VALUE create_additions = rb_hash_aref(opts, tmp);
if (rb_class_of(create_additions) == rb_cProc){
json->create_additions = create_additions;
}else{
json->create_additions = RTEST(create_additions);
}
} else {
json->create_additions = 0;
}
Expand Down Expand Up @@ -904,6 +914,7 @@ void Init_parser()
i_aset = rb_intern("[]=");
i_aref = rb_intern("[]");
i_leftshift = rb_intern("<<");
i_call = rb_intern("call");
#ifdef HAVE_RUBY_ENCODING_H
CEncoding_UTF_8 = rb_funcall(rb_path2class("Encoding"), rb_intern("find"), 1, rb_str_new2("utf-8"));
CEncoding_UTF_16BE = rb_funcall(rb_path2class("Encoding"), rb_intern("find"), 1, rb_str_new2("utf-16be"));
Expand Down
8 changes: 8 additions & 0 deletions java/src/json/ext/OptionsReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.jruby.RubyHash;
import org.jruby.RubyNumeric;
import org.jruby.RubyString;
import org.jruby.RubyObject;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.builtin.IRubyObject;
import org.jruby.util.ByteList;
Expand Down Expand Up @@ -111,4 +112,11 @@ public RubyHash getHash(String key) {
if (value == null || value.isNil()) return new RubyHash(runtime);
return (RubyHash) value;
}

RubyObject getObject(String key, RubyObject defaultValue) {
IRubyObject value = get(key);

if (value == null || value.isNil()) return defaultValue;
return (RubyObject)value;
}
}