Sha256: 13f9e02a2bb6f550a7e699708c33b121781f1a21a3f68db59805efffcb44ff8f

Contents?: true

Size: 1.01 KB

Versions: 99

Compression:

Stored size: 1.01 KB

Contents

module MultiJson
  module ConvertibleHashKeys
    private

    def symbolize_keys(object)
      prepare_object(object) do |key|
        key.respond_to?(:to_sym) ? key.to_sym : key
      end
    end

    def stringify_keys(object)
      prepare_object(object) do |key|
        key.respond_to?(:to_s) ? key.to_s : key
      end
    end

    def prepare_object(object, &key_modifier)
      return object unless block_given?
      case object
      when Array
        object.map do |value|
          prepare_object(value, &key_modifier)
        end
      when Hash
        object.inject({}) do |result, (key, value)|
          new_key   = key_modifier.call(key)
          new_value = prepare_object(value, &key_modifier)
          result.merge! new_key => new_value
        end
      when String, Numeric, true, false, nil
        object
      else
        if object.respond_to?(:to_json)
          object
        elsif object.respond_to?(:to_s)
          object.to_s
        else
          object
        end
      end
    end
  end
end

Version data entries

99 entries across 97 versions & 11 rubygems

Version Path
scout-5.9.13 vendor/multi_json/lib/multi_json/convertible_hash_keys.rb
scout-5.9.12 vendor/multi_json/lib/multi_json/convertible_hash_keys.rb
scout-5.9.11 vendor/multi_json/lib/multi_json/convertible_hash_keys.rb
scout-5.9.10.pre vendor/multi_json/lib/multi_json/convertible_hash_keys.rb
scout-5.9.8 vendor/multi_json/lib/multi_json/convertible_hash_keys.rb
scout-5.9.7.2.pre vendor/multi_json/lib/multi_json/convertible_hash_keys.rb
scout-5.9.8.pre vendor/multi_json/lib/multi_json/convertible_hash_keys.rb
scout-5.9.7.1 vendor/multi_json/lib/multi_json/convertible_hash_keys.rb
scout-5.9.7.pre vendor/multi_json/lib/multi_json/convertible_hash_keys.rb
vagrant-tiktalik-0.0.3 vendor/bundle/ruby/2.0.0/gems/multi_json-1.7.9/lib/multi_json/convertible_hash_keys.rb
scout-5.9.5 vendor/multi_json/lib/multi_json/convertible_hash_keys.rb
scout-5.9.4 vendor/multi_json/lib/multi_json/convertible_hash_keys.rb
scout-5.9.4.pre vendor/multi_json/lib/multi_json/convertible_hash_keys.rb
scout-5.9.3 vendor/multi_json/lib/multi_json/convertible_hash_keys.rb
scout-5.9.2 vendor/multi_json/lib/multi_json/convertible_hash_keys.rb
scout-5.9.1 vendor/multi_json/lib/multi_json/convertible_hash_keys.rb
scout-5.9.1.pre vendor/multi_json/lib/multi_json/convertible_hash_keys.rb
scout-5.9.0 vendor/multi_json/lib/multi_json/convertible_hash_keys.rb
scout-5.9.0.pre vendor/multi_json/lib/multi_json/convertible_hash_keys.rb
scout-5.8.9 vendor/multi_json/lib/multi_json/convertible_hash_keys.rb