我指定 ActiveSupport::JSON.encode 如何处理嵌套的哈希整数键



我不确定这是否是一个错误,或者我是否缺少一个选项来指定在使用 rails ActiveSupport::JSON.encode 转换为 JSON 时如何处理嵌套哈希的整数键。问题示例

$ rails console
[deprecated] I18n.enforce_available_locales will default to true in the future. If you really want to skip validation of your locale you can set I18n.enforce_available_locales = false to avoid this message.
Loading development environment (Rails 3.2.22.1)
Frame number: 0/5
[1] pry(main)> ActiveSupport::JSON.encode({outer: {1 => "a"}})
=> "{"outer":{"1":"a"}}"

如您所见,内部哈希键1已转换为字符串。我知道有多种选项可以指定如何处理未知类/类型和 ActiveRecord 特定的事情(例如允许与 :include 的连接),但我认为作为"本机"类型的整数不需要这种事情,并且默认情况下会处理嵌套。

在 JSON 中,"键"必须始终是字符串。

=> a = { 1 => "1" }
#> {1=>"1"}
=> a.to_json
#> "{"1":"1"}"
=> JSON.parse(a.to_json)
#> {"1"=>"1"}

最新更新