Sha256: 970b185392a360b5100116d9dd1121bbcf6f70f95ab0b7cd92c14c0ed58f46db

Contents?: true

Size: 1.25 KB

Versions: 137

Compression:

Stored size: 1.25 KB

Contents

require 'multi_json/vendor/okjson'

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

      def self.load(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.dump(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

137 entries across 77 versions & 10 rubygems

Version Path
classiccms-0.7.5 vendor/bundle/gems/multi_json-1.3.5/lib/multi_json/adapters/ok_json.rb
classiccms-0.7.5 vendor/bundle/gems/multi_json-1.3.4/lib/multi_json/adapters/ok_json.rb
classiccms-0.7.4 vendor/bundle/gems/multi_json-1.3.4/lib/multi_json/adapters/ok_json.rb
classiccms-0.7.4 vendor/bundle/gems/multi_json-1.3.5/lib/multi_json/adapters/ok_json.rb
classiccms-0.7.3 vendor/bundle/gems/multi_json-1.3.4/lib/multi_json/adapters/ok_json.rb
classiccms-0.7.3 vendor/bundle/gems/multi_json-1.3.5/lib/multi_json/adapters/ok_json.rb
classiccms-0.7.2 vendor/bundle/gems/multi_json-1.3.5/lib/multi_json/adapters/ok_json.rb
classiccms-0.7.2 vendor/bundle/gems/multi_json-1.3.4/lib/multi_json/adapters/ok_json.rb
classiccms-0.7.1 vendor/bundle/gems/multi_json-1.3.5/lib/multi_json/adapters/ok_json.rb
classiccms-0.7.1 vendor/bundle/gems/multi_json-1.3.4/lib/multi_json/adapters/ok_json.rb
classiccms-0.7.0 vendor/bundle/gems/multi_json-1.3.5/lib/multi_json/adapters/ok_json.rb
classiccms-0.7.0 vendor/bundle/gems/multi_json-1.3.4/lib/multi_json/adapters/ok_json.rb
classiccms-0.6.9 vendor/bundle/gems/multi_json-1.3.5/lib/multi_json/adapters/ok_json.rb
classiccms-0.6.9 vendor/bundle/gems/multi_json-1.3.4/lib/multi_json/adapters/ok_json.rb
challah-rolls-0.2.0 vendor/bundle/gems/challah-0.8.3/vendor/bundle/gems/multi_json-1.3.7/lib/multi_json/adapters/ok_json.rb
challah-rolls-0.2.0 vendor/bundle/gems/challah-0.8.3/vendor/bundle/gems/multi_json-1.3.6/lib/multi_json/adapters/ok_json.rb
challah-rolls-0.2.0 vendor/bundle/gems/multi_json-1.3.6/lib/multi_json/adapters/ok_json.rb
challah-rolls-0.2.0 vendor/bundle/gems/challah-0.8.0.pre/vendor/bundle/gems/multi_json-1.3.6/lib/multi_json/adapters/ok_json.rb
challah-rolls-0.2.0 vendor/bundle/gems/multi_json-1.3.7/lib/multi_json/adapters/ok_json.rb
challah-0.8.3 vendor/bundle/gems/multi_json-1.3.7/lib/multi_json/adapters/ok_json.rb