2013-06-14 06:02:30 -04:00
|
|
|
require "multi_json"
|
2012-04-25 10:31:28 -04:00
|
|
|
|
2011-08-09 14:39:43 -04:00
|
|
|
module Fog
|
2013-06-14 06:02:30 -04:00
|
|
|
|
|
|
|
# @note Extracting JSON components out of core is a work in progress.
|
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
#
|
|
|
|
# @todo Add +require "fog/json" and/or +include Fog::JSON+ to providers using
|
|
|
|
# its services
|
|
|
|
#
|
2013-05-05 14:38:54 -04:00
|
|
|
module JSON
|
2011-08-09 14:39:43 -04:00
|
|
|
|
|
|
|
def self.sanitize(data)
|
|
|
|
case data
|
|
|
|
when Array
|
|
|
|
data.map {|datum| sanitize(datum)}
|
|
|
|
when Hash
|
|
|
|
for key, value in data
|
|
|
|
data[key] = sanitize(value)
|
|
|
|
end
|
|
|
|
when ::Time
|
|
|
|
data.strftime("%Y-%m-%dT%H:%M:%SZ")
|
|
|
|
else
|
|
|
|
data
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-04-25 10:31:28 -04:00
|
|
|
# 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.
|
2013-05-05 14:38:54 -04:00
|
|
|
|
2012-12-14 13:41:39 -05:00
|
|
|
def self.encode(obj)
|
2013-05-05 14:38:54 -04:00
|
|
|
MultiJson.encode(obj)
|
2012-04-25 10:31:28 -04:00
|
|
|
end
|
|
|
|
|
2012-12-14 13:41:39 -05:00
|
|
|
def self.decode(obj)
|
2013-05-05 14:38:54 -04:00
|
|
|
MultiJson.decode(obj)
|
2012-04-25 10:31:28 -04:00
|
|
|
end
|
2011-08-09 14:39:43 -04:00
|
|
|
end
|
2012-04-25 12:42:42 -04:00
|
|
|
end
|