Skip to content

Commit

Permalink
remove JSON gem usage
Browse files Browse the repository at this point in the history
should fully convert to using ffi-yajl

there are still issues with JSON gem monkeypatching interacting with
chef-zero and the spec tests so we keep the requires here for the
json gem and the ffi_yajl/json_gem here.  when ohai and chef-zero are
fixed, we an just require ffi_yajl.
  • Loading branch information
lamont-granquist committed Aug 12, 2014
1 parent b4dfc6a commit 3f415b1
Show file tree
Hide file tree
Showing 34 changed files with 94 additions and 81 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Unreleased:

* chef/json_compat now throws its own exceptions not JSON gem exceptions
* Fix a bug in the experimental Policyfile mode that caused errors when
using templates.
* Disable JSON encoding of request body when non-JSON content type is
Expand Down
2 changes: 1 addition & 1 deletion lib/chef/api_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def to_hash
#
# @return [String] the JSON string.
def to_json(*a)
to_hash.to_json(*a)
Chef::JSONCompat.to_json(to_hash, *a)
end

def self.json_create(o)
Expand Down
2 changes: 1 addition & 1 deletion lib/chef/chef_fs/chef_fs_data_store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ def write_cookbook(path, data, *options)

# Create a little Chef::ChefFS memory filesystem with the data
cookbook_fs = Chef::ChefFS::FileSystem::MemoryRoot.new('uploading')
cookbook = JSON.parse(data, :create_additions => false)
cookbook = Chef::JSONCompat.parse(data)
cookbook.each_pair do |key, value|
if value.is_a?(Array)
value.each do |file|
Expand Down
2 changes: 1 addition & 1 deletion lib/chef/chef_fs/command_line.rb
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ def self.sort_keys(json_object)
end

def self.canonicalize_json(json_text)
parsed_json = JSON.parse(json_text, :create_additions => false)
parsed_json = Chef::JSONCompat.parse(json_text)
sorted_json = sort_keys(parsed_json)
JSON.pretty_generate(sorted_json)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/chef/chef_fs/file_system/acl_entry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def delete(recurse)

def write(file_contents)
# ACL writes are fun.
acls = data_handler.normalize(JSON.parse(file_contents, :create_additions => false), self)
acls = data_handler.normalize(Chef::JSONCompat.parse(file_contents), self)
PERMISSIONS.each do |permission|
begin
rest.put("#{api_path}/#{permission}", { permission => acls[permission] })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def data_handler

def chef_object
begin
return data_handler.chef_object(JSON.parse(read, :create_additions => false))
return data_handler.chef_object(Chef::JSONCompat.parse(read))
rescue
Chef::Log.error("Could not read #{path_for_printing} into a Chef object: #{$!}")
end
Expand All @@ -60,10 +60,10 @@ def write(file_contents)
end

def minimize(file_contents, entry)
object = JSONCompat.from_json(file_contents, :create_additions => false)
object = Chef::JSONCompat.from_json(file_contents)
object = data_handler.normalize(object, entry)
object = data_handler.minimize(object, entry)
JSONCompat.to_json_pretty(object)
Chef::JSONCompat.to_json_pretty(object)
end

def children
Expand Down
4 changes: 2 additions & 2 deletions lib/chef/chef_fs/file_system/rest_list_dir.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ def children

def create_child(name, file_contents)
begin
object = JSON.parse(file_contents, :create_additions => false)
rescue JSON::ParserError => e
object = Chef::JSONCompat.parse(file_contents)
rescue Chef::Exceptions::JSON::ParseError => e
raise Chef::ChefFS::FileSystem::OperationFailedError.new(:create_child, self, e), "Parse error reading JSON creating child '#{name}': #{e}"
end

Expand Down
8 changes: 4 additions & 4 deletions lib/chef/chef_fs/file_system/rest_list_entry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ def compare_to(other)
value = minimize_value(value)
value_json = Chef::JSONCompat.to_json_pretty(value)
begin
other_value = JSON.parse(other_value_json, :create_additions => false)
rescue JSON::ParserError => e
other_value = Chef::JSONCompat.parse(other_value_json)
rescue Chef::Exceptions::JSON::ParseError => e
Chef::Log.warn("Parse error reading #{other.path_for_printing} as JSON: #{e}")
return [ nil, value_json, other_value_json ]
end
Expand All @@ -145,8 +145,8 @@ def rest

