Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
1 parent
3d5c947
commit 3202fba
Showing
24 changed files
with
265 additions
and
166 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,31 @@ | ||
require 'active_support/json/encoders' | ||
require 'active_support/json/encoding' | ||
require 'active_support/json/decoding' | ||
|
||
module ActiveSupport | ||
module JSON #:nodoc: | ||
class CircularReferenceError < StandardError #:nodoc: | ||
end | ||
|
||
# A string that returns itself as as its JSON-encoded form. | ||
class Variable < String #:nodoc: | ||
def to_json | ||
self | ||
end | ||
end | ||
|
||
# When +true+, Hash#to_json will omit quoting string or symbol keys | ||
# if the keys are valid JavaScript identifiers. Note that this is | ||
# technically improper JSON (all object keys must be quoted), so if | ||
# you need strict JSON compliance, set this option to +false+. | ||
mattr_accessor :unquote_hash_key_identifiers | ||
@@unquote_hash_key_identifiers = true | ||
module JSON | ||
RESERVED_WORDS = %w( | ||
abstract delete goto private transient | ||
boolean do if protected try | ||
break double implements public typeof | ||
byte else import return var | ||
case enum in short void | ||
catch export instanceof static volatile | ||
char extends int super while | ||
class final interface switch with | ||
const finally long synchronized | ||
continue float native this | ||
debugger for new throw | ||
default function package throws | ||
) #:nodoc: | ||
|
||
class << self | ||
REFERENCE_STACK_VARIABLE = :json_reference_stack | ||
|
||
def encode(value) | ||
raise_on_circular_reference(value) do | ||
Encoders[value.class].call(value) | ||
end | ||
def valid_identifier?(key) #:nodoc: | ||
key.to_s =~ /^[[:alpha:]_$][[:alnum:]_$]*$/ && !reserved_word?(key) | ||
end | ||
|
||
def can_unquote_identifier?(key) | ||
return false unless unquote_hash_key_identifiers | ||
key.to_s =~ /^[[:alpha:]_$][[:alnum:]_$]*$/ | ||
|
||
def reserved_word?(key) #:nodoc: | ||
RESERVED_WORDS.include?(key.to_s) | ||
end | ||
|
||
protected | ||
def raise_on_circular_reference(value) | ||
stack = Thread.current[REFERENCE_STACK_VARIABLE] ||= [] | ||
raise CircularReferenceError, 'object references itself' if | ||
stack.include? value | ||
stack << value | ||
yield | ||
ensure | ||
stack.pop | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
require 'yaml' | ||
require 'strscan' | ||
|
||
module ActiveSupport | ||
module JSON | ||
class ParseError < StandardError | ||
end | ||
|
||
class << self | ||
# Converts a JSON string into a Ruby object. | ||
def decode(json) | ||
YAML.load(convert_json_to_yaml(json)) | ||
rescue ArgumentError => e | ||
raise ParseError, "Invalid JSON string" | ||
end | ||
|
||
protected | ||
# Ensure that ":" and "," are always followed by a space | ||
def convert_json_to_yaml(json) #:nodoc: | ||
scanner, quoting, marks = StringScanner.new(json), false, [] | ||
|
||
while scanner.scan_until(/(['":,]|\\.)/) | ||
case char = scanner[1] | ||
when '"', "'" | ||
quoting = quoting == char ? false : char | ||
when ":", "," | ||
marks << scanner.pos - 1 unless quoting | ||
end | ||
end | ||
|
||
if marks.empty? | ||
json | ||
else | ||
ranges = ([0] + marks.map(&:succ)).zip(marks + [json.length]) | ||
ranges.map { |(left, right)| json[left..right] }.join(" ") | ||
end | ||
end | ||
end | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module Enumerable | ||
def to_json #:nodoc: | ||
"[#{map { |value| ActiveSupport::JSON.encode(value) } * ', '}]" | ||
end | ||
end |
5 changes: 5 additions & 0 deletions
5
activesupport/lib/active_support/json/encoders/false_class.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class FalseClass | ||
def to_json #:nodoc: | ||
'false' | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class NilClass | ||
def to_json #:nodoc: | ||
'null' | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class Numeric | ||
def to_json #:nodoc: | ||
to_s | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class Regexp | ||
def to_json #:nodoc: | ||
inspect | ||
end | ||
end |
Oops, something went wrong.