Permalink
Browse files

Merge pull request #5 from h2so5/compatibility-cruby

Improve compatibility with CRuby
  • Loading branch information...
mattn committed Jun 19, 2013
2 parents bb86e54 + 53d172b commit d58d532f15e75cd89013e06e3b154ba6d089f2e8
Showing with 10 additions and 7 deletions.
  1. +7 −7 src/mrb_json.c
  2. +3 −0 test/json.rb
View
@@ -43,6 +43,9 @@ mrb_value_to_string(mrb_state* mrb, mrb_value value) {
case MRB_TT_UNDEF:
str = mrb_funcall(mrb, value, "to_s", 0, NULL);
break;
case MRB_TT_SYMBOL:
value = mrb_funcall(mrb, value, "to_s", 0, NULL);
/* FALLTHROUGH */
case MRB_TT_STRING:
{
int ai = mrb_gc_arena_save(mrb);
@@ -188,13 +191,9 @@ mrb_json_parse(mrb_state *mrb, mrb_value self)
static mrb_value
mrb_json_stringify(mrb_state *mrb, mrb_value self)
{
int argc;
mrb_value* argv = NULL;
if (mrb_get_args(mrb, "*", &argv, &argc) != 1) {
mrb_raise(mrb, E_ARGUMENT_ERROR, "invalid argument");
}
return mrb_value_to_string(mrb, argv[0]);
mrb_value obj;
mrb_get_args(mrb, "o", &obj);
return mrb_value_to_string(mrb, obj);
}
/*********************************************************
@@ -206,6 +205,7 @@ mrb_mruby_json_gem_init(mrb_state* mrb) {
struct RClass *_class_json = mrb_define_module(mrb, "JSON");
mrb_define_class_method(mrb, _class_json, "parse", mrb_json_parse, ARGS_REQ(1));
mrb_define_class_method(mrb, _class_json, "stringify", mrb_json_stringify, ARGS_REQ(1));
mrb_define_class_method(mrb, _class_json, "generate", mrb_json_stringify, ARGS_REQ(1));
}
void
View
@@ -10,6 +10,9 @@
assert('stringify boolean') do
JSON::stringify(true) == "true"
end
assert('stringify symbol') do
JSON::stringify(:symbol) == "\"symbol\""
end
assert('strnigify object with numeric value') do
JSON::stringify({"foo"=>"bar"}) == '{"foo":"bar"}'
end

0 comments on commit d58d532

Please sign in to comment.