def write(file_contents)
begin
object = JSON.parse(file_contents, :create_additions => false)
rescue JSON::ParserError => e
object = Chef::JSONCompat.parse(file_contents)
rescue Chef::Exceptions::JSON::ParseError => e
raise Chef::ChefFS::FileSystem::OperationFailedError.new(:write, self, e), "Parse error reading JSON: #{e}"
end

Expand Down
2 changes: 1 addition & 1 deletion lib/chef/config_fetcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def fetch_json
config_data = read_config
begin
Chef::JSONCompat.from_json(config_data)
rescue FFI_Yajl::ParseError => error
rescue Chef::Exceptions::JSON::ParseError => error
Chef::Application.fatal!("Could not parse the provided JSON file (#{config_location}): " + error.message, 2)
end
end
Expand Down
8 changes: 4 additions & 4 deletions lib/chef/cookbook/cookbook_version_loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ def remove_ignored_files
def apply_ruby_metadata(file)
begin
@metadata.from_file(file)
rescue JSON::ParserError
rescue Chef::Exceptions::JSON::ParseError
Chef::Log.error("Error evaluating metadata.rb for #@cookbook_name in " + file)
raise
end
Expand All @@ -179,7 +179,7 @@ def apply_ruby_metadata(file)
def apply_json_metadata(file)
begin
@metadata.from_json(IO.read(file))
rescue JSON::ParserError
rescue Chef::Exceptions::JSON::ParseError
Chef::Log.error("Couldn't parse cookbook metadata JSON for #@cookbook_name in " + file)
raise
end
Expand All @@ -189,7 +189,7 @@ def apply_json_cookbook_version_metadata(file)
begin
data = Chef::JSONCompat.from_json(IO.read(file), :create_additions => false)
@metadata.from_hash(data['metadata'])
rescue JSON::ParserError
rescue Chef::Exceptions::JSON::ParseError
Chef::Log.error("Couldn't parse cookbook metadata JSON for #@cookbook_name in " + file)
raise
end
Expand All @@ -200,7 +200,7 @@ def set_frozen
begin
data = Chef::JSONCompat.from_json(IO.read(uploaded_cookbook_version_file), :create_additions => false)
@frozen = data['frozen?']
rescue JSON::ParserError
rescue Chef::Exceptions::JSON::ParseError
Chef::Log.error("Couldn't parse cookbook metadata JSON for #@cookbook_name in #{uploaded_cookbook_version_file}")
raise
end
Expand Down
4 changes: 2 additions & 2 deletions lib/chef/cookbook/metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
require 'chef/log'
require 'chef/version_class'
require 'chef/version_constraint'
require 'chef/json_compat'

class Chef
class Cookbook
Expand Down Expand Up @@ -441,7 +442,7 @@ def to_hash
end

def to_json(*a)
self.to_hash.to_json(*a)
Chef::JSONCompat.to_json(to_hash, *a)
end

def self.from_hash(o)
Expand Down Expand Up @@ -651,6 +652,5 @@ def initialize(name, params)
end
end


end
end
5 changes: 2 additions & 3 deletions lib/chef/cookbook_version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,6 @@ def preferred_manifest_records_for_directory(node, segment, dirname)
records_by_pref[best_pref]
end


# Given a node, segment and path (filename or directory name),
# return the priority-ordered list of preference locations to
# look.
Expand Down Expand Up @@ -458,9 +457,9 @@ def to_hash
end

def to_json(*a)
result = self.to_hash
result = to_hash
result['json_class'] = self.class.name
result.to_json(*a)
Chef::JSONCompat.to_json(result, *a)
end

def self.json_create(o)
Expand Down
7 changes: 3 additions & 4 deletions lib/chef/data_bag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,16 @@ def name(arg=nil)

def to_hash
result = {
"name" => @name,
'name' => @name,
'json_class' => self.class.name,
"chef_type" => "data_bag",
'chef_type' => 'data_bag',
}
result
end

