Psych.to_json can create invalid JSON if scalar values are not marked at quoted
还没有人认领这个 Issue。
评估
调研方向
从链接所指的行开始查看 lib/psych/json/yaml_events.rb,并使用所提供 gist 中的可执行测试重现该问题。跟踪在 Psych.to_json 期间标量值和映射键是如何被标记的,然后添加一个回归测试,表明生成的文档是键带引号的有效 JSON。
由索引模型根据 Issue 内容生成。
描述
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)
- 主要语言
- Ruby
- 星标
- 596
- 派生
- 223
- 平均合并
- 8 小时 38 分钟
- 30 天内合并 PR
- 2
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 没有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
ruby/psych 的其他 Issue
-
难度 2/5 1-3 小时 新手友好度 78/100
-
Documentation
难度 1/5 1 小时以内 新手友好度 72/100
-
Psych.dump raises Encoding::CompatibilityError for UTF-16/UTF-32 strings可能已有人在做 @Halvanhelv 于 54 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 55/100
-
难度 4/5 3-5 天 新手友好度 68/100
-
难度 4/5 3-5 天 新手友好度 52/100
相似的 Issue
-
area/web interface
难度 2/5 1-3 小时 新手友好度 65/100
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 85/100
rubys/roundhouse#1757 ·
-
难度 2/5 半天 新手友好度 72/100
shakacode/react-on-rails-demo-flagship#102 ·
维护者通常 1 天内回复
-
难度 1/5 1 小时以内 新手友好度 88/100
akitaonrails/ai-book-2026#12 ·
-
难度 2/5 1-3 小时 新手友好度 76/100
openfoodfoundation/openfoodnetwork#14970 ·
维护者通常 1 天内回复