Skip to content

Commit

Permalink
Merge pull request #6691 from Magisus/multijson
Browse files Browse the repository at this point in the history
(PUP-8501) Replace direct references to JSON gem with MultiJson
  • Loading branch information
lindboe committed Mar 14, 2018
2 parents 7611f1b + c55bd06 commit d14c15d
Show file tree
Hide file tree
Showing 36 changed files with 157 additions and 100 deletions.
1 change: 1 addition & 0 deletions .gemspec
Expand Up @@ -38,6 +38,7 @@ Gem::Specification.new do |s|
# i18n support (gettext-setup and dependencies)
s.add_runtime_dependency(%q<fast_gettext>, "~> 1.1.2")
s.add_runtime_dependency(%q<locale>, "~> 2.1")
s.add_runtime_dependency(%q<multi_json>, "~> 1.13")
# hocon is an optional hiera backend shipped in puppet-agent packages
s.add_runtime_dependency(%q<hocon>, "~> 1.0")
# net-ssh is a runtime dependency of Puppet::Util::NetworkDevice::Transport::Ssh
Expand Down
4 changes: 1 addition & 3 deletions Gemfile
Expand Up @@ -49,9 +49,7 @@ group(:development, :test) do
gem "yarjuf", "~> 2.0"

# json-schema does not support windows, so omit it from the platforms list
# json-schema uses multi_json, but chokes with multi_json 1.7.9, so prefer 1.7.7
gem "multi_json", "1.7.7", :require => false, :platforms => [:ruby, :jruby]
gem "json-schema", "2.1.1", :require => false, :platforms => [:ruby, :jruby]
gem "json-schema", "~> 2.0", :require => false, :platforms => [:ruby, :jruby]

if RUBY_VERSION >= '2.0'
# pin rubocop as 0.50 requires a higher version of the rainbow gem (see below)
Expand Down
1 change: 1 addition & 0 deletions ext/project_data.yaml
Expand Up @@ -23,6 +23,7 @@ gem_runtime_dependencies:
# semantic_puppet: ['>= 0.1.3', '< 2']
fast_gettext: '~> 1.1.2'
locale: '~> 2.1'
multi_json: '~> 1.10'
gem_rdoc_options:
- --title
- "Puppet - Configuration Management"
Expand Down
2 changes: 1 addition & 1 deletion lib/puppet/application/lookup.rb
Expand Up @@ -351,7 +351,7 @@ def generate_scope

if fact_file
if fact_file.end_with?("json")
given_facts = JSON.parse(Puppet::FileSystem.read(fact_file, :encoding => 'utf-8'))
given_facts = Puppet::Util::Json.load(Puppet::FileSystem.read(fact_file, :encoding => 'utf-8'))
else
given_facts = YAML.load(Puppet::FileSystem.read(fact_file, :encoding => 'utf-8'))
end
Expand Down
2 changes: 1 addition & 1 deletion lib/puppet/face/epp.rb
Expand Up @@ -509,7 +509,7 @@ def create_compiler(options)
if fact_file.is_a?(Hash) # when used via the Face API
given_facts = fact_file
elsif fact_file.end_with?("json")
given_facts = JSON.parse(Puppet::FileSystem.read(fact_file, :encoding => 'utf-8'))
given_facts = Puppet::Util::Json.load(Puppet::FileSystem.read(fact_file, :encoding => 'utf-8'))
else
given_facts = YAML.load(Puppet::FileSystem.read(fact_file, :encoding => 'utf-8'))
end
Expand Down
6 changes: 3 additions & 3 deletions lib/puppet/forge.rb
Expand Up @@ -5,7 +5,7 @@
require 'tempfile'
require 'uri'
require 'pathname'
require 'json'
require 'puppet/util/json'
require 'semantic_puppet'

class Puppet::Forge < SemanticPuppet::Dependency::Source
Expand Down Expand Up @@ -64,7 +64,7 @@ def search(term)
response = make_http_request(uri)

if response.code == '200'
result = JSON.parse(response.body)
result = Puppet::Util::Json.load(response.body)
uri = decode_uri(result['pagination']['next'])
matches.concat result['results']
else
Expand Down Expand Up @@ -101,7 +101,7 @@ def fetch(input)
response = make_http_request(uri)

