From 9f44841df63fac60e773917bb3240d8d64e6ed2e Mon Sep 17 00:00:00 2001 From: tyler-ball Date: Wed, 8 Oct 2014 10:48:18 -0700 Subject: Adding back `require 'json'` to prevent breaking consumers who currently use Hash.to_json in their cookbooks --- lib/chef/json_compat.rb | 2 ++ spec/spec_helper.rb | 7 ------- spec/unit/json_compat_spec.rb | 7 ++++--- 3 files changed, 6 insertions(+), 10 deletions(-) diff --git a/lib/chef/json_compat.rb b/lib/chef/json_compat.rb index 843b87b25c..3350da0c13 100644 --- a/lib/chef/json_compat.rb +++ b/lib/chef/json_compat.rb @@ -19,6 +19,8 @@ require 'ffi_yajl' require 'chef/exceptions' +# We're requiring this to prevent breaking consumers using Hash.to_json +require 'json' class Chef class JSONCompat diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 9d629da226..8abd5f59af 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -186,12 +186,5 @@ module WEBrick end end -# We are no longer using the 'json' gem - deny all access to it! -orig_require = Kernel.send(:instance_method, :require) -Kernel.send(:remove_method, :require) -Kernel.send(:define_method, :require) { |path| - raise LoadError, 'JSON gem is no longer allowed - use Chef::JSONCompat.to_json' if path == 'json' - orig_require.bind(Kernel).call(path) -} # Enough stuff needs json serialization that I'm just adding it here for equality asserts require 'chef/json_compat' diff --git a/spec/unit/json_compat_spec.rb b/spec/unit/json_compat_spec.rb index 17beba5492..65d931df70 100644 --- a/spec/unit/json_compat_spec.rb +++ b/spec/unit/json_compat_spec.rb @@ -102,8 +102,9 @@ describe Chef::JSONCompat do end end - it "should not allow the json gem to be required because of the spec_helper" do - # We want to prevent ourselves from writing code that requires 'json' - expect { require 'json' }.to raise_error(LoadError) + it "should define .to_json on all classes" do + class SomeClass; end + + expect(SomeClass.new.respond_to?(:to_json)).to eq(true) end end -- cgit v1.2.1