1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
|
require "fog/core"
require "multi_json"
require File.expand_path("../json/version", __FILE__)
module Fog
# The {JSON} module includes functionality that is common between APIs using JSON to send and
# receive data.
#
# The intent is to provide common code for provider APIs using JSON but not require it for those
# using XML.
#
module JSON
class EncodeError < Fog::Errors::Error; end
class DecodeError < Fog::Errors::Error; end
# This cleans up Time objects to be ISO8601 format
#
def self.sanitize(data)
case data
when Array
data.map { |datum| sanitize(datum) }
when Hash
data.each { |key, value| data[key] = sanitize(value) }
when ::Time
data.strftime("%Y-%m-%dT%H:%M:%S%:z")
else
data
end
end
# Do the MultiJson introspection at this level so we can define our encode/decode methods and
# perform the introspection only once rather than once per call.
def self.encode(obj)
MultiJson.encode(obj)
rescue => err
raise EncodeError.slurp(err)
end
def self.decode(obj)
MultiJson.decode(obj)
rescue => err
raise DecodeError.slurp(err)
end
end
end
|