Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 6443

Show
Ignore:
Timestamp:
03/18/07 07:05:58 (2 years ago)
Author:
sam
Message:

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.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/activesupport/CHANGELOG

    r6441 r6443  
    11*SVN* 
     2 
     3* 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.  [Sam Stephenson] 
    24 
    35* alias_method_chain preserves the original method's visibility.  #7854 [Jonathan Viney] 
  • trunk/activesupport/lib/active_support/core_ext/blank.rb

    r3493 r6443  
    1 class Object #:nodoc: 
     1class Object  
    22  # "", "   ", nil, [], and {} are blank 
    3   def blank? 
     3  def blank? #:nodoc: 
    44    if respond_to?(:empty?) && respond_to?(:strip) 
    55      empty? or strip.empty? 
  • trunk/activesupport/lib/active_support/core_ext/object/extending.rb

    r6036 r6443  
    1 class Object #:nodoc: 
    2   def remove_subclasses_of(*superclasses) 
     1class Object 
     2  def remove_subclasses_of(*superclasses) #:nodoc: 
    33    Class.remove_class(*subclasses_of(*superclasses)) 
    44  end 
    55 
    6   def subclasses_of(*superclasses) 
     6  def subclasses_of(*superclasses) #:nodoc: 
    77    subclasses = [] 
    88    ObjectSpace.each_object(Class) do |k| 
     
    1717  end 
    1818   
    19   def extended_by 
     19  def extended_by #:nodoc: 
    2020    ancestors = class << self; ancestors end 
    2121    ancestors.select { |mod| mod.class == Module } - [ Object, Kernel ] 
    2222  end 
    2323   
    24   def copy_instance_variables_from(object, exclude = []) 
     24  def copy_instance_variables_from(object, exclude = []) #:nodoc: 
    2525    exclude += object.protected_instance_variables if object.respond_to? :protected_instance_variables 
    2626     
     
    2929  end 
    3030   
    31   def extend_with_included_modules_from(object) 
     31  def extend_with_included_modules_from(object) #:nodoc: 
    3232    object.extended_by.each { |mod| extend mod } 
    3333  end 
    3434 
    35   def instance_values 
     35  def instance_values #:nodoc: 
    3636    instance_variables.inject({}) do |values, name| 
    3737      values[name[1..-1]] = instance_variable_get(name) 
     
    4141   
    4242  unless defined? instance_exec # 1.9 
    43     def instance_exec(*arguments, &block) 
     43    def instance_exec(*arguments, &block) #:nodoc: 
    4444      block.bind(self)[*arguments] 
    4545    end 
  • trunk/activesupport/lib/active_support/core_ext/object/misc.rb

    r6011 r6443  
    4444  end 
    4545   
    46   # Dumps object in JSON (JavaScript Object Notation).  See www.json.org for more info. 
    47   # 
    48   #   Account.find(1).to_json 
    49   #   => "{attributes: {username: \"foo\", id: \"1\", password: \"bar\"}}" 
    50   # 
    51   def to_json 
    52     ActiveSupport::JSON.encode(self) 
    53   end 
    54  
    5546  # A duck-type assistant method. For example, ActiveSupport extends Date 
    5647  # to define an acts_like_date? method, and extends Time to define 
     
    5950  # we want to act like Time simply need to define an acts_like_time? method. 
    6051  def acts_like?(duck) 
    61     respond_to? :"acts_like_#{duck}?" 
     52    respond_to? "acts_like_#{duck}?" 
    6253  end 
    6354end 
  • trunk/activesupport/lib/active_support/dependencies.rb

    r6426 r6443  
    481481end 
    482482 
    483 class Object #:nodoc: 
     483class Object 
    484484   
    485485  alias_method :load_without_new_constant_marking, :load 
    486486   
    487   def load(file, *extras) 
     487  def load(file, *extras) #:nodoc: 
    488488    Dependencies.new_constants_in(Object) { super(file, *extras) } 
    489489  rescue Exception => exception  # errors from loading file 
     
    492492  end 
    493493 
    494   def require(file, *extras) 
     494  def require(file, *extras) #:nodoc: 
    495495    Dependencies.new_constants_in(Object) { super(file, *extras) } 
    496496  rescue Exception => exception  # errors from required file 
  • trunk/activesupport/lib/active_support/json.rb

    r5486 r6443  
    1 require 'active_support/json/encoders' 
     1require 'active_support/json/encoding' 
     2require 'active_support/json/decoding' 
    23 
    34module ActiveSupport 
    4   module JSON #:nodoc: 
    5     class CircularReferenceError < StandardError #:nodoc: 
    6     end 
    7      
    8     # A string that returns itself as as its JSON-encoded form. 
    9     class Variable < String #:nodoc: 
    10       def to_json 
    11         self 
    12       end 
    13     end 
    14      
    15     # When +true+, Hash#to_json will omit quoting string or symbol keys 
    16     # if the keys are valid JavaScript identifiers.  Note that this is 
    17     # technically improper JSON (all object keys must be quoted), so if 
    18     # you need strict JSON compliance, set this option to +false+. 
    19     mattr_accessor :unquote_hash_key_identifiers 
    20     @@unquote_hash_key_identifiers = true 
     5  module JSON 
     6    RESERVED_WORDS = %w( 
     7      abstract      delete        goto          private       transient 
     8      boolean       do            if            protected     try 
     9      break         double        implements    public        typeof 
     10      byte          else          import        return        var 
     11      case          enum          in            short         void 
     12      catch         export        instanceof    static        volatile 
     13      char          extends       int           super         while 
     14      class         final         interface     switch        with 
     15      const         finally       long          synchronized 
     16      continue      float         native        this 
     17      debugger      for           new           throw 
     18      default       function      package       throws 
     19    ) #:nodoc: 
    2120 
    2221    class << self 
    23       REFERENCE_STACK_VARIABLE = :json_reference_stack 
    24        
    25       def encode(value) 
    26         raise_on_circular_reference(value) do 
    27           Encoders[value.class].call(value) 
    28         end 
     22      def valid_identifier?(key) #:nodoc: 
     23        key.to_s =~ /^[[:alpha:]_$][[:alnum:]_$]*$/ && !reserved_word?(key) 
    2924      end 
    30        
    31       def can_unquote_identifier?(key) 
    32         return false unless unquote_hash_key_identifiers 
    33         key.to_s =~ /^[[:alpha:]_$][[:alnum:]_$]*$/ 
     25 
     26      def reserved_word?(key) #:nodoc: 
     27        RESERVED_WORDS.include?(key.to_s) 
    3428      end 
    35        
    36       protected 
    37         def raise_on_circular_reference(value) 
    38           stack = Thread.current[REFERENCE_STACK_VARIABLE] ||= [] 
    39           raise CircularReferenceError, 'object references itself' if 
    40             stack.include? value 
    41           stack << value 
    42           yield 
    43         ensure 
    44           stack.pop 
    45         end 
    4629    end 
    4730  end 
  • trunk/activesupport/lib/active_support/vendor/builder/blankslate.rb

    r4260 r6443  
    4949end 
    5050 
    51 class Object #:nodoc: 
     51class Object 
    5252  class << self 
    5353    alias_method :blank_slate_method_added, :method_added 
     
    5555    # Detect method additions to Object and remove them in the 
    5656    # BlankSlate class. 
    57     def method_added(name) 
     57    def method_added(name) #:nodoc: 
    5858      blank_slate_method_added(name) 
    5959      return if self != Object