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

JSON.stringify output is truncated. #192

Open
sbstp opened this issue Jun 3, 2015 · 2 comments
Open

JSON.stringify output is truncated. #192

sbstp opened this issue Jun 3, 2015 · 2 comments

Comments

@sbstp
Copy link
Member

sbstp commented Jun 3, 2015

I was debugging some code using JSON.stringifyand it seems that some objects get truncated.
That's the code in question.

print(JSON.stringify(members, null, 2));

members actually has 3 items. I've checked the length property. However, the output only has one element.

[
  {
    "wrapper": "CPtr",
    "to": {
      "wrapper": "CStruct",
      "members": [
        {
          "wrapper": "CType",
          "name": "double",
          "base_type": "f64",
          "size": 8,
          "load_fun": "$ir_load_f64",
          "store_fun": "$ir_store_f64",
          "wrapper_fun": null
        },

      ],
      "name": "s{}",
      "size": 16
    },
    "base_type": "*",
    "name": "*s{}",
    "size": 8,
    "load_fun": "$ir_load_rawptr",
    "store_fun": "$ir_store_rawptr"
  },
  ,

]

If I print the 3 items manually, like below, it displays all of them.

for (var i = 0; i < members.length; i++) {
    print(JSON.stringify(members[i], null, 2));
}

Thinking it maybe was a print bug, I checked the length of the generated strings. They should be about the same, ± a few characters. There's a huge gap! Doesn't look like a bug with print.

print(names.length, members.length);
var x = 0;
for (var i = 0; i < members.length; i++) {
    x += JSON.stringify(members[i], null, 2).length;
}
var y = JSON.stringify(members, null, 2).length;
print(x, y);
// ~1400, ~500
@sbstp
Copy link
Member Author

sbstp commented Jun 3, 2015

I removed the null & 2 arguments from stringify and the result is the same. It's not a pretty-printing error.

EDIT: Haven't been able to replicate with something else than the FFI code atm.

@sbstp
Copy link
Member Author

sbstp commented Jun 3, 2015

After investigation, it seems that it's a cycle detection issue.

var a = {qux: 'baz'};
var b = [a, a];
print(JSON.stringify(b, null, 2));

Outputs

[
  {
    "qux": "baz"
  },

]

EDIT: Objects are also affected.

var a = {qux: 'baz'};
var b = {a: a, a: a};
print(JSON.stringify(b, null, 2));

Output

{
  "a": {
    "qux": "baz"
  }
}

The output is valid JSON however.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant