提交 7ef21d80 编写于 作者: F Francesco Rodriguez

update AS/json docs [ci skip]

上级 5ea3f284
......@@ -39,8 +39,10 @@ def with_backend(name)
self.backend = old_backend
end
# Returns the class of the error that will be raised when there is an error in decoding JSON.
# Using this method means you won't directly depend on the ActiveSupport's JSON implementation, in case it changes in the future.
# Returns the class of the error that will be raised when there is an
# error in decoding JSON. Using this method means you won't directly
# depend on the ActiveSupport's JSON implementation, in case it changes
# in the future.
#
# begin
# obj = ActiveSupport::JSON.decode(some_string)
......
......@@ -25,9 +25,10 @@ module JSON
# matches YAML-formatted dates
DATE_REGEX = /^(?:\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})?))$/
# Dumps objects in JSON (JavaScript Object Notation). See www.json.org for more info.
# Dumps objects in JSON (JavaScript Object Notation).
# See www.json.org for more info.
#
# ActiveSupport::JSON.encode({team: 'rails', players: '36'})
# ActiveSupport::JSON.encode({ team: 'rails', players: '36' })
# # => "{\"team\":\"rails\",\"players\":\"36\"}"
def self.encode(value, options = nil)
Encoding::Encoder.new(options).encode(value)
......@@ -51,7 +52,7 @@ def encode(value, use_options = true)
end
end
# like encode, but only calls as_json, without encoding to string
# like encode, but only calls as_json, without encoding to string.
def as_json(value, use_options = true)
check_for_circular_references(value) do
use_options ? value.as_json(options_for(value)) : value.as_json
......@@ -60,7 +61,8 @@ def as_json(value, use_options = true)
def options_for(value)
if value.is_a?(Array) || value.is_a?(Hash)
# hashes and arrays need to get encoder in the options, so that they can detect circular references
# hashes and arrays need to get encoder in the options, so that
# they can detect circular references.
options.merge(:encoder => self)
else
options
......@@ -105,10 +107,12 @@ def check_for_circular_references(value)
'&' => '\u0026' }
class << self
# If true, use ISO 8601 format for dates and times. Otherwise, fall back to the Active Support legacy format.
# If true, use ISO 8601 format for dates and times. Otherwise, fall back
# to the Active Support legacy format.
attr_accessor :use_standard_json_time_format
# If false, serializes BigDecimal objects as numeric instead of wrapping them in a string
# If false, serializes BigDecimal objects as numeric instead of wrapping
# them in a string.
attr_accessor :encode_big_decimal_as_string
attr_accessor :escape_regex
......@@ -231,11 +235,13 @@ class BigDecimal
# those libraries would get in general a wrong number and no way to recover
# other than manually inspecting the string with the JSON code itself.
#
# That's why a JSON string is returned. The JSON literal is not numeric, but if
# the other end knows by contract that the data is supposed to be a BigDecimal,
# it still has the chance to post-process the string and get the real value.
# That's why a JSON string is returned. The JSON literal is not numeric, but
# if the other end knows by contract that the data is supposed to be a
# BigDecimal, it still has the chance to post-process the string and get the
# real value.
#
# Use ActiveSupport.use_standard_json_big_decimal_format = true to override this behaviour
# Use <tt>ActiveSupport.use_standard_json_big_decimal_format = true</tt> to
# override this behaviour.
def as_json(options = nil) #:nodoc:
if finite?
ActiveSupport.encode_big_decimal_as_string ? to_s : self
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册