2012-04-25 10:31:28 -04:00
|
|
|
require 'multi_json'
|
|
|
|
|
2011-08-09 14:39:43 -04:00
|
|
|
module Fog
|
|
|
|
module JSON
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
if MultiJson.respond_to?(:dump)
|
|
|
|
def self.encode(obj)
|
2012-04-26 09:48:46 -04:00
|
|
|
MultiJson.dump(obj)
|
2012-04-25 10:31:28 -04:00
|
|
|
end
|
|
|
|
else
|
|
|
|
def self.encode(obj)
|
|
|
|
MultiJson.encode(obj)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if MultiJson.respond_to?(:load)
|
|
|
|
def self.decode(obj)
|
2012-04-26 09:48:46 -04:00
|
|
|
MultiJson.load(obj)
|
2012-04-25 10:31:28 -04:00
|
|
|
end
|
|
|
|
else
|
|
|
|
def self.decode(obj)
|
|
|
|
MultiJson.decode(obj)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2011-08-09 14:39:43 -04:00
|
|
|
end
|
2012-04-25 12:42:42 -04:00
|
|
|
end
|