mattn / mattn/mruby-json

hash to json strangeness

Open
#13 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C
Stars
33
Forks
31
PR merge metrics
No merged PRs in 30d

Description

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...."

``` ruby
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
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.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with src/mrb_json.c, especially mrb_value_to_string, and reproduce the difference between mirb and the embedded mruby C application using the hash shown in the issue. Compare the serialized output with and without the temporary debug print; done means the embedded output preserves separate name and url values without relying on the print statement.

Written by the indexing model from the issue text.

Assessment

Tech stack
c, ruby
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.