if response.code == '200'
response = JSON.parse(response.body)
response = Puppet::Util::Json.load(response.body)
else
raise ResponseError.new(:uri => URI.parse(@host).merge(uri), :response => response)
end
Expand Down
6 changes: 3 additions & 3 deletions lib/puppet/forge/errors.rb
@@ -1,4 +1,4 @@
require 'json'
require 'puppet/util/json'
require 'puppet/error'
require 'puppet/forge'

Expand Down Expand Up @@ -82,11 +82,11 @@ def initialize(options)
@response = "#{response.code} #{response.message.strip}"

begin
body = JSON.parse(response.body)
body = Puppet::Util::Json.load(response.body)
if body['message']
@message ||= body['message'].strip
end
rescue JSON::ParserError
rescue Puppet::Util::Json::ParseError
end

message = if @message
Expand Down
4 changes: 2 additions & 2 deletions lib/puppet/functions/json_data.rb
Expand Up @@ -19,8 +19,8 @@ def json_data(options, context)
path = options['path']
context.cached_file_data(path) do |content|
begin
JSON.parse(content)
rescue JSON::ParserError => ex
Puppet::Util::Json.load(content)
rescue Puppet::Util::Json::ParseError => ex
# Filename not included in message, so we add it here.
raise Puppet::DataBinding::LookupError, "Unable to parse (%{path}): %{message}" % { path: path, message: ex.message }
end
Expand Down
4 changes: 2 additions & 2 deletions lib/puppet/indirector/rest.rb
@@ -1,6 +1,6 @@
require 'net/http'
require 'uri'
require 'json'
require 'puppet/util/json'
require 'semantic_puppet'

require 'puppet/network/http'
Expand Down Expand Up @@ -301,7 +301,7 @@ def convert_to_http_error(response)
elsif response['content-type'].is_a?(String)
content_type, body = parse_response(response)
if content_type =~ /[pj]son/
returned_message = JSON.parse(body)["message"]
returned_message = Puppet::Util::Json.load(body)["message"]
else
returned_message = uncompress_body(response)
end
Expand Down
6 changes: 3 additions & 3 deletions lib/puppet/module.rb
@@ -1,6 +1,6 @@
require 'puppet/util/logging'
require 'puppet/module/task'
require 'json'
require 'puppet/util/json'
require 'semantic_puppet/gem_version'

# Support for modules
Expand Down Expand Up @@ -202,10 +202,10 @@ def license_file

def read_metadata
md_file = metadata_file
md_file.nil? ? {} : JSON.parse(File.read(md_file, :encoding => 'utf-8'))
md_file.nil? ? {} : Puppet::Util::Json.load(File.read(md_file, :encoding => 'utf-8'))
rescue Errno::ENOENT
{}
rescue JSON::JSONError => e
rescue Puppet::Util::Json::ParseError => e
#TRANSLATORS 'metadata.json' is a specific file name and should not be translated.
msg = _("%{name} has an invalid and unparsable metadata.json file. The parse error: %{error}") % { name: name, error: e.message }
case Puppet[:strict]
Expand Down
1 change: 0 additions & 1 deletion lib/puppet/module/task.rb
@@ -1,4 +1,3 @@
require 'json'
require 'puppet/util/logging'

class Puppet::Module
Expand Down
8 changes: 4 additions & 4 deletions lib/puppet/module_tool/applications/application.rb
@@ -1,5 +1,5 @@
require 'net/http'
require 'json'
require 'puppet/util/json'
require 'puppet/util/colors'

