Sha256: 4af9afb9a1c29d8851800154d3d4ed4b48df50a3ce3a562769360f1aad139165

Contents?: true

Size: 1.87 KB

Versions: 10

Compression:

Stored size: 1.87 KB

Contents

module java::util::Map #:nodoc:
  # Returns a JSON string representing the hash.
  #
  # Without any +options+, the returned JSON string will include all
  # the hash keys. For example:
  #
  #   { :name => "Konata Izumi", 'age' => 16, 1 => 2 }.to_json
  #   # => {"name": "Konata Izumi", 1: 2, "age": 16}
  #
  # The keys in the JSON string are unordered due to the nature of hashes.
  #
  # The <tt>:only</tt> and <tt>:except</tt> options can be used to limit the
  # attributes included, and will accept 1 or more hash keys to include/exclude.
  #
  #   { :name => "Konata Izumi", 'age' => 16, 1 => 2 }.to_json(:only => [:name, 'age'])
  #   # => {"name": "Konata Izumi", "age": 16}
  #
  #   { :name => "Konata Izumi", 'age' => 16, 1 => 2 }.to_json(:except => 1)
  #   # => {"name": "Konata Izumi", "age": 16}
  #
  # The +options+ also filter down to any hash values. This is particularly
  # useful for converting hashes containing ActiveRecord objects or any object
  # that responds to options in their <tt>to_json</tt> method. For example:
  #
  #   users = User.find(:all)
  #   { :users => users, :count => users.size }.to_json(:include => :posts)
  #
  # would pass the <tt>:include => :posts</tt> option to <tt>users</tt>,
  # allowing the posts association in the User model to be converted to JSON
  # as well.
  def to_json(options = {}) #:nodoc:
    hash = as_json(options)
    return ActiveSupport::JSON.encode(hash, options) if hash != self

    result = '{'
    result << hash.map do |key, value|
      "#{ActiveSupport::JSON.encode(key.to_s)}:#{ActiveSupport::JSON.encode(value, options)}"
    end * ','
    result << '}'
  end

  def as_json(options = nil) #:nodoc:
    if options
      if attrs = options[:except]
        except(*Array.wrap(attrs))
      elsif attrs = options[:only]
        slice(*Array.wrap(attrs))
      else
        self
      end
    else
      self
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
jactive_support-2.1.2 lib/jactive_support/json/encoders/map.rb
jactive_support-3.0.0 lib/jactive_support/json/encoders/map.rb
jactive_support-3.0.0.pre2 lib/jactive_support/json/encoders/map.rb
jactive_support-3.0.0.pre1 lib/jactive_support/json/encoders/map.rb
jactive_support-2.1.1 lib/jactive_support/json/encoders/map.rb
jactive_support-2.1.0 lib/jactive_support/json/encoders/map.rb
jactive_support-2.0.0 lib/jactive_support/json/encoders/map.rb
jactive_support-1.0.2 lib/jactive_support/json/encoders/map.rb
jactive_support-1.0.1-universal-java-1.6 lib/jactive_support/json/encoders/map.rb
jactive_support-1.0.0-universal-java-1.6 lib/jactive_support/json/encoders/map.rb