跳至内容 跳至搜索
方法
D
E
J
L
P

常量

DATETIME_REGEX = /\A(?:\d{4}-\d{2}-\d{2}|\d{4}-\d{1,2}-\d{1,2}[T \t]+\d{1,2}:\d{2}:\d{2}(\.[0-9]*)?(([ \t]*)Z|[-+]\d{2}?(:\d{2})?)?)\z/
 
DATE_REGEX = /\A\d{4}-\d{2}-\d{2}\z/
 

匹配 YAML 格式的日期

Attributes

[RW] escape_html_entities_in_json

如果为 true,则将 >、<、& 编码为 Unicode 转义序列(例如,> 编码为 u003e)以提高安全性。

[RW] escape_js_separators_in_json

如果为 true,则将 LINE SEPARATOR (U+2028) 和 PARAGRAPH SEPARATOR (U+2029) 编码为 Unicode 转义序列(‘u2028’ 和 ‘u2029’)。历史上,这些字符在 JavaScript 字符串中无效,但在 ECMAScript 2019 中已更改。因此,在现代浏览器中已不再是问题: caniuse.com/mdn-javascript_builtins_json_json_superset

[R] json_encoder

设置 Rails 用于将 Ruby 对象编码为 +Object#to_json+ 和 ActiveSupport::JSON.encode 中的 JSON 字符串的编码器。

[RW] time_precision

设置编码时间值的精度。默认为 3(相当于毫秒精度)

[RW] use_standard_json_time_format

如果为 true,则对日期和时间使用 ISO 8601 格式。否则,回退到 Active Support 旧格式。

类公共方法

decode(json, options = {})

JSON(JavaScript Object Notation)字符串解析为 Ruby 对象。有关更多信息,请参阅 www.json.org

ActiveSupport::JSON.decode("{\"team\":\"rails\",\"players\":\"36\"}")
# => {"team" => "rails", "players" => "36"}
ActiveSupport::JSON.decode("2.39")
# => 2.39
也别名为: load
# File activesupport/lib/active_support/json/decoding.rb, line 24
def decode(json, options = {})
  data = ::JSON.parse(json, options)

  if ActiveSupport.parse_json_times
    convert_dates_from(data)
  else
    data
  end
end

dump(value, options = nil)

别名: encode

encode(value, options = nil)

将对象转储为 JSON(JavaScript Object Notation)。有关更多信息,请参阅 www.json.org

ActiveSupport::JSON.encode({ team: 'rails', players: '36' })
# => "{\"team\":\"rails\",\"players\":\"36\"}"

默认情况下,它生成的 JSON 安全地包含在 JavaScript 中,因为它转义了 U+2028(行分隔符)和 U+2029(段落分隔符)

ActiveSupport::JSON.encode({ key: "\u2028" })
# => "{\"key\":\"\\u2028\"}"

默认情况下,它还生成安全地包含在 HTML 中的 JSON,因为它转义了 <>&

ActiveSupport::JSON.encode({ key: "<>&" })
# => "{\"key\":\"\\u003c\\u003e\\u0026\"}"

此行为可以通过 `escape_html_entities` 选项或全局 escape_html_entities_in_json 配置选项进行更改。

ActiveSupport::JSON.encode({ key: "<>&" }, escape_html_entities: false)
# => "{\"key\":\"<>&\"}"

出于性能原因,您可以将 `escape` 选项设置为 false,这将跳过所有转义

ActiveSupport::JSON.encode({ key: "\u2028<>&" }, escape: false)
# => "{\"key\":\"\u2028<>&\"}"
也别名为: dump
# File activesupport/lib/active_support/json/encoding.rb, line 47
def encode(value, options = nil)
  if options.nil? || options.empty?
    Encoding.encode_without_options(value)
  elsif options == { escape: false }.freeze
    Encoding.encode_without_escape(value)
  else
    Encoding.json_encoder.new(options).encode(value)
  end
end

json_encoder=(encoder)

# File activesupport/lib/active_support/json/encoding.rb, line 229
def json_encoder=(encoder)
  @json_encoder = encoder
  @encoder_without_options = encoder.new
  @encoder_without_escape = encoder.new(escape: false)
end

load(json, options = {})

别名: decode

parse_error()

返回解码 JSON 时将引发的错误类。使用此方法意味着您不会直接依赖 ActiveSupport 的 JSON 实现,以防将来发生更改。

begin
  obj = ActiveSupport::JSON.decode(some_string)
rescue ActiveSupport::JSON.parse_error
  Rails.logger.warn("Attempted to decode invalid JSON: #{some_string}")
end
# File activesupport/lib/active_support/json/decoding.rb, line 45
def parse_error
  ::JSON::ParserError
end