From 3202fbabe6df3591d7e2c35727ea9c8b68df8828 Mon Sep 17 00:00:00 2001 From: Sam Stephenson Date: Sun, 18 Mar 2007 07:05:58 +0000 Subject: 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 --- activesupport/lib/active_support/json/decoding.rb | 40 +++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 activesupport/lib/active_support/json/decoding.rb (limited to 'activesupport/lib/active_support/json/decoding.rb') diff --git a/activesupport/lib/active_support/json/decoding.rb b/activesupport/lib/active_support/json/decoding.rb new file mode 100644 index 0000000000..60003a94e5 --- /dev/null +++ b/activesupport/lib/active_support/json/decoding.rb @@ -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 -- cgit v1.2.3