diff options
Diffstat (limited to 'activesupport/lib/active_support/json')
-rw-r--r-- | activesupport/lib/active_support/json/decoding.rb | 5 | ||||
-rw-r--r-- | activesupport/lib/active_support/json/encoding.rb | 23 |
2 files changed, 18 insertions, 10 deletions
diff --git a/activesupport/lib/active_support/json/decoding.rb b/activesupport/lib/active_support/json/decoding.rb index 986a764479..72fd97ceee 100644 --- a/activesupport/lib/active_support/json/decoding.rb +++ b/activesupport/lib/active_support/json/decoding.rb @@ -8,6 +8,11 @@ module ActiveSupport module JSON class << self + # Parses a JSON string (JavaScript Object Notation) into a hash. + # See www.json.org for more info. + # + # ActiveSupport::JSON.decode("{\"team\":\"rails\",\"players\":\"36\"}") + # => {"team" => "rails", "players" => "36"} def decode(json, options ={}) data = MultiJson.load(json, options) if ActiveSupport.parse_json_times diff --git a/activesupport/lib/active_support/json/encoding.rb b/activesupport/lib/active_support/json/encoding.rb index a6e4e7ced2..ded02c873a 100644 --- a/activesupport/lib/active_support/json/encoding.rb +++ b/activesupport/lib/active_support/json/encoding.rb @@ -25,7 +25,10 @@ module ActiveSupport # 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 object 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'}) + # # => "{\"team\":\"rails\",\"players\":\"36\"}" def self.encode(value, options = nil) Encoding::Encoder.new(options).encode(value) end @@ -159,18 +162,18 @@ class Struct #:nodoc: end class TrueClass - AS_JSON = ActiveSupport::JSON::Variable.new('true').freeze - def as_json(options = nil) AS_JSON end #:nodoc: + def as_json(options = nil) self end #:nodoc: + def encode_json(encoder) to_s end #:nodoc: end class FalseClass - AS_JSON = ActiveSupport::JSON::Variable.new('false').freeze - def as_json(options = nil) AS_JSON end #:nodoc: + def as_json(options = nil) self end #:nodoc: + def encode_json(encoder) to_s end #:nodoc: end class NilClass - AS_JSON = ActiveSupport::JSON::Variable.new('null').freeze - def as_json(options = nil) AS_JSON end #:nodoc: + def as_json(options = nil) self end #:nodoc: + def encode_json(encoder) 'null' end #:nodoc: end class String @@ -189,8 +192,8 @@ end class Float # Encoding Infinity or NaN to JSON should return "null". The default returns - # "Infinity" or "NaN" what breaks parsing the JSON. E.g. JSON.parse('[NaN]'). - def as_json(options = nil) finite? ? self : NilClass::AS_JSON end #:nodoc: + # "Infinity" or "NaN" breaks parsing the JSON. E.g. JSON.parse('[NaN]'). + def as_json(options = nil) finite? ? self : nil end #:nodoc: end class BigDecimal @@ -208,7 +211,7 @@ class BigDecimal if finite? ActiveSupport.encode_big_decimal_as_string ? to_s : self else - NilClass::AS_JSON + nil end end end |