1
0
Fork 0
mirror of https://github.com/fog/fog.git synced 2022-11-09 13:51:43 -05:00

Merge pull request #1730 from rackspace/json_lib

[Core] Updated Fog::JSON to use JSON gem by default
This commit is contained in:
Kyle Rames 2013-04-08 08:13:46 -07:00
commit 7b07d74e14
38 changed files with 147 additions and 57 deletions

View file

@ -1,3 +1,5 @@
source "https://rubygems.org"
gemspec
gem "multi_json", '~>1.0'

View file

@ -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')

View file

@ -1,7 +1,51 @@
require 'multi_json'
require 'singleton'
require 'fog/core/logger'
module Fog
module JSON
class JSON
include Singleton
class LoadError < StandardError
attr_reader :data
def initialize(message='', backtrace=[], data='')
super(message)
self.set_backtrace(backtrace)
@data = data
end
end
module LegacyJSON
def encode(obj)
::JSON.generate(obj)
end
def decode(obj)
::JSON.parse(obj)
rescue ::JSON::ParserError => e
raise LoadError.new(e.message, e.backtrace, obj)
end
end
module NewJSON
def encode(obj)
::MultiJson.encode(obj)
end
def decode(obj)
::MultiJson.decode(obj)
rescue MultiJson::LoadError => e
raise LoadError.new(e.message, e.backtrace, obj)
end
end
begin
require 'multi_json'
include NewJSON
rescue Exception => e
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 +64,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

View file

@ -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']

View file

