From d07b9ed7f2e8806b1840b4f60605ef45487655e1 Mon Sep 17 00:00:00 2001 From: William Woodruff Date: Sun, 20 Nov 2016 13:00:01 -0500 Subject: [PATCH] Replace Utils::JSON with corelib JSON calls. --- Library/Homebrew/cmd/info.rb | 4 ++-- Library/Homebrew/cmd/outdated.rb | 2 +- Library/Homebrew/cmd/style.rb | 4 ++-- Library/Homebrew/cmd/tap-info.rb | 2 +- Library/Homebrew/descriptions.rb | 4 ++-- Library/Homebrew/dev-cmd/boneyard-formula-pr.rb | 4 ++-- Library/Homebrew/dev-cmd/bottle.rb | 4 ++-- Library/Homebrew/dev-cmd/pull.rb | 4 ++-- Library/Homebrew/download_strategy.rb | 6 +++--- Library/Homebrew/tab.rb | 6 +++--- Library/Homebrew/tap.rb | 8 ++++---- Library/Homebrew/test/json_test.rb | 8 ++++---- .../support/helper/integration_command_test_case.rb | 2 +- Library/Homebrew/test/tab_test.rb | 2 +- Library/Homebrew/utils/github.rb | 12 ++++++------ 15 files changed, 36 insertions(+), 36 deletions(-) diff --git a/Library/Homebrew/cmd/info.rb b/Library/Homebrew/cmd/info.rb index a3062ec99a..fc47e1731c 100644 --- a/Library/Homebrew/cmd/info.rb +++ b/Library/Homebrew/cmd/info.rb @@ -22,7 +22,7 @@ require "options" require "formula" require "keg" require "tab" -require "utils/json" +require "json" module Homebrew module_function @@ -72,7 +72,7 @@ module Homebrew ARGV.formulae end json = ff.map(&:to_hash) - puts Utils::JSON.dump(json) + puts JSON.generate(json) end def github_remote_path(remote, path) diff --git a/Library/Homebrew/cmd/outdated.rb b/Library/Homebrew/cmd/outdated.rb index 3ff3ef1070..9ed7a0f79a 100644 --- a/Library/Homebrew/cmd/outdated.rb +++ b/Library/Homebrew/cmd/outdated.rb @@ -88,7 +88,7 @@ module Homebrew installed_versions: outdated_versions.collect(&:to_s), current_version: current_version } end - puts Utils::JSON.dump(json) + puts JSON.generate(json) outdated end diff --git a/Library/Homebrew/cmd/style.rb b/Library/Homebrew/cmd/style.rb index 8b6793e778..9befcf9ac1 100644 --- a/Library/Homebrew/cmd/style.rb +++ b/Library/Homebrew/cmd/style.rb @@ -14,7 +14,7 @@ #: Exits with a non-zero status if any style violations are found. require "utils" -require "utils/json" +require "json" module Homebrew module_function @@ -74,7 +74,7 @@ module Homebrew # exitstatus can also be nil if RuboCop process crashes, e.g. due to # native extension problems raise "Error while running RuboCop" if $?.exitstatus.nil? || $?.exitstatus > 1 - RubocopResults.new(Utils::JSON.load(json)) + RubocopResults.new(JSON.parse(json)) else raise "Invalid output_type for check_style_impl: #{output_type}" end diff --git a/Library/Homebrew/cmd/tap-info.rb b/Library/Homebrew/cmd/tap-info.rb index f31bf480c8..0d9f8db3d9 100644 --- a/Library/Homebrew/cmd/tap-info.rb +++ b/Library/Homebrew/cmd/tap-info.rb @@ -82,6 +82,6 @@ module Homebrew end def print_tap_json(taps) - puts Utils::JSON.dump(taps.map(&:to_hash)) + puts JSON.generate(taps.map(&:to_hash)) end end diff --git a/Library/Homebrew/descriptions.rb b/Library/Homebrew/descriptions.rb index 08860f7cf5..a338bb73ad 100644 --- a/Library/Homebrew/descriptions.rb +++ b/Library/Homebrew/descriptions.rb @@ -12,14 +12,14 @@ class Descriptions # If the cache file exists, load it into, and return, a hash; otherwise, # return nil. def self.load_cache - @cache = Utils::JSON.load(CACHE_FILE.read) if CACHE_FILE.exist? + @cache = JSON.parse(CACHE_FILE.read) if CACHE_FILE.exist? end # Write the cache to disk after ensuring the existence of the containing # directory. def self.save_cache HOMEBREW_CACHE.mkpath - CACHE_FILE.atomic_write Utils::JSON.dump(@cache) + CACHE_FILE.atomic_write JSON.dump(@cache) end # Create a hash mapping all formulae to their descriptions; diff --git a/Library/Homebrew/dev-cmd/boneyard-formula-pr.rb b/Library/Homebrew/dev-cmd/boneyard-formula-pr.rb index 3d95f14b9f..4c2fbb6f3d 100644 --- a/Library/Homebrew/dev-cmd/boneyard-formula-pr.rb +++ b/Library/Homebrew/dev-cmd/boneyard-formula-pr.rb @@ -9,7 +9,7 @@ #: If `--reason=` is passed, append this to the commit/PR message. require "formula" -require "utils/json" +require "json" require "fileutils" begin @@ -60,7 +60,7 @@ module Homebrew EOS safe_system "git", "add", tap_migrations_path end - tap_migrations = Utils::JSON.load(File.read(tap_migrations_path)) + tap_migrations = JSON.parse(File.read(tap_migrations_path)) tap_migrations[formula.name] = boneyard_tap.name tap_migrations = tap_migrations.sort.inject({}) { |acc, elem| acc.merge!(elem[0] => elem[1]) } tap_migrations_path.atomic_write(JSON.pretty_generate(tap_migrations) + "\n") diff --git a/Library/Homebrew/dev-cmd/bottle.rb b/Library/Homebrew/dev-cmd/bottle.rb index 7e98f2ebb1..9618cf4121 100644 --- a/Library/Homebrew/dev-cmd/bottle.rb +++ b/Library/Homebrew/dev-cmd/bottle.rb @@ -344,7 +344,7 @@ module Homebrew }, } File.open("#{filename.prefix}.bottle.json", "w") do |file| - file.write Utils::JSON.dump json + file.write JSON.generate json end end @@ -352,7 +352,7 @@ module Homebrew write = ARGV.include? "--write" bottles_hash = ARGV.named.reduce({}) do |hash, json_file| - deep_merge_hashes hash, Utils::JSON.load(IO.read(json_file)) + deep_merge_hashes hash, JSON.parse(IO.read(json_file)) end bottles_hash.each do |formula_name, bottle_hash| diff --git a/Library/Homebrew/dev-cmd/pull.rb b/Library/Homebrew/dev-cmd/pull.rb index 7e6e86a850..f7006baaa9 100644 --- a/Library/Homebrew/dev-cmd/pull.rb +++ b/Library/Homebrew/dev-cmd/pull.rb @@ -32,7 +32,7 @@ require "net/http" require "net/https" require "utils" -require "utils/json" +require "json" require "formula" require "formulary" require "tap" @@ -433,7 +433,7 @@ module Homebrew return nil unless $?.success? Homebrew.force_utf8!(json) - FormulaInfoFromJson.new(Utils::JSON.load(json)[0]) + FormulaInfoFromJson.new(JSON.parse(json)[0]) end def bottle_tags diff --git a/Library/Homebrew/download_strategy.rb b/Library/Homebrew/download_strategy.rb index 63b821cebf..1f77fa20c9 100644 --- a/Library/Homebrew/download_strategy.rb +++ b/Library/Homebrew/download_strategy.rb @@ -1,4 +1,4 @@ -require "utils/json" +require "json" require "rexml/document" require "time" @@ -444,14 +444,14 @@ class CurlApacheMirrorDownloadStrategy < CurlDownloadStrategy return super if @tried_apache_mirror @tried_apache_mirror = true - mirrors = Utils::JSON.load(apache_mirrors) + mirrors = JSON.parse(apache_mirrors) path_info = mirrors.fetch("path_info") @url = mirrors.fetch("preferred") + path_info @mirrors |= %W[https://archive.apache.org/dist/#{path_info}] ohai "Best Mirror #{@url}" super - rescue IndexError, Utils::JSON::Error + rescue IndexError, JSON::ParserError raise CurlDownloadStrategyError, "Couldn't determine mirror, try again later." end end diff --git a/Library/Homebrew/tab.rb b/Library/Homebrew/tab.rb index 3bb7d8b624..28f29aaad1 100644 --- a/Library/Homebrew/tab.rb +++ b/Library/Homebrew/tab.rb @@ -1,7 +1,7 @@ require "cxxstdlib" require "ostruct" require "options" -require "utils/json" +require "json" require "development_tools" # Inherit from OpenStruct to gain a generic initialization method that takes a @@ -58,7 +58,7 @@ class Tab < OpenStruct # Like Tab.from_file, but bypass the cache. def self.from_file_content(content, path) - attributes = Utils::JSON.load(content) + attributes = JSON.parse(content) attributes["tabfile"] = path attributes["source_modified_time"] ||= 0 attributes["source"] ||= {} @@ -313,7 +313,7 @@ class Tab < OpenStruct "source" => source, } - Utils::JSON.dump(attributes) + JSON.generate(attributes) end def write diff --git a/Library/Homebrew/tap.rb b/Library/Homebrew/tap.rb index 3659abe4ff..68b21ac604 100644 --- a/Library/Homebrew/tap.rb +++ b/Library/Homebrew/tap.rb @@ -442,10 +442,10 @@ class Tap # Hash with tap formula renames def formula_renames - require "utils/json" + require "json" @formula_renames ||= if (rename_file = path/"formula_renames.json").file? - Utils::JSON.load(rename_file.read) + JSON.parse(rename_file.read) else {} end @@ -453,10 +453,10 @@ class Tap # Hash with tap migrations def tap_migrations - require "utils/json" + require "json" @tap_migrations ||= if (migration_file = path/"tap_migrations.json").file? - Utils::JSON.load(migration_file.read) + JSON.parse(migration_file.read) else {} end diff --git a/Library/Homebrew/test/json_test.rb b/Library/Homebrew/test/json_test.rb index 14d2f2b4c9..7c4c3671d6 100644 --- a/Library/Homebrew/test/json_test.rb +++ b/Library/Homebrew/test/json_test.rb @@ -1,20 +1,20 @@ require "testing_env" -require "utils/json" +require "json" class JsonSmokeTest < Homebrew::TestCase def test_encode hash = { "foo" => ["bar", "baz"] } json = '{"foo":["bar","baz"]}' - assert_equal json, Utils::JSON.dump(hash) + assert_equal json, JSON.generate(hash) end def test_decode hash = { "foo" => ["bar", "baz"], "qux" => 1 } json = '{"foo":["bar","baz"],"qux":1}' - assert_equal hash, Utils::JSON.load(json) + assert_equal hash, JSON.parse(json) end def test_decode_failure - assert_raises(Utils::JSON::Error) { Utils::JSON.load("nope") } + assert_raises(JSON::ParserError) { JSON.parse("nope") } end end diff --git a/Library/Homebrew/test/support/helper/integration_command_test_case.rb b/Library/Homebrew/test/support/helper/integration_command_test_case.rb index 5940fd84b6..b79fdd6e0d 100644 --- a/Library/Homebrew/test/support/helper/integration_command_test_case.rb +++ b/Library/Homebrew/test/support/helper/integration_command_test_case.rb @@ -185,7 +185,7 @@ class IntegrationCommandTestCase < Homebrew::TestCase cmd("install", old_name) (core_tap.path/"Formula/#{old_name}.rb").unlink formula_renames = core_tap.path/"formula_renames.json" - formula_renames.write Utils::JSON.dump(old_name => new_name) + formula_renames.write JSON.generate(old_name => new_name) core_tap.path.cd do shutup do diff --git a/Library/Homebrew/test/tab_test.rb b/Library/Homebrew/test/tab_test.rb index 76c9aacc9f..ef653103c1 100644 --- a/Library/Homebrew/test/tab_test.rb +++ b/Library/Homebrew/test/tab_test.rb @@ -186,7 +186,7 @@ class TabTests < Homebrew::TestCase end def test_to_json - tab = Tab.new(Utils::JSON.load(@tab.to_json)) + tab = Tab.new(JSON.parse(@tab.to_json)) assert_equal @tab.used_options.sort, tab.used_options.sort assert_equal @tab.unused_options.sort, tab.unused_options.sort assert_equal @tab.built_as_bottle, tab.built_as_bottle diff --git a/Library/Homebrew/utils/github.rb b/Library/Homebrew/utils/github.rb index 43a0e88a83..5f961974cb 100644 --- a/Library/Homebrew/utils/github.rb +++ b/Library/Homebrew/utils/github.rb @@ -149,9 +149,9 @@ module GitHub data_tmpfile = nil if data begin - data = Utils::JSON.dump data + data = JSON.generate data data_tmpfile = Tempfile.new("github_api_post", HOMEBREW_TEMP) - rescue Utils::JSON::Error => e + rescue JSON::ParserError => e raise Error, "Failed to parse JSON request:\n#{e.message}\n#{data}", e.backtrace end end @@ -183,13 +183,13 @@ module GitHub if !http_code.start_with?("2") && !status.success? raise_api_error(output, errors, http_code, headers, scopes) end - json = Utils::JSON.load output + json = JSON.parse output if block_given? yield json else json end - rescue Utils::JSON::Error => e + rescue JSON::ParserError => e raise Error, "Failed to parse JSON response\n#{e.message}", e.backtrace end end @@ -205,7 +205,7 @@ module GitHub if meta.fetch("x-ratelimit-remaining", 1).to_i <= 0 reset = meta.fetch("x-ratelimit-reset").to_i - error = Utils::JSON.load(output)["message"] + error = JSON.parse(output)["message"] raise RateLimitExceededError.new(reset, error) end @@ -218,7 +218,7 @@ module GitHub raise HTTPNotFoundError, output else error = begin - Utils::JSON.load(output)["message"] + JSON.parse(output)["message"] rescue nil end