From adbb0fa85cc45b93ba91dc3019094b312cafb35c Mon Sep 17 00:00:00 2001 From: Lamont Granquist Date: Sat, 9 Aug 2014 10:04:58 -0700 Subject: remove #to_json Monkeypatching --- lib/chef/api_client.rb | 2 +- .../chef_fs/file_system/chef_repository_file_system_entry.rb | 1 + lib/chef/chef_fs/file_system/rest_list_entry.rb | 2 ++ lib/chef/cookbook/metadata.rb | 4 ++-- lib/chef/cookbook_version.rb | 5 ++--- lib/chef/data_bag.rb | 7 +++---- lib/chef/data_bag_item.rb | 12 +++++------- lib/chef/environment.rb | 4 +--- lib/chef/exceptions.rb | 5 ++--- lib/chef/handler/json_file.rb | 1 - lib/chef/json_compat.rb | 10 ++++++---- lib/chef/knife/cookbook_site_share.rb | 2 +- lib/chef/node.rb | 3 +-- lib/chef/provider/deploy/revision.rb | 2 +- lib/chef/resource.rb | 4 +--- lib/chef/resource_collection.rb | 10 ++++++---- lib/chef/resource_reporter.rb | 6 +++--- lib/chef/role.rb | 2 +- lib/chef/run_list.rb | 9 ++++++--- lib/chef/user.rb | 2 +- lib/chef/util/diff.rb | 3 +-- 21 files changed, 47 insertions(+), 49 deletions(-) (limited to 'lib/chef') diff --git a/lib/chef/api_client.rb b/lib/chef/api_client.rb index 7b7fd99ff7..e8585106e4 100644 --- a/lib/chef/api_client.rb +++ b/lib/chef/api_client.rb @@ -121,7 +121,7 @@ class Chef # # @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) diff --git a/lib/chef/chef_fs/file_system/chef_repository_file_system_entry.rb b/lib/chef/chef_fs/file_system/chef_repository_file_system_entry.rb index 3d3f58201e..1b99a0a9cd 100644 --- a/lib/chef/chef_fs/file_system/chef_repository_file_system_entry.rb +++ b/lib/chef/chef_fs/file_system/chef_repository_file_system_entry.rb @@ -41,6 +41,7 @@ class Chef def chef_object begin + # FIXME: FFI_Yajl needs to support :create_additions and this needs to use it return data_handler.chef_object(JSON.parse(read, :create_additions => false)) rescue Chef::Log.error("Could not read #{path_for_printing} into a Chef object: #{$!}") diff --git a/lib/chef/chef_fs/file_system/rest_list_entry.rb b/lib/chef/chef_fs/file_system/rest_list_entry.rb index 0d5557de1d..ce1b27df6a 100644 --- a/lib/chef/chef_fs/file_system/rest_list_entry.rb +++ b/lib/chef/chef_fs/file_system/rest_list_entry.rb @@ -128,6 +128,7 @@ class Chef value = minimize_value(value) value_json = Chef::JSONCompat.to_json_pretty(value) begin + # FIXME: replace with FFI_Yajl when it supports :create_additions other_value = JSON.parse(other_value_json, :create_additions => false) rescue JSON::ParserError => e Chef::Log.warn("Parse error reading #{other.path_for_printing} as JSON: #{e}") @@ -145,6 +146,7 @@ class Chef def write(file_contents) begin + # FIXME: replace with FFI_Yajl when it supports :create_additions object = JSON.parse(file_contents, :create_additions => false) rescue JSON::ParserError => e raise Chef::ChefFS::FileSystem::OperationFailedError.new(:write, self, e), "Parse error reading JSON: #{e}" diff --git a/lib/chef/cookbook/metadata.rb b/lib/chef/cookbook/metadata.rb index 32574c75e2..1ab7760209 100644 --- a/lib/chef/cookbook/metadata.rb +++ b/lib/chef/cookbook/metadata.rb @@ -24,6 +24,7 @@ require 'chef/mixin/params_validate' require 'chef/log' require 'chef/version_class' require 'chef/version_constraint' +require 'chef/json_compat' class Chef class Cookbook @@ -441,7 +442,7 @@ class Chef end def to_json(*a) - self.to_hash.to_json(*a) + ::Chef::JSONCompat.to_json(to_hash, *a) end def self.from_hash(o) @@ -651,6 +652,5 @@ INVALID end end - end end diff --git a/lib/chef/cookbook_version.rb b/lib/chef/cookbook_version.rb index 778a6043bf..2291caabd8 100644 --- a/lib/chef/cookbook_version.rb +++ b/lib/chef/cookbook_version.rb @@ -405,7 +405,6 @@ class Chef 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. @@ -458,9 +457,9 @@ class Chef 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) diff --git a/lib/chef/data_bag.rb b/lib/chef/data_bag.rb index 1133aa3e73..a901517a65 100644 --- a/lib/chef/data_bag.rb +++ b/lib/chef/data_bag.rb @@ -54,16 +54,16 @@ class Chef 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 @@ -164,4 +164,3 @@ class Chef end end - diff --git a/lib/chef/data_bag_item.rb b/lib/chef/data_bag_item.rb index 07dd15a1dc..a537880f41 100644 --- a/lib/chef/data_bag_item.rb +++ b/lib/chef/data_bag_item.rb @@ -112,13 +112,13 @@ class Chef # 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) @@ -210,5 +210,3 @@ class Chef end end - - diff --git a/lib/chef/environment.rb b/lib/chef/environment.rb index 5c719ca285..84487492d0 100644 --- a/lib/chef/environment.rb +++ b/lib/chef/environment.rb @@ -129,7 +129,7 @@ class Chef end def to_json(*a) - to_hash.to_json(*a) + ::Chef::JSONCompat.to_json(to_hash, *a) end def update_from!(o) @@ -140,7 +140,6 @@ class Chef 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])) @@ -213,7 +212,6 @@ class Chef end end - def self.json_create(o) environment = new environment.name(o["name"]) diff --git a/lib/chef/exceptions.rb b/lib/chef/exceptions.rb index 8c1a285095..9f4e1edd66 100644 --- a/lib/chef/exceptions.rb +++ b/lib/chef/exceptions.rb @@ -195,7 +195,6 @@ class Chef end end - end # Exception class for collecting multiple failures. Used when running # delayed notifications so that chef can process each delayed @@ -263,7 +262,7 @@ class Chef "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 @@ -298,7 +297,7 @@ class Chef "non_existent_cookbooks" => non_existent_cookbooks, "most_constrained_cookbooks" => most_constrained_cookbooks } - result.to_json(*a) + ::Chef::JSONCompat.to_json(result, *a) end end diff --git a/lib/chef/handler/json_file.rb b/lib/chef/handler/json_file.rb index 3473db1838..405c91795e 100644 --- a/lib/chef/handler/json_file.rb +++ b/lib/chef/handler/json_file.rb @@ -58,7 +58,6 @@ class Chef end end - end end end diff --git a/lib/chef/json_compat.rb b/lib/chef/json_compat.rb index 2dbb607d9b..baab1386f6 100644 --- a/lib/chef/json_compat.rb +++ b/lib/chef/json_compat.rb @@ -18,7 +18,6 @@ # Wrapper class for interacting with JSON. require 'ffi_yajl' -require 'ffi_yajl/json_gem' # XXX: parts of chef require JSON gem's Hash#to_json monkeypatch class Chef class JSONCompat @@ -88,14 +87,17 @@ class Chef end def to_json(obj, opts = nil) - obj.to_json(opts) + ::FFI_Yajl::Encoder.encode(obj, opts) end def to_json_pretty(obj, opts = nil) - ::JSON.pretty_generate(obj, opts) + opts ||= {} + options_map = {} + options_map[:pretty] = true + options_map[:indent] = opts[:indent] if opts.has_key?(:indent) + ::FFI_Yajl::Encoder.encode(obj, options_map).chomp end - # Map +json_class+ to a Class object. We use a +case+ instead of a Hash # assigned to a constant because otherwise this file could not be loaded # until all the constants were defined, which means you'd have to load diff --git a/lib/chef/knife/cookbook_site_share.rb b/lib/chef/knife/cookbook_site_share.rb index 330f3cb229..08841191fe 100644 --- a/lib/chef/knife/cookbook_site_share.rb +++ b/lib/chef/knife/cookbook_site_share.rb @@ -87,7 +87,7 @@ class Chef def do_upload(cookbook_filename, cookbook_category, user_id, user_secret_filename) uri = "http://cookbooks.opscode.com/api/v1/cookbooks" - category_string = { 'category'=>cookbook_category }.to_json + category_string = ::Chef::JSONCompat.to_json({ 'category'=>cookbook_category }) http_resp = Chef::CookbookSiteStreamingUploader.post(uri, user_id, user_secret_filename, { :tarball => File.open(cookbook_filename), diff --git a/lib/chef/node.rb b/lib/chef/node.rb index 17ec1d0f0a..e9c0d09751 100644 --- a/lib/chef/node.rb +++ b/lib/chef/node.rb @@ -181,7 +181,6 @@ class Chef attributes.override end - def override_attrs attributes.override end @@ -416,7 +415,7 @@ class Chef # Serialize this object as a hash def to_json(*a) - for_json.to_json(*a) + ::Chef::JSONCompat.to_json(for_json, *a) end def for_json diff --git a/lib/chef/provider/deploy/revision.rb b/lib/chef/provider/deploy/revision.rb index f1eb171cd7..386718532a 100644 --- a/lib/chef/provider/deploy/revision.rb +++ b/lib/chef/provider/deploy/revision.rb @@ -97,7 +97,7 @@ class Chef end def save_cache(cache) - Chef::FileCache.store("revision-deploys/#{new_resource.name}", cache.to_json) + Chef::FileCache.store("revision-deploys/#{new_resource.name}", ::Chef::JSONCompat.to_json(cache)) cache end diff --git a/lib/chef/resource.rb b/lib/chef/resource.rb index 6c8e0434a0..dfbef293ec 100644 --- a/lib/chef/resource.rb +++ b/lib/chef/resource.rb @@ -134,7 +134,6 @@ F extend Chef::Mixin::ConvertToClassName - if Module.method(:const_defined?).arity == 1 def self.strict_const_defined?(const) const_defined?(const) @@ -277,7 +276,6 @@ F end end - def updated=(true_or_false) Chef::Log.warn("Chef::Resource#updated=(true|false) is deprecated. Please call #updated_by_last_action(true|false) instead.") Chef::Log.warn("Called from:") @@ -544,7 +542,7 @@ F # Serialize this object as a hash def to_json(*a) results = as_json - results.to_json(*a) + ::Chef::JSONCompat.to_json(results, *a) end def to_hash diff --git a/lib/chef/resource_collection.rb b/lib/chef/resource_collection.rb index a528a18aed..e22e6ce026 100644 --- a/lib/chef/resource_collection.rb +++ b/lib/chef/resource_collection.rb @@ -158,7 +158,6 @@ class Chef # compat. alias_method :resources, :find - # Returns true if +query_object+ is a valid string for looking up a # resource, or raises InvalidResourceSpecification if not. # === Arguments @@ -189,16 +188,19 @@ class Chef end # Serialize this object as a hash - def to_json(*a) + def to_hash instance_vars = Hash.new self.instance_variables.each do |iv| instance_vars[iv] = self.instance_variable_get(iv) end - results = { + { 'json_class' => self.class.name, 'instance_vars' => instance_vars } - results.to_json(*a) + end + + def to_json(*a) + ::Chef::JSONCompat.to_json(to_hash, *a) end def self.json_create(o) diff --git a/lib/chef/resource_reporter.rb b/lib/chef/resource_reporter.rb index 046e4e82c6..78e41c1556 100644 --- a/lib/chef/resource_reporter.rb +++ b/lib/chef/resource_reporter.rb @@ -230,7 +230,7 @@ class Chef resource_history_url = "reports/nodes/#{node_name}/runs/#{run_id}" Chef::Log.info("Sending resource update report (run-id: #{run_id})") Chef::Log.debug run_data.inspect - compressed_data = encode_gzip(run_data.to_json) + compressed_data = encode_gzip(::Chef::JSONCompat.to_json(run_data)) Chef::Log.debug("Sending compressed run data...") # Since we're posting compressed data we can not directly call post_rest which expects JSON reporting_url = @rest_client.create_url(resource_history_url) @@ -273,7 +273,7 @@ class Chef resource_record.for_json end run_data["status"] = @status - run_data["run_list"] = @run_status.node.run_list.to_json + run_data["run_list"] = ::Chef::JSONCompat.to_json(@run_status.node.run_list) run_data["total_res_count"] = @total_res_count.to_s run_data["data"] = {} run_data["start_time"] = start_time.to_s @@ -283,7 +283,7 @@ class Chef exception_data = {} exception_data["class"] = exception.inspect exception_data["message"] = exception.message - exception_data["backtrace"] = exception.backtrace.to_json + exception_data["backtrace"] = ::Chef::JSONCompat.to_json(exception.backtrace) exception_data["description"] = @error_descriptions run_data["data"]["exception"] = exception_data end diff --git a/lib/chef/role.rb b/lib/chef/role.rb index 57f3a2aa29..232a7e18cd 100644 --- a/lib/chef/role.rb +++ b/lib/chef/role.rb @@ -143,7 +143,7 @@ class Chef # Serialize this object as a hash def to_json(*a) - to_hash.to_json(*a) + ::Chef::JSONCompat.to_json(to_hash, *a) end def update_from!(o) diff --git a/lib/chef/run_list.rb b/lib/chef/run_list.rb index 684c5e19fc..fbdbaaa1cc 100644 --- a/lib/chef/run_list.rb +++ b/lib/chef/run_list.rb @@ -85,8 +85,12 @@ class Chef @run_list_items.join(", ") end - def to_json(*args) - to_a.map { |item| item.to_s}.to_json(*args) + def for_json + to_a.map { |item| item.to_s } + end + + def to_json(*a) + ::Chef::JSONCompat.to_json(for_json, *a) end def empty? @@ -158,6 +162,5 @@ class Chef end end - end end diff --git a/lib/chef/user.rb b/lib/chef/user.rb index e2ef45dc5c..4f939556b4 100644 --- a/lib/chef/user.rb +++ b/lib/chef/user.rb @@ -73,7 +73,7 @@ class Chef end def to_json(*a) - to_hash.to_json(*a) + ::Chef::JSONCompat.to_json(to_hash, *a) end def destroy diff --git a/lib/chef/util/diff.rb b/lib/chef/util/diff.rb index 7bce52d874..3117484a71 100644 --- a/lib/chef/util/diff.rb +++ b/lib/chef/util/diff.rb @@ -82,7 +82,7 @@ class Chef end end end - + # produces a unified-output-format diff with 3 lines of context # ChefFS uses udiff() directly def udiff(old_file, new_file) @@ -185,4 +185,3 @@ class Chef end end end - -- cgit v1.2.1