Sha256: 3bd4aea36db5d40bffaecfe0356584d366dc9fd00a0faa3be4c69d3e8be6c4b9

Contents?: true

Size: 1.26 KB

Versions: 34

Compression:

Stored size: 1.26 KB

Contents

require "multi_json/vendor/ok_json"

module MultiJson
  module Engines
    class OkJson
      ParseError = ::MultiJson::OkJson::Error

      def self.decode(string, options = {}) #:nodoc:
        string = string.read if string.respond_to?(:read)
        result = ::MultiJson::OkJson.decode(string)
        options[:symbolize_keys] ? symbolize_keys(result) : result
      end

      def self.encode(object, options = {}) #:nodoc:
        ::MultiJson::OkJson.valenc(stringify_keys(object))
      end

      def self.symbolize_keys(object) #:nodoc:
        modify_keys(object) do |key|
          key.is_a?(String) ? key.to_sym : key
        end
      end

      def self.stringify_keys(object) #:nodoc:
        modify_keys(object) do |key|
          key.is_a?(Symbol) ? key.to_s : key
        end
      end

      def self.modify_keys(object, &modifier) #:nodoc:
        case object
        when Array
          object.map do |value|
            modify_keys(value, &modifier)
          end
        when Hash
          object.inject({}) do |result, (key, value)|
            new_key   = modifier.call(key)
            new_value = modify_keys(value, &modifier)
            result.merge! new_key => new_value
          end
        else
          object
        end
      end
    end
  end
end

Version data entries

34 entries across 29 versions & 5 rubygems

Version Path
challah-0.6.2 vendor/bundle/gems/multi_json-1.1.0/lib/multi_json/engines/ok_json.rb
challah-0.6.1 vendor/bundle/gems/multi_json-1.0.4/lib/multi_json/engines/ok_json.rb
challah-0.6.1 vendor/bundle/gems/multi_json-1.1.0/lib/multi_json/engines/ok_json.rb
resque-pool-0.3.0 vendor/bundle/ruby/1.8/gems/multi_json-1.1.0/lib/multi_json/engines/ok_json.rb
challah-0.6.0 vendor/bundle/gems/multi_json-1.0.4/lib/multi_json/engines/ok_json.rb
challah-0.6.0 vendor/bundle/gems/multi_json-1.1.0/lib/multi_json/engines/ok_json.rb
challah-0.5.4 vendor/bundle/gems/multi_json-1.0.4/lib/multi_json/engines/ok_json.rb
challah-0.5.4 vendor/bundle/gems/multi_json-1.1.0/lib/multi_json/engines/ok_json.rb
resque-pool-0.3.0.beta.2 vendor/bundle/ruby/1.8/gems/multi_json-1.1.0/lib/multi_json/engines/ok_json.rb
challah-0.5.3 vendor/bundle/gems/multi_json-1.1.0/lib/multi_json/engines/ok_json.rb
challah-0.5.3 vendor/bundle/gems/multi_json-1.0.4/lib/multi_json/engines/ok_json.rb
challah-0.5.2 vendor/bundle/gems/multi_json-1.0.4/lib/multi_json/engines/ok_json.rb
challah-0.5.2 vendor/bundle/gems/multi_json-1.1.0/lib/multi_json/engines/ok_json.rb
challah-0.5.1 vendor/bundle/gems/multi_json-1.1.0/lib/multi_json/engines/ok_json.rb
radiant-1.0.0 ruby-debug/ruby/1.8/gems/multi_json-1.1.0/lib/multi_json/engines/ok_json.rb
challah-0.5.0 vendor/bundle/gems/multi_json-1.0.4/lib/multi_json/engines/ok_json.rb
multi_json-1.1.0 lib/multi_json/engines/ok_json.rb
challah-0.4.1 vendor/bundle/gems/multi_json-1.0.4/lib/multi_json/engines/ok_json.rb
challah-0.4.0 vendor/bundle/gems/multi_json-1.0.4/lib/multi_json/engines/ok_json.rb
challah-0.3.5 vendor/bundle/gems/multi_json-1.0.4/lib/multi_json/engines/ok_json.rb