Sha256: f0acda977d70836af6d9803016d3abe873a9314529633cef352063c0b5e4b5d5

Contents?: true

Size: 860 Bytes

Versions: 2

Compression:

Stored size: 860 Bytes

Contents

require 'json'

module DataMapper
  module Types
    class Json < DataMapper::Type
      primitive String
      length    65535
      lazy      true

      def self.load(value, property)
        if value.nil?
          nil
        elsif value.is_a?(String)
          ::JSON.load(value)
        else
          raise ArgumentError.new("+value+ of a property of JSON type must be nil or a String")
        end
      end

      def self.dump(value, property)
        if value.nil? || value.is_a?(String)
          value
        else
          ::JSON.dump(value)
        end
      end

      def self.typecast(value, property)
        if value.nil? || value.kind_of?(Array) || value.kind_of?(Hash)
          value
        else
          ::JSON.load(value.to_s)
        end
      end
    end # class Json
    JSON = Json
  end # module Types
end # module DataMapper

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
dm-types-0.10.1 lib/dm-types/json.rb
dm-types-0.10.0 lib/dm-types/json.rb