diff options
| author | William Woodruff | 2016-11-20 12:43:13 -0500 |
|---|---|---|
| committer | William Woodruff | 2016-11-20 15:02:47 -0500 |
| commit | 54d18cee17a7af49b5858dd752bf2eda59014472 (patch) | |
| tree | 2284b6927c1bbeda86df183e34ab188f8fdbc716 /Library/Homebrew/compat | |
| parent | 7fbab8be9c1aa7b3a5a57ab1362fc128d12135ed (diff) | |
| download | brew-54d18cee17a7af49b5858dd752bf2eda59014472.tar.bz2 | |
compat: deprecate Utils::JSON in favor of corelib JSON.
Diffstat (limited to 'Library/Homebrew/compat')
| -rw-r--r-- | Library/Homebrew/compat/json.rb | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/Library/Homebrew/compat/json.rb b/Library/Homebrew/compat/json.rb new file mode 100644 index 000000000..c8bf1c292 --- /dev/null +++ b/Library/Homebrew/compat/json.rb @@ -0,0 +1,37 @@ +require "json" + +module Utils + module JSON + module_function + + Error = Class.new(StandardError) + + def load(str) + odeprecated "Utils::JSON.load", "JSON.parse" + ::JSON.parse(str) + rescue ::JSON::ParserError => e + raise Error, e.message + end + + def dump(obj) + odeprecated "Utils::JSON.dump", "JSON.generate" + ::JSON.generate(obj) + end + + def stringify_keys(obj) + odeprecated "Utils::JSON.stringify_keys" + case obj + when Array + obj.map { |val| stringify_keys(val) } + when Hash + obj.inject({}) do |result, (key, val)| + key = key.respond_to?(:to_s) ? key.to_s : key + val = stringify_keys(val) + result.merge!(key => val) + end + else + obj + end + end + end +end |