@ -25,7 +25,7 @@ module Fog
if message.nil? and !data.values.first.nil?
message = data.values.first['message']
end
rescue MultiJson::DecodeError
rescue Fog::JSON::LoadError
message = error.response.body #### body is not in JSON format, so just return as is
end
end

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -6,7 +6,7 @@ module Fog
def allocate_address(pool = nil)
request(
:body => MultiJson.encode({'pool' => pool}),
:body => Fog::JSON.encode({'pool' => pool}),
:expects => [200, 202],
:method => 'POST',
:path => 'os-floating-ips.json'

View file

@ -11,7 +11,7 @@ module Fog
}
}
request(
:body => MultiJson.encode(data),
:body => Fog::JSON.encode(data),
:expects => [200, 202],
:method => 'POST',
:path => "servers/%s/os-volume_attachments" % [server_id]

View file

@ -29,7 +29,7 @@ module Fog
end
request(
:body => MultiJson.encode(data),
:body => Fog::JSON.encode(data),
:expects => [200, 202],
:method => 'POST',
:path => '/os-volumes_boot.json'

View file

@ -35,7 +35,7 @@ module Fog
}
request(
:body => MultiJson.encode(data),
:body => Fog::JSON.encode(data),
:expects => 200,
:method => 'POST',
:path => 'flavors'

View file

@ -14,7 +14,7 @@ module Fog
data['keypair']['public_key'] = public_key unless public_key.nil?
request(
:body => MultiJson.encode(data),
:body => Fog::JSON.encode(data),
:expects => 200,
:method => 'POST',
:path => 'os-keypairs.json'

View file

@ -12,7 +12,7 @@ module Fog
}
request(
:body => MultiJson.encode(data),
:body => Fog::JSON.encode(data),
:expects => 200,
:method => 'POST',
:path => 'os-security-groups.json'

View file

@ -18,7 +18,7 @@ module Fog
request(
:expects => 200,
:method => 'POST',
:body => MultiJson.encode(data),
:body => Fog::JSON.encode(data),
:path => 'os-security-group-rules.json'
)
end

View file

@ -17,7 +17,7 @@ module Fog
data['volume'][key] = options[key]
end
request(
:body => MultiJson.encode(data),
:body => Fog::JSON.encode(data),
:expects => [200, 202],
:method => 'POST',
:path => "os-volumes"

View file

@ -14,7 +14,7 @@ module Fog
}
request(
:body => MultiJson.encode(data),
:body => Fog::JSON.encode(data),
:expects => [200, 202],
:method => 'POST',
:path => "os-snapshots"

View file

@ -11,7 +11,7 @@ module Fog
:method => 'GET',
:path => '/v2.0/tenants'
})
response.body = MultiJson.decode(response.body)
response.body = Fog::JSON.decode(response.body)
response
end
end

View file

@ -6,7 +6,7 @@ module Fog
def update_quota(tenant_id, options = {})
options['tenant_id'] = tenant_id
request(
:body => MultiJson.encode({ 'quota_set' => options }),
:body => Fog::JSON.encode({ 'quota_set' => options }),
:expects => 200,
:method => 'PUT',
:path => "/os-quota-sets/#{tenant_id}"

View file

@ -26,7 +26,7 @@ module Fog
data = { 'tenant_id' => tenant_id }
request(
:body => MultiJson.encode(data),
:body => Fog::JSON.encode(data),
:expects => [200, 202],
:method => 'POST',
:path => "users/#{user_id}/credentials/OS-EC2"

View file

@ -10,7 +10,7 @@ module Fog
}
request(
:body => MultiJson.encode(data),
:body => Fog::JSON.encode(data),
:expects => [200, 202],
:method => 'POST',
:path => '/OS-KSADM/roles'

View file

@ -15,7 +15,7 @@ module Fog
}
request(
:body => MultiJson.encode(data),
:body => Fog::JSON.encode(data),
:expects => [200, 202],
:method => 'POST',
:path => '/users'

View file

@ -6,7 +6,7 @@ module Fog
def update_user(user_id, options = {})
url = options.delete('url') || "/users/#{user_id}"
request(
:body => MultiJson.encode({ 'user' => options }),
:body => Fog::JSON.encode({ 'user' => options }),
:expects => 200,
:method => 'PUT',
:path => url

View file

@ -11,7 +11,7 @@ module Fog
data = { 'memberships' => members }
request(
:body => MultiJson.encode(data),
:body => Fog::JSON.encode(data),
:expects => [200, 202],
:method => 'PUT',
:path => "images/#{image_id}/members"

View file

@ -17,7 +17,7 @@ module Fog
data['volume'][key] = options[key]
end
request(
:body => MultiJson.encode(data),
:body => Fog::JSON.encode(data),
:expects => [200, 202],
:method => 'POST',
:path => "volumes"

View file

@ -14,7 +14,7 @@ module Fog
}
request(
:body => MultiJson.encode(data),
:body => Fog::JSON.encode(data),
:expects => [200, 202],
:method => 'POST',
:path => "snapshots"

View file

@ -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

View file

@ -1,4 +1,4 @@
require 'fog/core'
require File.join(File.dirname(__FILE__), 'core')
require 'fog/rackspace/mock_data'
require 'fog/rackspace/service'

View file

@ -160,7 +160,7 @@ module Fog
unless response.body.empty?
begin
response.body = Fog::JSON.decode(response.body)
rescue MultiJson::DecodeError => e
rescue Fog::JSON::LoadError => e
response.body = {}
end
end

View file

@ -71,9 +71,9 @@ module Fog
module UserUtils
def update_riakcs_user(key_id, user)
response = @s3_connection.put_object('riak-cs', "user/#{key_id}", MultiJson.encode(user), { 'Content-Type' => 'application/json' })
response = @s3_connection.put_object('riak-cs', "user/#{key_id}", Fog::JSON.encode(user), { 'Content-Type' => 'application/json' })
if !response.body.empty?
response.body = MultiJson.decode(response.body)
response.body = Fog::JSON.decode(response.body)
end
response
end

View file

@ -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

View file

@ -3,7 +3,7 @@ module Fog
class Provisioning
class Real
def create_user(email, name, options = {})
payload = MultiJson.encode({ :email => email, :name => name })
payload = Fog::JSON.encode({ :email => email, :name => name })
headers = { 'Content-Type' => 'application/json' }
if(options[:anonymous])
@ -20,7 +20,7 @@ module Fog
if !response.body.empty?
case response.headers['Content-Type']
when 'application/json'
response.body = MultiJson.decode(response.body)
response.body = Fog::JSON.decode(response.body)
end
end
response

View file

@ -7,7 +7,7 @@ module Fog
def get_user(key_id)
response = @s3_connection.get_object('riak-cs', "user/#{key_id}", { 'Accept' => 'application/json' })
response.body = MultiJson.decode(response.body)
response.body = Fog::JSON.decode(response.body)
response
end
end

View file

@ -10,7 +10,7 @@ module Fog
boundary = extract_boundary(response.headers['Content-Type'])
parts = parse(response.body, boundary)
decoded = parts.map { |part| MultiJson.decode(part[:body]) }
decoded = parts.map { |part| Fog::JSON.decode(part[:body]) }
response.body = decoded.flatten

View file

@ -37,7 +37,7 @@ module Fog
response = @connection.get_object('riak-cs', ["usage", request_uri(access_key_id, options)].join("/"))
if !response.body.empty?
response.body = MultiJson.decode(response.body)
response.body = Fog::JSON.decode(response.body)
end
response
end

View file

@ -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]

View file

@ -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)

62
tests/core/json_tests.rb Normal file
View file

@ -0,0 +1,62 @@
Shindo.tests('Fog#JSON', 'core') do
TEST_HASH = {'quantity' => 4}
VALID_JSON = "{\"quantity\":4}"
tests('LegacyJSON') do
begin
require 'json'
rescue => e
returns('Failed to load JSON library', true) { false }
end
pending? unless defined? ::JSON
class LegacyJSONTester
include Fog::JSON::LegacyJSON
end
@tester = LegacyJSONTester.new
tests('encode').returns(VALID_JSON) do
@tester.encode(TEST_HASH)
end
tests('decode').returns(TEST_HASH) do
@tester.decode(VALID_JSON)
end
tests('invalid decode').raises(Fog::JSON::LoadError) do
@tester.decode("I am not json")
end
end
tests('NewJSON') do
begin
require 'multi_json'
rescue => e
returns('Failed to load multi_json library', true) { false }
end
pending unless defined? ::MultiJson
class NewJSONTester
include Fog::JSON::NewJSON
end
@tester = NewJSONTester.new
tests('encode').returns(VALID_JSON) do
@tester.encode(TEST_HASH)
end
tests('decode').returns(TEST_HASH) do
@tester.decode(VALID_JSON)
end
tests('invalid decode').raises(Fog::JSON::LoadError) do
@tester.decode("I am not json")
end
end
end