module Puppet::ModuleTool
Expand All @@ -26,7 +26,7 @@ def discuss(response, success, failure)
when Net::HTTPOK, Net::HTTPCreated
Puppet.notice success
else
errors = JSON.parse(response.body)['error'] rescue "HTTP #{response.code}, #{response.body}"
errors = Puppet::Util::Json.load(response.body)['error'] rescue "HTTP #{response.code}, #{response.body}"
Puppet.warning "#{failure} (#{errors})"
end
end
Expand All @@ -48,8 +48,8 @@ def metadata(require_metadata = false)
if File.file?(metadata_path)
File.open(metadata_path) do |f|
begin
@metadata.update(JSON.load(f))
rescue JSON::ParserError => ex
@metadata.update(Puppet::Util::Json.load(f))
rescue Puppet::Util::Json::ParserError => ex
raise ArgumentError, _("Could not parse JSON %{metadata_path}") % { metadata_path: metadata_path }, ex.backtrace
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/puppet/module_tool/applications/builder.rb
@@ -1,5 +1,5 @@
require 'fileutils'
require 'json'
require 'puppet/util/json'
require 'puppet/file_system'
require 'pathspec'
require 'facter'
Expand Down Expand Up @@ -140,7 +140,7 @@ def write_json
end

Puppet::FileSystem.open(File.join(build_path, 'checksums.json'), nil, 'wb') do |f|
f.write(JSON.pretty_generate(Checksums.new(build_path)))
f.write(Puppet::Util::Json.dump(Checksums.new(build_path), :pretty => true))
end
end

Expand Down
6 changes: 3 additions & 3 deletions lib/puppet/module_tool/applications/checksummer.rb
@@ -1,4 +1,4 @@
require 'json'
require 'puppet/util/json'
require 'puppet/module_tool/checksums'

module Puppet::ModuleTool
Expand Down Expand Up @@ -40,10 +40,10 @@ def run

def checksums
if checksums_file.exist?
JSON.parse(checksums_file.read)
Puppet::Util::Json.load(checksums_file.read)
elsif metadata_file.exist?
# Check metadata.json too; legacy modules store their checksums there.
JSON.parse(metadata_file.read)['checksums'] or
Puppet::Util::Json.load(metadata_file.read)['checksums'] or
raise ArgumentError, _("No file containing checksums found.")
else
raise ArgumentError, _("No file containing checksums found.")
Expand Down
4 changes: 2 additions & 2 deletions lib/puppet/module_tool/applications/unpacker.rb
@@ -1,6 +1,6 @@
require 'pathname'
require 'tmpdir'
require 'json'
require 'puppet/util/json'
require 'puppet/file_system'

module Puppet::ModuleTool
Expand Down Expand Up @@ -75,7 +75,7 @@ def root_dir

# @api private
def module_name
metadata = JSON.parse((root_dir + 'metadata.json').read)
metadata = Puppet::Util::Json.load((root_dir + 'metadata.json').read)
metadata['name'][/-(.*)/, 1]
end

Expand Down
6 changes: 3 additions & 3 deletions lib/puppet/module_tool/metadata.rb
Expand Up @@ -2,7 +2,7 @@
require 'puppet/module_tool'
require 'puppet/network/format_support'
require 'uri'
require 'json'
require 'puppet/util/json'
require 'set'

module Puppet::ModuleTool
Expand Down Expand Up @@ -102,8 +102,8 @@ def to_json
data = @data.dup.merge('dependencies' => dependencies)

