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

Psych.to_json can create invalid JSON if scalar values are not marked at quoted #303

Open
ianks opened this issue Nov 30, 2016 · 0 comments

Comments

@ianks
Copy link

ianks commented Nov 30, 2016

Steps to reproduce

I have created an executable test for this bug: https://gist.github.com/ianks/ba1f7175754780168c2a6618f13c95ec

I did not know whether to report this to Psych or Rails, just let me know if it belongs elsewhere and I will move the issue.

Expected behavior

Psych.to_json should emit valid JSON. Currently, not all keys are double-quoted; which although valid in JS, is not valid JSON.

Actual behavior

It emits invalid json, which can be seen in this example:

{"raw_attributes": {"id": null}, "attributes": {attributes: {"id": {name: "id", value_before_type_cast: null,
        type: {precision: null, scale: null, limit: null, range: {"begin": -2147483648,
            "end": 2147483648, "excl": true}}, original_attribute: null}}}, "new_record": true,
  "active_record_yaml_version": 1}

System configuration

Rails version: 5.0.0

Ruby version: 2.3.1

Notes

Upon further investigation, it seems the scalar types for attributes are not marked as quoted. Even if they are not marked as quoted, should we still quote the values since all keys need to be quoted in JSON? (corresponding code https://github.com/ruby/psych/blob/master/lib/psych/json/yaml_events.rb#L21)

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

No branches or pull requests

1 participant