Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #1730 from rackspace/json_lib

[Core] Updated Fog::JSON to use JSON gem by default
  • Loading branch information...
commit 7b07d74e14e9cfeede7856fefd61e21ba735302c 2 parents e634e90 + 7115207
Kyle Rames authored
Showing with 147 additions and 57 deletions.
  1. +2 −0  Gemfile
  2. +1 −1  fog.gemspec
  3. +48 −7 lib/fog/core/json.rb
  4. +1 −3 lib/fog/dreamhost/dns.rb
  5. +1 −1  lib/fog/hp.rb
  6. +1 −2  lib/fog/openstack/identity.rb
  7. +1 −4 lib/fog/openstack/image.rb
  8. +1 −3 lib/fog/openstack/network.rb
  9. +1 −1  lib/fog/openstack/requests/compute/allocate_address.rb
  10. +1 −1  lib/fog/openstack/requests/compute/attach_volume.rb
  11. +1 −1  lib/fog/openstack/requests/compute/boot_from_snapshot.rb
  12. +1 −1  lib/fog/openstack/requests/compute/create_flavor.rb
  13. +1 −1  lib/fog/openstack/requests/compute/create_key_pair.rb
  14. +1 −1  lib/fog/openstack/requests/compute/create_security_group.rb
  15. +1 −1  lib/fog/openstack/requests/compute/create_security_group_rule.rb
  16. +1 −1  lib/fog/openstack/requests/compute/create_volume.rb
  17. +1 −1  lib/fog/openstack/requests/compute/create_volume_snapshot.rb
  18. +1 −1  lib/fog/openstack/requests/compute/list_tenants.rb
  19. +1 −1  lib/fog/openstack/requests/compute/update_quota.rb
  20. +1 −1  lib/fog/openstack/requests/identity/create_ec2_credential.rb
  21. +1 −1  lib/fog/openstack/requests/identity/create_role.rb
  22. +1 −1  lib/fog/openstack/requests/identity/create_user.rb
  23. +1 −1  lib/fog/openstack/requests/identity/update_user.rb
  24. +1 −1  lib/fog/openstack/requests/image/update_image_members.rb
  25. +1 −1  lib/fog/openstack/requests/volume/create_volume.rb
  26. +1 −1  lib/fog/openstack/requests/volume/create_volume_snapshot.rb
  27. +1 −4 lib/fog/openstack/volume.rb
  28. +1 −1  lib/fog/rackspace.rb
  29. +1 −1  lib/fog/rackspace/compute_v2.rb
  30. +2 −2 lib/fog/riakcs.rb
  31. +1 −3 lib/fog/riakcs/provisioning.rb
  32. +2 −2 lib/fog/riakcs/requests/provisioning/create_user.rb
  33. +1 −1  lib/fog/riakcs/requests/provisioning/get_user.rb
  34. +1 −1  lib/fog/riakcs/requests/provisioning/list_users.rb
  35. +1 −1  lib/fog/riakcs/requests/usage/get_usage.rb
  36. +0 −1  lib/fog/riakcs/usage.rb
  37. +0 −1  lib/tasks/changelog_task.rb
  38. +62 −0 tests/core/json_tests.rb