contents = data.keys.map do |k|
value = (JSON.pretty_generate(data[k]) rescue data[k].to_json)
"#{k.to_json}: #{value}"
value = (Puppet::Util::Json.dump(data[k], :pretty => true) rescue data[k].to_json)
%Q("#{k.to_s}": #{value})
end

"{\n" + contents.join(",\n").gsub(/^/, ' ') + "\n}\n"
Expand Down
2 changes: 1 addition & 1 deletion lib/puppet/network/format_support.rb
Expand Up @@ -97,7 +97,7 @@ def to_pson(*args)
end

def to_json(*args)
to_data_hash.to_json(*args)
Puppet::Util::Json.dump(to_data_hash, *args)
end

def render(format = nil)
Expand Down
12 changes: 5 additions & 7 deletions lib/puppet/network/formats.rb
@@ -1,5 +1,5 @@
require 'puppet/network/format_handler'
require 'json'
require 'puppet/util/json'

Puppet::Network::FormatHandler.create_serialized_formats(:msgpack, :weight => 20, :mime => "application/x-msgpack", :required_methods => [:render_method, :intern_method], :intern_method => :from_data_hash) do

Expand Down Expand Up @@ -101,19 +101,17 @@ def data_to_instance(klass, data)

Puppet::Network::FormatHandler.create_serialized_formats(:json, :mime => 'application/json', :charset => Encoding::UTF_8, :weight => 15, :required_methods => [:render_method, :intern_method], :intern_method => :from_data_hash) do
def intern(klass, text)
data_to_instance(klass, JSON.parse(text))
data_to_instance(klass, Puppet::Util::Json.load(text))
end

def intern_multiple(klass, text)
JSON.parse(text).collect do |data|
Puppet::Util::Json.load(text).collect do |data|
data_to_instance(klass, data)
end
end

# JSON monkey-patches Array, so this works.
# https://github.com/ruby/ruby/blob/ruby_1_9_3/ext/json/generator/generator.c#L1416
def render_multiple(instances)
instances.to_json
Puppet::Util::Json.dump(instances)
end

# Unlike PSON, we do not need to unwrap the data envelope, because legacy 3.x agents
Expand Down Expand Up @@ -162,7 +160,7 @@ def render(datum)
end

# ...or pretty-print the inspect outcome.
JSON.pretty_generate(datum, :quirks_mode => true)
Puppet::Util::Json.dump(datum, :pretty => true, :quirks_mode => true)
end

def render_multiple(data)
Expand Down
4 changes: 2 additions & 2 deletions lib/puppet/network/http/api/master/v3/environment.rb
@@ -1,4 +1,4 @@
require 'json'
require 'puppet/util/json'
require 'puppet/parser/environment_compiler'

class Puppet::Network::HTTP::API::Master::V3::Environment
Expand All @@ -15,7 +15,7 @@ def call(request, response)

env_graph = build_environment_graph(catalog)

response.respond_with(200, "application/json", JSON.dump(env_graph))
response.respond_with(200, "application/json", Puppet::Util::Json.dump(env_graph))
end

def build_environment_graph(catalog)
Expand Down
4 changes: 2 additions & 2 deletions lib/puppet/network/http/api/master/v3/environments.rb
@@ -1,12 +1,12 @@
require 'json'
require 'puppet/util/json'

class Puppet::Network::HTTP::API::Master::V3::Environments
def initialize(env_loader)
@env_loader = env_loader
end

def call(request, response)
response.respond_with(200, "application/json", JSON.dump({
response.respond_with(200, "application/json", Puppet::Util::Json.dump({
"search_paths" => @env_loader.search_paths,
"environments" => Hash[@env_loader.list.collect do |env|
[env.name, {
Expand Down
6 changes: 3 additions & 3 deletions lib/puppet/network/http/error.rb
@@ -1,4 +1,4 @@
require 'json'
require 'puppet/util/json'

module Puppet::Network::HTTP::Error
Issues = Puppet::Network::HTTP::Issues
Expand All @@ -13,7 +13,7 @@ def initialize(message, status, issue_kind)
end

def to_json
JSON({:message => message, :issue_kind => @issue_kind})
Puppet::Util::Json.dump({:message => message, :issue_kind => @issue_kind})
end
end

Expand Down Expand Up @@ -67,7 +67,7 @@ def initialize(original_error, issue_kind = Issues::RUNTIME_ERROR)
end

def to_json
JSON({:message => message, :issue_kind => @issue_kind})
Puppet::Util::Json.dump({:message => message, :issue_kind => @issue_kind})
end
end
end
4 changes: 2 additions & 2 deletions lib/puppet/pops/loader/task_instantiator.rb
Expand Up @@ -31,8 +31,8 @@ def self.create_task(loader, name, task_source, metadata)
else
json_text = loader.get_contents(metadata)
begin
create_task_from_hash(name, task_source, JSON.parse(json_text) || EMPTY_HASH)
rescue JSON::ParserError => ex
create_task_from_hash(name, task_source, Puppet::Util::Json.load(json_text) || EMPTY_HASH)
rescue Puppet::Util::Json::ParseError => ex
raise Puppet::ParseError.new(ex.message, metadata)
rescue Types::TypeAssertionError => ex
# Not strictly a parser error but from the users perspective, the file content didn't parse properly. The
Expand Down

0 comments on commit d14c15d

Please sign in to comment.