class DDP::EJSON

EJSON is a way of embedding more than the built-in JSON types in JSON. It supports all types built into JSON as plain JSON, plus some custom types identified by a key prefixed with ‘$’.

Public Class Methods

add_serializable_class(klass) click to toggle source
# File lib/ddp/ejson.rb, line 87
def self.add_serializable_class(klass)
        @classes ||= {}
        @classes[klass.name] = klass
end
as_ejson(object) click to toggle source
# File lib/ddp/ejson.rb, line 29
def self.as_ejson(object)
        if object.respond_to? :as_ejson
                object.as_ejson
        elsif object.is_a? Hash
                hash_as_ejson(object)
        elsif object.is_a? Array
                object.map { |i| as_ejson(i) }
        else
                object
        end
end
deserialize(object) click to toggle source
# File lib/ddp/ejson.rb, line 19
def self.deserialize(object)
        if object.is_a? Hash
                deserialize_hash(object)
        elsif object.is_a? Array
                object.map { |e| deserialize(e) }
        else
                object
        end
end
deserialize_escape(hash) click to toggle source
# File lib/ddp/ejson.rb, line 72
def self.deserialize_escape(hash)
        hash.map do |k, v|
                [k, deserialize(v)]
        end.to_h
end
deserialize_hash(hash) click to toggle source
# File lib/ddp/ejson.rb, line 53
def self.deserialize_hash(hash)
        deserialize_operation(hash) || hash.map do |k, v|
                [k, deserialize(v)]
        end.to_h
end
deserialize_operation(hash) click to toggle source
# File lib/ddp/ejson.rb, line 59
def self.deserialize_operation(hash)
        if hash['$escape']
                return deserialize_escape(hash['$escape'])
        elsif hash['$date']
                return Time.at(hash['$date'] / 1000.0)
        elsif hash['$binary']
                return Base64.decode64(hash['$binary'])
        elsif hash['$type']
                return deserialize_type(hash)
        end
        false
end
deserialize_type(hash) click to toggle source
# File lib/ddp/ejson.rb, line 78
def self.deserialize_type(hash)
        klass = @classes[hash['$type']]
        if klass
                klass.from_ejson(hash['$value'])
        else
                raise UnknownTypeError, "Don't know how to deserialize #{hash['$type']}"
        end
end
generate(object) click to toggle source
# File lib/ddp/ejson.rb, line 15
def self.generate(object)
        JSON.generate as_ejson(object)
end
hash_as_ejson(hash) click to toggle source

Hashes can contain keys that need to be escaped

# File lib/ddp/ejson.rb, line 42
def self.hash_as_ejson(hash)
        result = hash.map do |k, v|
                if k.is_a?(String) && k[0] == '$'
                        ['$escape', { k => as_ejson(v) }]
                else
                        [k, as_ejson(v)]
                end
        end
        Hash[result]
end
parse(string) click to toggle source
# File lib/ddp/ejson.rb, line 9
def self.parse(string)
        parsed = JSON.parse string

        deserialize(parsed)
end
rename_serializable_class(klass, name) click to toggle source
# File lib/ddp/ejson.rb, line 92
def self.rename_serializable_class(klass, name)
        @classes.delete(klass.name)
        @classes[name] = klass
end