2  Gemfile
View
@@ -1,3 +1,5 @@
source "https://rubygems.org"
gemspec
+
+gem "multi_json", '~>1.0'
2  fog.gemspec
View
@@ -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')
55 lib/fog/core/json.rb
View
@@ -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 @@ def self.sanitize(data)
# 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
4 lib/fog/dreamhost/dns.rb
View
@@ -47,8 +47,6 @@ def reset_data
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 @@ def request(params)
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']
2  lib/fog/hp.rb
View
@@ -25,7 +25,7 @@ def self.slurp(error)
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
3  lib/fog/openstack/identity.rb
View
@@ -96,7 +96,6 @@ def self.reset!
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 @@ def request(params)
end
end
unless response.body.empty?
- response.body = MultiJson.decode(response.body)
+ response.body = Fog::JSON.decode(response.body)
end
response
end
5 lib/fog/openstack/image.rb
View
@@ -44,7 +44,6 @@ def self.reset
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 @@ class Real
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 @@ def request(params)
end
end
unless response.body.empty?
- response.body = MultiJson.decode(response.body)
+ response.body = Fog::JSON.decode(response.body)
end
response
end
4 lib/fog/openstack/network.rb
View
@@ -112,8 +112,6 @@ class Real
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 @@ def request(params)
end
end
unless response.body.empty?
- response.body = MultiJson.decode(response.body)
+ response.body = Fog::JSON.decode(response.body)
end
response
end
2  lib/fog/openstack/requests/compute/allocate_address.rb
View
@@ -6,7 +6,7 @@ class Real
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'
2  lib/fog/openstack/requests/compute/attach_volume.rb
View
@@ -11,7 +11,7 @@ def attach_volume(volume_id, server_id, device)
}
}
request(
- :body => MultiJson.encode(data),
+ :body => Fog::JSON.encode(data),
:expects => [200, 202],
:method => 'POST',
:path => "servers/%s/os-volume_attachments" % [server_id]
2  lib/fog/openstack/requests/compute/boot_from_snapshot.rb
View
@@ -29,7 +29,7 @@ def boot_from_snapshot(name, image_ref, flavor_ref, options={})
end
request(
- :body => MultiJson.encode(data),
+ :body => Fog::JSON.encode(data),
:expects => [200, 202],
:method => 'POST',
:path => '/os-volumes_boot.json'
2  lib/fog/openstack/requests/compute/create_flavor.rb
View
@@ -35,7 +35,7 @@ def create_flavor(attributes)
}
request(
- :body => MultiJson.encode(data),
+ :body => Fog::JSON.encode(data),
:expects => 200,
:method => 'POST',
:path => 'flavors'
2  lib/fog/openstack/requests/compute/create_key_pair.rb
View
@@ -14,7 +14,7 @@ def create_key_pair(key_name, public_key = nil)
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'
2  lib/fog/openstack/requests/compute/create_security_group.rb
View
@@ -12,7 +12,7 @@ def create_security_group(name, description)
}
request(
- :body => MultiJson.encode(data),
+ :body => Fog::JSON.encode(data),
:expects => 200,
:method => 'POST',
:path => 'os-security-groups.json'
2  lib/fog/openstack/requests/compute/create_security_group_rule.rb
View
@@ -18,7 +18,7 @@ def create_security_group_rule(parent_group_id, ip_protocol, from_port, to_port,
request(
:expects => 200,
:method => 'POST',
- :body => MultiJson.encode(data),
+ :body => Fog::JSON.encode(data),
:path => 'os-security-group-rules.json'
)
end
2  lib/fog/openstack/requests/compute/create_volume.rb
View
@@ -17,7 +17,7 @@ def create_volume(name, description, size, options={})
data['volume'][key] = options[key]
end
request(
- :body => MultiJson.encode(data),
+ :body => Fog::JSON.encode(data),
:expects => [200, 202],
:method => 'POST',
:path => "os-volumes"
2  lib/fog/openstack/requests/compute/create_volume_snapshot.rb
View
@@ -14,7 +14,7 @@ def create_volume_snapshot(volume_id, name, description, force=false)
}
request(
- :body => MultiJson.encode(data),
+ :body => Fog::JSON.encode(data),
:expects => [200, 202],
:method => 'POST',
:path => "os-snapshots"
2  lib/fog/openstack/requests/compute/list_tenants.rb
View
@@ -11,7 +11,7 @@ def list_tenants
:method => 'GET',
:path => '/v2.0/tenants'
})
- response.body = MultiJson.decode(response.body)
+ response.body = Fog::JSON.decode(response.body)
response
end
end
2  lib/fog/openstack/requests/compute/update_quota.rb
View
@@ -6,7 +6,7 @@ class Real
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}"
2  lib/fog/openstack/requests/identity/create_ec2_credential.rb
View
@@ -26,7 +26,7 @@ def create_ec2_credential(user_id, tenant_id)
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"
2  lib/fog/openstack/requests/identity/create_role.rb
View
@@ -10,7 +10,7 @@ def create_role(name)
}
request(
- :body => MultiJson.encode(data),
+ :body => Fog::JSON.encode(data),
:expects => [200, 202],
:method => 'POST',
:path => '/OS-KSADM/roles'
2  lib/fog/openstack/requests/identity/create_user.rb
View
@@ -15,7 +15,7 @@ def create_user(name, password, email, tenantId=nil, enabled=true)
}
request(
- :body => MultiJson.encode(data),
+ :body => Fog::JSON.encode(data),
:expects => [200, 202],
:method => 'POST',
:path => '/users'
2  lib/fog/openstack/requests/identity/update_user.rb
View
@@ -6,7 +6,7 @@ class Real
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
2  lib/fog/openstack/requests/image/update_image_members.rb
View
@@ -11,7 +11,7 @@ def update_image_members(image_id, members)
data = { 'memberships' => members }
request(
- :body => MultiJson.encode(data),
+ :body => Fog::JSON.encode(data),
:expects => [200, 202],
:method => 'PUT',
:path => "images/#{image_id}/members"
2  lib/fog/openstack/requests/volume/create_volume.rb
View
@@ -17,7 +17,7 @@ def create_volume(name, description, size, options={})
data['volume'][key] = options[key]
end
request(
- :body => MultiJson.encode(data),
+ :body => Fog::JSON.encode(data),
:expects => [200, 202],
:method => 'POST',
:path => "volumes"
2  lib/fog/openstack/requests/volume/create_volume_snapshot.rb
View
@@ -14,7 +14,7 @@ def create_volume_snapshot(volume_id, name, description, force=false)
}
request(
- :body => MultiJson.encode(data),
+ :body => Fog::JSON.encode(data),
:expects => [200, 202],
:method => 'POST',
:path => "snapshots"
5 lib/fog/openstack/volume.rb
View
@@ -47,7 +47,6 @@ def self.reset
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 @@ class Real
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 @@ def request(params)
end
end
unless response.body.empty?
- response.body = MultiJson.decode(response.body)
+ response.body = Fog::JSON.decode(response.body)
end
response
end
2  lib/fog/rackspace.rb
View
@@ -1,4 +1,4 @@
-require 'fog/core'
+require File.join(File.dirname(__FILE__), 'core')
require 'fog/rackspace/mock_data'
require 'fog/rackspace/service'
2  lib/fog/rackspace/compute_v2.rb
View
@@ -160,7 +160,7 @@ def request(params)
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
4 lib/fog/riakcs.rb
View
@@ -71,9 +71,9 @@ def parse_multipart_section(part)
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
4 lib/fog/riakcs/provisioning.rb
View
@@ -48,8 +48,6 @@ class Real
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 @@ def request(params, parse_response = true, &block)
end
end
if !response.body.empty? && parse_response
- response.body = MultiJson.decode(response.body)
+ response.body = Fog::JSON.decode(response.body)
end
response
end
4 lib/fog/riakcs/requests/provisioning/create_user.rb
View
@@ -3,7 +3,7 @@ module RiakCS
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 @@ def create_user(email, name, options = {})
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
2  lib/fog/riakcs/requests/provisioning/get_user.rb
View
@@ -7,7 +7,7 @@ class Real
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
2  lib/fog/riakcs/requests/provisioning/list_users.rb
View
@@ -10,7 +10,7 @@ def list_users(options = {})
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
2  lib/fog/riakcs/requests/usage/get_usage.rb
View
@@ -37,7 +37,7 @@ def get_usage(access_key_id, options = {})
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
1  lib/fog/riakcs/usage.rb
View
@@ -42,7 +42,6 @@ class Real
def initialize(options = {})
require 'mime/types'
- require 'multi_json'
configure_uri_options(options)
@riakcs_access_key_id = options[:riakcs_access_key_id]
1  lib/tasks/changelog_task.rb
View
@@ -14,7 +14,6 @@ def initialize
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
View
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.