Skip to content

Commit

Permalink
JSON: split encoding and coercion
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremy committed Jun 8, 2009
1 parent 5e1b46d commit 00ee990
Show file tree
Hide file tree
Showing 20 changed files with 279 additions and 332 deletions.
11 changes: 3 additions & 8 deletions activeresource/lib/active_resource/base.rb
Expand Up @@ -873,7 +873,7 @@ def to_xml(options={})
attributes.to_xml({:root => self.class.element_name}.merge(options))
end

# Converts the resource to a JSON string representation.
# Coerces to a hash for JSON encoding.
#
# ==== Options
# The +options+ are passed to the +to_json+ method on each
Expand All @@ -897,8 +897,8 @@ def to_xml(options={})
#
# person.to_json(:except => ["first_name"])
# # => {"last_name": "Smith"}
def to_json(options={})
ActiveSupport::JSON.encode(attributes, options)
def as_json(options = nil)
attributes.as_json(options)
end

# Returns the serialized string representation of the resource in the configured
Expand Down Expand Up @@ -1072,11 +1072,6 @@ def split_options(options = {})
self.class.__send__(:split_options, options)
end

# For compatibility with ActiveSupport::JSON.encode
def rails_to_json(options, *args)
to_json(options)
end

def method_missing(method_symbol, *arguments) #:nodoc:
method_name = method_symbol.to_s

Expand Down
2 changes: 2 additions & 0 deletions activesupport/CHANGELOG
@@ -1,5 +1,7 @@
*Edge*

* JSON: +Object#to_json+ calls +as_json+ to coerce itself into something natively encodable like +Hash+, +Integer+, or +String+. Override +as_json+ instead of +to_json+ so you're JSON library agnostic. [Jeremy Kemper]

* String #to_time and #to_datetime: handle fractional seconds #864 [Jason Frey]

* Update bundled TZInfo to v0.3.13 [Geoff Buesing]
Expand Down
87 changes: 1 addition & 86 deletions activesupport/lib/active_support/json.rb
@@ -1,87 +1,2 @@
require 'active_support/core_ext/module/delegation'
require 'active_support/core_ext/module/attribute_accessors'

module ActiveSupport
# If true, use ISO 8601 format for dates and times. Otherwise, fall back to the Active Support legacy format.
mattr_accessor :use_standard_json_time_format
# Look for and parse json strings that look like ISO 8601 times.
mattr_accessor :parse_json_times

module JSON
# matches YAML-formatted dates
DATE_REGEX = /^(?:\d{4}-\d{2}-\d{2}|\d{4}-\d{1,2}-\d{1,2}[ \t]+\d{1,2}:\d{2}:\d{2}(\.[0-9]*)?(([ \t]*)Z|[-+]\d{2}?(:\d{2})?))$/

module Encoding #:nodoc:
mattr_accessor :escape_regex

ESCAPED_CHARS = {
"\010" => '\b',
"\f" => '\f',
"\n" => '\n',
"\r" => '\r',
"\t" => '\t',
'"' => '\"',
'\\' => '\\\\',
'>' => '\u003E',
'<' => '\u003C',
'&' => '\u0026'
}

def self.escape(string)
string = string.dup.force_encoding(::Encoding::BINARY) if string.respond_to?(:force_encoding)
json = string.gsub(escape_regex) { |s| ESCAPED_CHARS[s] }.
gsub(/([\xC0-\xDF][\x80-\xBF]|
[\xE0-\xEF][\x80-\xBF]{2}|
[\xF0-\xF7][\x80-\xBF]{3})+/nx) { |s|
s.unpack("U*").pack("n*").unpack("H*")[0].gsub(/.{4}/, '\\\\u\&')
}
%("#{json}")
end
end

class << self
delegate :decode, :to => :backend

def backend
unless defined? @backend
self.backend = defined?(::JSON) ? "JSONGem" : "Yaml"
end
@backend
end

def backend=(name)
if name.is_a?(Module)
@backend = name
else
require "active_support/json/backends/#{name.to_s.downcase}.rb"
@backend = ActiveSupport::JSON::Backends::const_get(name)
end
end

def with_backend(name)
old_backend, self.backend = backend, name
yield
ensure
self.backend = old_backend
end
end
end

class << self
attr_reader :escape_html_entities_in_json

def escape_html_entities_in_json=(value)
ActiveSupport::JSON::Encoding.escape_regex = \
if value
/[\010\f\n\r\t"\\><&]/
else
/[\010\f\n\r\t"\\]/
end
@escape_html_entities_in_json = value
end
end
end

ActiveSupport.escape_html_entities_in_json = true

require 'active_support/json/decoding'
require 'active_support/json/encoding'
35 changes: 35 additions & 0 deletions activesupport/lib/active_support/json/decoding.rb
@@ -0,0 +1,35 @@
require 'active_support/core_ext/module/attribute_accessors'

module ActiveSupport
# Look for and parse json strings that look like ISO 8601 times.
mattr_accessor :parse_json_times

module JSON
class << self
delegate :decode, :to => :backend

def backend
@backend || begin
self.backend = "Yaml"
@backend
end
end

def backend=(name)
if name.is_a?(Module)
@backend = name
else
require "active_support/json/backends/#{name.to_s.downcase}.rb"
@backend = ActiveSupport::JSON::Backends::const_get(name)
end
end

def with_backend(name)
old_backend, self.backend = backend, name
yield
ensure
self.backend = old_backend
end
end
end
end
22 changes: 0 additions & 22 deletions activesupport/lib/active_support/json/encoders/date.rb

This file was deleted.

22 changes: 0 additions & 22 deletions activesupport/lib/active_support/json/encoders/date_time.rb

This file was deleted.

13 changes: 0 additions & 13 deletions activesupport/lib/active_support/json/encoders/enumerable.rb

This file was deleted.

6 changes: 0 additions & 6 deletions activesupport/lib/active_support/json/encoders/false_class.rb

This file was deleted.

51 changes: 0 additions & 51 deletions activesupport/lib/active_support/json/encoders/hash.rb

This file was deleted.

6 changes: 0 additions & 6 deletions activesupport/lib/active_support/json/encoders/nil_class.rb

This file was deleted.

6 changes: 0 additions & 6 deletions activesupport/lib/active_support/json/encoders/numeric.rb

This file was deleted.

13 changes: 0 additions & 13 deletions activesupport/lib/active_support/json/encoders/object.rb

This file was deleted.

6 changes: 0 additions & 6 deletions activesupport/lib/active_support/json/encoders/regexp.rb

This file was deleted.

6 changes: 0 additions & 6 deletions activesupport/lib/active_support/json/encoders/string.rb

This file was deleted.

6 changes: 0 additions & 6 deletions activesupport/lib/active_support/json/encoders/symbol.rb

This file was deleted.

24 changes: 0 additions & 24 deletions activesupport/lib/active_support/json/encoders/time.rb

This file was deleted.

6 changes: 0 additions & 6 deletions activesupport/lib/active_support/json/encoders/true_class.rb

This file was deleted.

0 comments on commit 00ee990

Please sign in to comment.