1
0
Fork 0
mirror of https://github.com/fog/fog.git synced 2022-11-09 13:51:43 -05:00
fog--fog/lib/fog/json.rb
Paul Thornthwaite f6280910f8 [core] Move XML/JSON code up out of core
Rather than embedding XML and JSON support within fog-core, this is the
start of extracting them to exist alongside core.

That will move the responsibility to each provider to declare if they want
XML or JSON services.

Goal being as we move towards a modular fog this allows providers to
require core and either JSON or XML gems and not pick up dependencies
only required by the other.
2013-06-24 17:30:00 +01:00

44 lines
1 KiB
Ruby

require "multi_json"
module Fog
# @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
#
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
# 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)
end
def self.decode(obj)
MultiJson.decode(obj)
end
end
end