diff --git a/Library/Contributions/cmd/brew-ls-taps.rb b/Library/Contributions/cmd/brew-ls-taps.rb index 98c14dccf2..40cf0cafe3 100755 --- a/Library/Contributions/cmd/brew-ls-taps.rb +++ b/Library/Contributions/cmd/brew-ls-taps.rb @@ -1,7 +1,7 @@ -require 'vendor/multi_json' +require 'utils/json' GitHub.open "https://api.github.com/legacy/repos/search/homebrew" do |f| - MultiJson.decode(f.read)["repositories"].each do |repo| + Utils::JSON.load(f.read)["repositories"].each do |repo| if repo['name'] =~ /^homebrew-(\S+)$/ puts tap = if repo['username'] == "Homebrew" "homebrew/#{$1}" diff --git a/Library/Homebrew/cmd/info.rb b/Library/Homebrew/cmd/info.rb index de98d692c0..b5f133dfd4 100644 --- a/Library/Homebrew/cmd/info.rb +++ b/Library/Homebrew/cmd/info.rb @@ -3,6 +3,7 @@ require 'tab' require 'keg' require 'caveats' require 'blacklist' +require 'utils/json' module Homebrew extend self def info @@ -46,14 +47,12 @@ module Homebrew extend self end def print_json - require 'vendor/multi_json' - formulae = ARGV.include?("--all") ? Formula : ARGV.formulae json = formulae.map {|f| f.to_hash} if json.size == 1 - puts MultiJson.encode json.pop + puts Utils::JSON.dump(json.pop) else - puts MultiJson.encode json + puts Utils::JSON.dump(json) end end diff --git a/Library/Homebrew/cmd/search.rb b/Library/Homebrew/cmd/search.rb index 0ac76e435d..2ec8afd81d 100644 --- a/Library/Homebrew/cmd/search.rb +++ b/Library/Homebrew/cmd/search.rb @@ -1,7 +1,7 @@ require 'formula' require 'blacklist' require 'utils' -require 'vendor/multi_json' +require 'utils/json' module Homebrew extend self def search @@ -73,7 +73,7 @@ module Homebrew extend self results = [] GitHub.open "https://api.github.com/repos/#{user}/homebrew-#{repo}/git/trees/HEAD?recursive=1" do |f| user.downcase! if user == "Homebrew" # special handling for the Homebrew organization - MultiJson.decode(f.read)["tree"].map{ |hash| hash['path'] }.compact.each do |file| + Utils::JSON.load(f.read)["tree"].map{ |hash| hash['path'] }.compact.each do |file| name = File.basename(file, '.rb') if file =~ /\.rb$/ and name =~ rx results << "#{user}/#{repo}/#{name}" diff --git a/Library/Homebrew/download_strategy.rb b/Library/Homebrew/download_strategy.rb index 8e3bd81391..c7d6aeefb6 100644 --- a/Library/Homebrew/download_strategy.rb +++ b/Library/Homebrew/download_strategy.rb @@ -1,5 +1,5 @@ require 'open-uri' -require 'vendor/multi_json' +require 'utils/json' class AbstractDownloadStrategy attr_accessor :local_bottle_path @@ -172,12 +172,12 @@ end # Detect and download from Apache Mirror class CurlApacheMirrorDownloadStrategy < CurlDownloadStrategy def _fetch - mirrors = MultiJson.decode(open("#{@url}&asjson=1").read) + mirrors = Utils::JSON.load(open("#{@url}&asjson=1").read) url = mirrors.fetch('preferred') + mirrors.fetch('path_info') ohai "Best Mirror #{url}" curl url, '-C', downloaded_size, '-o', @temporary_path - rescue IndexError, MultiJson::DecodeError + rescue IndexError, Utils::JSON::Error raise "Couldn't determine mirror. Try again later." end end diff --git a/Library/Homebrew/tab.rb b/Library/Homebrew/tab.rb index d35f69f8ad..48f7091133 100644 --- a/Library/Homebrew/tab.rb +++ b/Library/Homebrew/tab.rb @@ -1,6 +1,6 @@ require 'ostruct' require 'options' -require 'vendor/multi_json' +require 'utils/json' # Inherit from OpenStruct to gain a generic initialization method that takes a # hash and creates an attribute for each key and value. `Tab.new` probably @@ -27,7 +27,7 @@ class Tab < OpenStruct end def self.from_file path - tab = Tab.new MultiJson.decode(open(path).read) + tab = Tab.new Utils::JSON.load(open(path).read) tab.tabfile = path tab end @@ -90,7 +90,7 @@ class Tab < OpenStruct end def to_json - MultiJson.encode({ + Utils::JSON.dump({ :used_options => used_options.to_a, :unused_options => unused_options.to_a, :built_as_bottle => built_as_bottle, diff --git a/Library/Homebrew/test/test_json.rb b/Library/Homebrew/test/test_json.rb index 6bece67c00..f77b7852d9 100644 --- a/Library/Homebrew/test/test_json.rb +++ b/Library/Homebrew/test/test_json.rb @@ -1,16 +1,16 @@ require 'testing_env' -require 'vendor/multi_json' +require 'utils/json' class JsonSmokeTest < Test::Unit::TestCase def test_encode hash = { "foo" => ["bar", "baz"] } json = %q|{"foo":["bar","baz"]}| - assert_equal json, MultiJson.encode(hash) + assert_equal json, Utils::JSON.dump(hash) end def test_decode hash = { "foo" => ["bar", "baz"], "qux" => 1 } json = %q|{"foo":["bar","baz"],"qux":1}| - assert_equal hash, MultiJson.decode(json) + assert_equal hash, Utils::JSON.load(json) end end diff --git a/Library/Homebrew/utils.rb b/Library/Homebrew/utils.rb index 60eead67c8..f8b5466f26 100644 --- a/Library/Homebrew/utils.rb +++ b/Library/Homebrew/utils.rb @@ -1,7 +1,7 @@ require 'pathname' require 'exceptions' require 'macos' -require 'vendor/multi_json' +require 'utils/json' require 'open-uri' class Tty @@ -265,7 +265,7 @@ module GitHub extend self Kernel.open(url, default_headers.merge(headers), &block) rescue OpenURI::HTTPError => e if e.io.meta['x-ratelimit-remaining'].to_i <= 0 - raise "GitHub #{MultiJson.decode(e.io.read)['message']}" + raise "GitHub #{Utils::JSON.load(e.io.read)['message']}" else raise e end @@ -283,7 +283,7 @@ module GitHub extend self uri = URI.parse("https://api.github.com/legacy/issues/search/mxcl/homebrew/open/#{name}") GitHub.open uri do |f| - MultiJson.decode(f.read)['issues'].each do |issue| + Utils::JSON.load(f.read)['issues'].each do |issue| # don't include issues that just refer to the tool in their body issues << issue['html_url'] if issue['title'].include? name end @@ -297,7 +297,7 @@ module GitHub extend self uri = URI.parse("https://api.github.com/legacy/issues/search/mxcl/homebrew/open/#{query}") GitHub.open uri do |f| - MultiJson.decode(f.read)['issues'].each do |pull| + Utils::JSON.load(f.read)['issues'].each do |pull| yield pull['pull_request_url'] if rx.match pull['title'] and pull['pull_request_url'] end end diff --git a/Library/Homebrew/utils/json.rb b/Library/Homebrew/utils/json.rb new file mode 100644 index 0000000000..f52881c84a --- /dev/null +++ b/Library/Homebrew/utils/json.rb @@ -0,0 +1,21 @@ +require 'vendor/multi_json' + +module Utils + module JSON + extend self + + Error = Class.new(StandardError) + + def load(str) + MultiJson.load(str) + rescue MultiJson::DecodeError => e + raise Error, e.message + end + + def dump(obj) + MultiJson.dump(obj) + rescue MultiJson::EncodeError => e + raise Error, e.message + end + end +end