# Serialize this object as a hash
def to_json(*a)
to_hash.to_json(*a)
Chef::JSONCompat.to_json(to_hash, *a)
end

def chef_server_rest
Expand Down Expand Up @@ -164,4 +164,3 @@ def to_s

end
end

12 changes: 5 additions & 7 deletions lib/chef/data_bag_item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,13 @@ def to_hash
# Serialize this object as a hash
def to_json(*a)
result = {
"name" => self.object_name,
"name" => object_name,
"json_class" => self.class.name,
"chef_type" => "data_bag_item",
"data_bag" => self.data_bag,
"raw_data" => self.raw_data
"chef_type" => "data_bag_item",
"data_bag" => data_bag,
"raw_data" => raw_data
}
result.to_json(*a)
Chef::JSONCompat.to_json(result, *a)
end

def self.from_hash(h)
Expand Down Expand Up @@ -210,5 +210,3 @@ def id

end
end


7 changes: 3 additions & 4 deletions lib/chef/encrypted_data_bag_item/decryptor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#

require 'yaml'
require 'ffi_yajl'
require 'chef/json_compat'
require 'openssl'
require 'base64'
require 'digest/sha2'
Expand Down Expand Up @@ -121,8 +121,8 @@ def initialize(encrypted_data, key)
end

def for_decrypted_item
FFI_Yajl::Parser.parse(decrypted_data)["json_wrapper"]
rescue FFI_Yajl::ParseError
Chef::JSONCompat.parse(decrypted_data)["json_wrapper"]
rescue Chef::Exceptions::JSON::ParseError
# convert to a DecryptionFailure error because the most likely scenario
# here is that the decryption step was unsuccessful but returned bad
# data rather than raising an error.
Expand Down Expand Up @@ -197,7 +197,6 @@ def initialize(encrypted_data, key)
assert_aead_requirements_met!(algorithm)
end


# Returns the used decryption algorithm
def algorithm
AEAD_ALGORITHM
Expand Down
4 changes: 1 addition & 3 deletions lib/chef/environment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ def to_hash
end

def to_json(*a)
to_hash.to_json(*a)
Chef::JSONCompat.to_json(to_hash, *a)
end

def update_from!(o)
Expand All @@ -140,7 +140,6 @@ def update_from!(o)
self
end


def update_attributes_from_params(params)
unless params[:default_attributes].nil? || params[:default_attributes].size == 0
default_attributes(Chef::JSONCompat.from_json(params[:default_attributes]))
Expand Down Expand Up @@ -213,7 +212,6 @@ def validate_required_attrs_present
end
end


def self.json_create(o)
environment = new
environment.name(o["name"])
Expand Down
11 changes: 8 additions & 3 deletions lib/chef/exceptions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,6 @@ def initialize(message_or_expansion=NULL)
end
end


end
# Exception class for collecting multiple failures. Used when running
# delayed notifications so that chef can process each delayed
Expand Down Expand Up @@ -263,7 +262,7 @@ def to_json(*a)
"non_existent_cookbooks" => non_existent_cookbooks,
"cookbooks_with_no_versions" => cookbooks_with_no_matching_versions
}
result.to_json(*a)
Chef::JSONCompat.to_json(result, *a)
end
end

Expand Down Expand Up @@ -298,7 +297,7 @@ def to_json(*a)
"non_existent_cookbooks" => non_existent_cookbooks,
"most_constrained_cookbooks" => most_constrained_cookbooks
}
result.to_json(*a)
Chef::JSONCompat.to_json(result, *a)
end
end

Expand Down Expand Up @@ -339,5 +338,11 @@ def initialize(res_cksum, cont_cksum)
end

class BadProxyURI < RuntimeError; end

# Raised by Chef::JSONCompat
class JSON
class EncodeError < RuntimeError; end
class ParseError < RuntimeError; end
end
end
end
1 change: 0 additions & 1 deletion lib/chef/handler/json_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ def build_report_dir
end
end


end
end
end
Loading

0 comments on commit 3f415b1

Please sign in to comment.