From 66638b25d76a5eb737c9a41ed9b4847841549d50 Mon Sep 17 00:00:00 2001 From: Kyle Rames Date: Wed, 3 Apr 2013 08:42:45 -0500 Subject: [PATCH] Updated gem spec to require json rather than multi_json; Fog::JSON will attempt to load and use multi_json first and then fallback to require json; removed hard coded references to multi_json --- fog.gemspec | 2 +- lib/fog/core/json.rb | 42 ++++++++++++++++++++++++++++------ lib/fog/dreamhost/dns.rb | 4 +--- lib/fog/openstack/identity.rb | 3 +-- lib/fog/openstack/image.rb | 5 +--- lib/fog/openstack/network.rb | 4 +--- lib/fog/openstack/volume.rb | 5 +--- lib/fog/rackspace.rb | 2 +- lib/fog/riakcs/provisioning.rb | 4 +--- lib/fog/riakcs/usage.rb | 1 - lib/tasks/changelog_task.rb | 1 - 11 files changed, 43 insertions(+), 30 deletions(-) diff --git a/fog.gemspec b/fog.gemspec index 6908dfcf2..190d888c4 100644 --- a/fog.gemspec +++ b/fog.gemspec @@ -43,7 +43,7 @@ Gem::Specification.new do |s| s.add_dependency('builder') s.add_dependency('excon', '~>0.20') s.add_dependency('formatador', '~>0.2.0') - s.add_dependency('multi_json', '~>1.0') + s.add_dependency('json', '~>1.7') s.add_dependency('mime-types') s.add_dependency('net-scp', '~>1.1') s.add_dependency('net-ssh', '>=2.1.3') diff --git a/lib/fog/core/json.rb b/lib/fog/core/json.rb index de5d9042d..734dc1cdb 100644 --- a/lib/fog/core/json.rb +++ b/lib/fog/core/json.rb @@ -1,7 +1,38 @@ -require 'multi_json' +require 'singleton' +require 'fog/core/logger' module Fog - module JSON + class JSON + include Singleton + + module LegacyJSON + def encode(obj) + ::JSON.generate(obj) + end + + def decode(obj) + ::JSON.parse(obj) + end + end + + module NewJSON + def encode(obj) + MultiJson.encode(obj) + end + + def decode(obj) + MultiJson.decode(obj) + end + end + + begin + require 'multi_json' + include NewJSON + rescue LoadError + Fog::Logger.deprecation "Defaulting to json library for json parsing. Please consider using multi_json library for the greatest performance/flexibility." + require 'json' + include LegacyJSON + end def self.sanitize(data) case data @@ -20,15 +51,12 @@ module Fog # 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) + Fog::JSON.instance.encode(obj) end def self.decode(obj) - MultiJson.decode(obj) + Fog::JSON.instance.decode(obj) end - - end end diff --git a/lib/fog/dreamhost/dns.rb b/lib/fog/dreamhost/dns.rb index 2dc8b4f07..a297cc132 100644 --- a/lib/fog/dreamhost/dns.rb +++ b/lib/fog/dreamhost/dns.rb @@ -47,8 +47,6 @@ module Fog class Real def initialize(options={}) - require 'multi_json' - @dreamhost_api_key = options[:dreamhost_api_key] if options[:dreamhost_url] uri = URI.parse(options[:dreamhost_url]) @@ -73,7 +71,7 @@ module Fog response = @connection.request(params) unless response.body.empty? - response.body = MultiJson.decode(response.body) + response.body = Fog::JSON.decode(response.body) end if response.body['result'] != 'success' raise response.body['data'] diff --git a/lib/fog/openstack/identity.rb b/lib/fog/openstack/identity.rb index 97205dbf7..d9b0b6882 100644 --- a/lib/fog/openstack/identity.rb +++ b/lib/fog/openstack/identity.rb @@ -96,7 +96,6 @@ module Fog end def initialize(options={}) - require 'multi_json' @openstack_username = options[:openstack_username] || 'admin' @openstack_tenant = options[:openstack_tenant] || 'admin' @openstack_auth_uri = URI.parse(options[:openstack_auth_url]) @@ -249,7 +248,7 @@ module Fog end end unless response.body.empty? - response.body = MultiJson.decode(response.body) + response.body = Fog::JSON.decode(response.body) end response end diff --git a/lib/fog/openstack/image.rb b/lib/fog/openstack/image.rb index 74eec7f07..1caa80f06 100644 --- a/lib/fog/openstack/image.rb +++ b/lib/fog/openstack/image.rb @@ -44,7 +44,6 @@ module Fog end def initialize(options={}) - require 'multi_json' @openstack_username = options[:openstack_username] @openstack_tenant = options[:openstack_tenant] @openstack_auth_uri = URI.parse(options[:openstack_auth_url]) @@ -92,8 +91,6 @@ module Fog attr_reader :current_tenant def initialize(options={}) - require 'multi_json' - @openstack_auth_token = options[:openstack_auth_token] unless @openstack_auth_token @@ -168,7 +165,7 @@ module Fog end end unless response.body.empty? - response.body = MultiJson.decode(response.body) + response.body = Fog::JSON.decode(response.body) end response end diff --git a/lib/fog/openstack/network.rb b/lib/fog/openstack/network.rb index 71ce5ef04..8c2c6d4fb 100644 --- a/lib/fog/openstack/network.rb +++ b/lib/fog/openstack/network.rb @@ -112,8 +112,6 @@ module Fog attr_reader :current_tenant def initialize(options={}) - require 'multi_json' - @openstack_auth_token = options[:openstack_auth_token] unless @openstack_auth_token @@ -188,7 +186,7 @@ module Fog end end unless response.body.empty? - response.body = MultiJson.decode(response.body) + response.body = Fog::JSON.decode(response.body) end response end diff --git a/lib/fog/openstack/volume.rb b/lib/fog/openstack/volume.rb index 1c5017dbf..a5cec9f24 100644 --- a/lib/fog/openstack/volume.rb +++ b/lib/fog/openstack/volume.rb @@ -47,7 +47,6 @@ module Fog end def initialize(options={}) - require 'multi_json' @openstack_username = options[:openstack_username] @openstack_tenant = options[:openstack_tenant] @openstack_auth_uri = URI.parse(options[:openstack_auth_url]) @@ -94,8 +93,6 @@ module Fog attr_reader :current_tenant def initialize(options={}) - require 'multi_json' - @openstack_auth_token = options[:openstack_auth_token] unless @openstack_auth_token @@ -170,7 +167,7 @@ module Fog end end unless response.body.empty? - response.body = MultiJson.decode(response.body) + response.body = Fog::JSON.decode(response.body) end response end diff --git a/lib/fog/rackspace.rb b/lib/fog/rackspace.rb index 1499d4381..6132fbc84 100644 --- a/lib/fog/rackspace.rb +++ b/lib/fog/rackspace.rb @@ -1,4 +1,4 @@ -require 'fog/core' +require File.join(File.dirname(__FILE__), 'core') require 'fog/rackspace/mock_data' require 'fog/rackspace/service' diff --git a/lib/fog/riakcs/provisioning.rb b/lib/fog/riakcs/provisioning.rb index 88765c54b..a5e12250e 100644 --- a/lib/fog/riakcs/provisioning.rb +++ b/lib/fog/riakcs/provisioning.rb @@ -48,8 +48,6 @@ module Fog def initialize(options = {}) require 'mime/types' - require 'multi_json' - configure_uri_options(options) @riakcs_access_key_id = options[:riakcs_access_key_id] @riakcs_secret_access_key = options[:riakcs_secret_access_key] @@ -89,7 +87,7 @@ module Fog end end if !response.body.empty? && parse_response - response.body = MultiJson.decode(response.body) + response.body = Fog::JSON.decode(response.body) end response end diff --git a/lib/fog/riakcs/usage.rb b/lib/fog/riakcs/usage.rb index 51df897f0..3df9d3802 100644 --- a/lib/fog/riakcs/usage.rb +++ b/lib/fog/riakcs/usage.rb @@ -42,7 +42,6 @@ module Fog def initialize(options = {}) require 'mime/types' - require 'multi_json' configure_uri_options(options) @riakcs_access_key_id = options[:riakcs_access_key_id] diff --git a/lib/tasks/changelog_task.rb b/lib/tasks/changelog_task.rb index 01c2ec37b..8d13e2406 100644 --- a/lib/tasks/changelog_task.rb +++ b/lib/tasks/changelog_task.rb @@ -14,7 +14,6 @@ module Fog changelog << ('=' * changelog[0].length) changelog << '' - require 'multi_json' github_repo_data = Fog::JSON.decode(Excon.get('https://api.github.com/repos/fog/fog').body) data = github_repo_data.reject {|key, value| !['forks', 'open_issues', 'watchers'].include?(key)} github_collaborator_data = Fog::JSON.decode(Excon.get('https://api.github.com/repos/fog/fog/collaborators').body)