aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support/json/encoders
diff options
context:
space:
mode:
authorSam Stephenson <sam@37signals.com>2007-03-18 07:05:58 +0000
committerSam Stephenson <sam@37signals.com>2007-03-18 07:05:58 +0000
commit3202fbabe6df3591d7e2c35727ea9c8b68df8828 (patch)
treec55f19f82564280e074d9aca449ee4848a97f158 /activesupport/lib/active_support/json/encoders
parent3d5c947155934fb498f8788a204ae9f2e03f2f42 (diff)
downloadrails-3202fbabe6df3591d7e2c35727ea9c8b68df8828.tar.gz
rails-3202fbabe6df3591d7e2c35727ea9c8b68df8828.tar.bz2
rails-3202fbabe6df3591d7e2c35727ea9c8b68df8828.zip
Refactor ActiveSupport::JSON to be less obtuse. Add support for JSON decoding by way of Syck with ActiveSupport::JSON.decode(json_string). Prevent hash keys that are JavaScript reserved words from being unquoted during encoding.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@6443 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activesupport/lib/active_support/json/encoders')
-rw-r--r--activesupport/lib/active_support/json/encoders/core.rb68
-rw-r--r--activesupport/lib/active_support/json/encoders/enumerable.rb5
-rw-r--r--activesupport/lib/active_support/json/encoders/false_class.rb5
-rw-r--r--activesupport/lib/active_support/json/encoders/hash.rb12
-rw-r--r--activesupport/lib/active_support/json/encoders/nil_class.rb5
-rw-r--r--activesupport/lib/active_support/json/encoders/numeric.rb5
-rw-r--r--activesupport/lib/active_support/json/encoders/object.rb10
-rw-r--r--activesupport/lib/active_support/json/encoders/regexp.rb5
-rw-r--r--activesupport/lib/active_support/json/encoders/string.rb27
-rw-r--r--activesupport/lib/active_support/json/encoders/symbol.rb5
-rw-r--r--activesupport/lib/active_support/json/encoders/true_class.rb5
11 files changed, 84 insertions, 68 deletions
diff --git a/activesupport/lib/active_support/json/encoders/core.rb b/activesupport/lib/active_support/json/encoders/core.rb
deleted file mode 100644
index f6cdfbcde5..0000000000
--- a/activesupport/lib/active_support/json/encoders/core.rb
+++ /dev/null
@@ -1,68 +0,0 @@
-module ActiveSupport
- module JSON #:nodoc:
- module Encoders #:nodoc:
- define_encoder Object do |object|
- object.instance_values.to_json
- end
-
- define_encoder TrueClass do
- 'true'
- end
-
- define_encoder FalseClass do
- 'false'
- end
-
- define_encoder NilClass do
- 'null'
- end
-
- ESCAPED_CHARS = {
- "\010" => '\b',
- "\f" => '\f',
- "\n" => '\n',
- "\r" => '\r',
- "\t" => '\t',
- '"' => '\"',
- '\\' => '\\\\'
- }
-
- define_encoder String do |string|
- '"' + string.gsub(/[\010\f\n\r\t"\\]/) { |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\&')
- } + '"'
- end
-
- define_encoder Numeric do |numeric|
- numeric.to_s
- end
-
- define_encoder Symbol do |symbol|
- symbol.to_s.to_json
- end
-
- define_encoder Enumerable do |enumerable|
- "[#{enumerable.map { |value| value.to_json } * ', '}]"
- end
-
- define_encoder Hash do |hash|
- returning result = '{' do
- result << hash.map do |key, value|
- key = ActiveSupport::JSON::Variable.new(key.to_s) if
- ActiveSupport::JSON.can_unquote_identifier?(key)
- "#{key.to_json}: #{value.to_json}"
- end * ', '
- result << '}'
- end
- end
-
- define_encoder Regexp do |regexp|
- regexp.inspect
- end
- end
- end
-end
diff --git a/activesupport/lib/active_support/json/encoders/enumerable.rb b/activesupport/lib/active_support/json/encoders/enumerable.rb
new file mode 100644
index 0000000000..150d233939
--- /dev/null
+++ b/activesupport/lib/active_support/json/encoders/enumerable.rb
@@ -0,0 +1,5 @@
+module Enumerable
+ def to_json #:nodoc:
+ "[#{map { |value| ActiveSupport::JSON.encode(value) } * ', '}]"
+ end
+end
diff --git a/activesupport/lib/active_support/json/encoders/false_class.rb b/activesupport/lib/active_support/json/encoders/false_class.rb
new file mode 100644
index 0000000000..78524e2a03
--- /dev/null
+++ b/activesupport/lib/active_support/json/encoders/false_class.rb
@@ -0,0 +1,5 @@
+class FalseClass
+ def to_json #:nodoc:
+ 'false'
+ end
+end
diff --git a/activesupport/lib/active_support/json/encoders/hash.rb b/activesupport/lib/active_support/json/encoders/hash.rb
new file mode 100644
index 0000000000..3654e10b2e
--- /dev/null
+++ b/activesupport/lib/active_support/json/encoders/hash.rb
@@ -0,0 +1,12 @@
+class Hash
+ def to_json #:nodoc:
+ returning result = '{' do
+ result << map do |key, value|
+ key = ActiveSupport::JSON::Variable.new(key.to_s) if
+ ActiveSupport::JSON.can_unquote_identifier?(key)
+ "#{ActiveSupport::JSON.encode(key)}: #{ActiveSupport::JSON.encode(value)}"
+ end * ', '
+ result << '}'
+ end
+ end
+end
diff --git a/activesupport/lib/active_support/json/encoders/nil_class.rb b/activesupport/lib/active_support/json/encoders/nil_class.rb
new file mode 100644
index 0000000000..98bb6fb677
--- /dev/null
+++ b/activesupport/lib/active_support/json/encoders/nil_class.rb
@@ -0,0 +1,5 @@
+class NilClass
+ def to_json #:nodoc:
+ 'null'
+ end
+end
diff --git a/activesupport/lib/active_support/json/encoders/numeric.rb b/activesupport/lib/active_support/json/encoders/numeric.rb
new file mode 100644
index 0000000000..5d9b2eea9e
--- /dev/null
+++ b/activesupport/lib/active_support/json/encoders/numeric.rb
@@ -0,0 +1,5 @@
+class Numeric
+ def to_json #:nodoc:
+ to_s
+ end
+end
diff --git a/activesupport/lib/active_support/json/encoders/object.rb b/activesupport/lib/active_support/json/encoders/object.rb
new file mode 100644
index 0000000000..51852e17cb
--- /dev/null
+++ b/activesupport/lib/active_support/json/encoders/object.rb
@@ -0,0 +1,10 @@
+class Object
+ # Dumps object in JSON (JavaScript Object Notation). See www.json.org for more info.
+ #
+ # Account.find(1).to_json
+ # => "{attributes: {username: \"foo\", id: \"1\", password: \"bar\"}}"
+ #
+ def to_json
+ ActiveSupport::JSON.encode(instance_values)
+ end
+end
diff --git a/activesupport/lib/active_support/json/encoders/regexp.rb b/activesupport/lib/active_support/json/encoders/regexp.rb
new file mode 100644
index 0000000000..a3f32eafa2
--- /dev/null
+++ b/activesupport/lib/active_support/json/encoders/regexp.rb
@@ -0,0 +1,5 @@
+class Regexp
+ def to_json #:nodoc:
+ inspect
+ end
+end
diff --git a/activesupport/lib/active_support/json/encoders/string.rb b/activesupport/lib/active_support/json/encoders/string.rb
new file mode 100644
index 0000000000..707298d987
--- /dev/null
+++ b/activesupport/lib/active_support/json/encoders/string.rb
@@ -0,0 +1,27 @@
+module ActiveSupport
+ module JSON
+ module Encoding
+ ESCAPED_CHARS = {
+ "\010" => '\b',
+ "\f" => '\f',
+ "\n" => '\n',
+ "\r" => '\r',
+ "\t" => '\t',
+ '"' => '\"',
+ '\\' => '\\\\'
+ }
+ end
+ end
+end
+
+class String
+ def to_json #:nodoc:
+ '"' + gsub(/[\010\f\n\r\t"\\]/) { |s|
+ ActiveSupport::JSON::Encoding::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\&')
+ } + '"'
+ end
+end
diff --git a/activesupport/lib/active_support/json/encoders/symbol.rb b/activesupport/lib/active_support/json/encoders/symbol.rb
new file mode 100644
index 0000000000..57291e5448
--- /dev/null
+++ b/activesupport/lib/active_support/json/encoders/symbol.rb
@@ -0,0 +1,5 @@
+class Symbol
+ def to_json #:nodoc:
+ ActiveSupport::JSON.encode(to_s)
+ end
+end
diff --git a/activesupport/lib/active_support/json/encoders/true_class.rb b/activesupport/lib/active_support/json/encoders/true_class.rb
new file mode 100644
index 0000000000..f652f8bb54
--- /dev/null
+++ b/activesupport/lib/active_support/json/encoders/true_class.rb
@@ -0,0 +1,5 @@
+class TrueClass
+ def to_json #:nodoc:
+ 'true'
+ end
+end