Skip to content

hash to json strangeness #13

@donpdonp

Description

@donpdonp

I've been struggling with a hash to json conversion that fails but only some of the time.

the output from mirb is correct

mirb> JSON.stringify({:id=>"c58", :method=>"vm.add", :params=>{:name=>"eval1", :url=>"https://gist.github.com/donpdonp/aab0987a4e062ca66d6a"}})
 => "{\"id\":\"c58\",\"method\":\"vm.add\",\"params\":{\"name\":\"eval1\",\"url\":\"https://gist.github.com/donpdonp/aab0987a4e062ca66d6a\"}}"

when I stringify the same object in mruby inside the C app, the output is wrong.
the ruby creating the output is at
https://gist.github.com/donpdonp/07bcb50f4f61680962d3#file-vm-rb-L104
notice the "name" : "eval1 url...."

vm to_s {:id=>"c58", :method=>"vm.add", :params=>{:name=>"eval1", :url=>"https://gist.github.com/donpdonp/aab0987a4e062ca66d6a"}}
vm stringify {"id":"c58","method":"vm.add","params":{"name":"eval1 https://gist.github.com/donpdonp/aab0
987a4e062ca66d6a","url":"https://gist.github.com/donpdonp/aab0987a4e062ca66d6a"}}

If I add a debug print statement like this

diff --git a/src/mrb_json.c b/src/mrb_json.c
index a04871a..1262748 100644
--- a/src/mrb_json.c  
+++ b/src/mrb_json.c
@@ -95,6 +95,7 @@ mrb_value_to_string(mrb_state* mrb, mrb_value value) {
         mrb_str_concat(mrb, str, enckey);  
         mrb_str_cat_cstr(mrb, str, ":");
         obj = mrb_hash_get(mrb, value, key);
+        printf("js k:%s v:%s\n", mrb_string_value_cstr(mrb, &key), mrb_string_value_cstr(mrb, &obj));
         mrb_str_concat(mrb, str, mrb_value_to_string(mrb, obj));
         if (n != l - 1) {
           mrb_str_cat_cstr(mrb, str, ",");

The output becomes correct where "name": "eval1" without the url

vm to_s {:id=>"eys", :method=>"vm.add", :params=>{:name=>"eval1", :url=>"https://gist.github.com/donpdon
p/aab0987a4e062ca66d6a"}}
js k:id v:eys
js k:method v:vm.add
js k:params v:{:name=>"eval1", :url=>"https://gist.github.com/donpdonp/aab0987a4e062ca66d6a"}
js k:name v:eval1
js k:url v:https://gist.github.com/donpdonp/aab0987a4e062ca66d6a
vm stringify {"id":"eys","method":"vm.add","params":{"name":"eval1","url":"https://gist.github.com/donpd
onp/aab0987a4e062ca66d6a"}}

I apologize for the complicated setup and for filing a bug when the mirb output is correct, but I cant think of anywhere else to look for this problem. Do you have any ideas here? thanks.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions