Sha256: e0bfbbb0abb093d12d74553a25e041a330d5ba85acd8e479fa8a03e3c7cd109c

Contents?: true

Size: 1.32 KB

Versions: 3

Compression:

Stored size: 1.32 KB

Contents

require 'json'

class Redis
  class Store < self
    module Strategy
      module Json
        private
          SERIALIZABLE = [String, TrueClass, FalseClass, NilClass, Numeric, Date, Time].freeze
          MARSHAL_INDICATORS = ["\x04", "\004", "\u0004"].freeze

          def _dump(object)
            object = _marshal(object)
            object.to_json
          end

          def _load(string)
            object = JSON.parse(string, :symbolize_names => true)
            _unmarshal(object)
          end

          def _marshal(object)
            case object
            when Hash
              object.each { |k,v| object[k] = _marshal(v) }
            when Array
              object.each_with_index { |v, i| object[i] = _marshal(v) }
            when *SERIALIZABLE
              object
            else
              ::Marshal.dump(object)
            end
          end

          def _unmarshal(object)
            case object
            when Hash
              object.each { |k,v| object[k] = _unmarshal(v) }
            when Array
              object.each_with_index { |v, i| object[i] = _unmarshal(v) }
            when String
              MARSHAL_INDICATORS.any?{ |indicator| object.start_with?(indicator) } ? ::Marshal.load(object) : object
            else
              object
            end
          end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 3 rubygems

Version Path
redis-store-json-3.0.0 redis-store-json/lib/redis/store/strategy/json.rb
redis-rack-json-1.5.2 redis-store-json/lib/redis/store/strategy/json.rb
redis-actionpack-json-4.0.0 redis-store-json/lib/redis/store/strategy/json.rb