略過內容 略過搜尋
方法
D
E
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 格式日期

類別公共方法

decode(json)

JSON 字串(JavaScript Object 表示法)解析成雜湊。詳細資訊請參閱 www.json.org

ActiveSupport::JSON.decode("{\"team\":\"rails\",\"players\":\"36\"}")
=> {"team" => "rails", "players" => "36"}
別名為:load
# File activesupport/lib/active_support/json/decoding.rb, line 22
def decode(json)
  data = ::JSON.parse(json, quirks_mode: true)

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

dump(value, options = nil)

別名為:encode

encode(value, options = nil)

別名為:dump
# File activesupport/lib/active_support/json/encoding.rb, line 22
def encode(value, options = nil)
  Encoding.json_encoder.new(options).encode(value)
end

load(json)

別名為: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 43
def parse_error
  ::JSON::ParserError
end