From e55a90f730456053118f578a52c91af6cda8e722 Mon Sep 17 00:00:00 2001 From: Weston Ganger Date: Thu, 27 Jun 2019 22:22:09 -0700 Subject: [PATCH] use hjson only if included in Gemfile --- Gemfile.lock | 8 +++----- lib/locomotive/steam.rb | 6 +++++- .../adapters/filesystem/sanitizers/page.rb | 17 +++++++++++++---- .../adapters/filesystem/sanitizers/section.rb | 16 ++++++++++++---- .../steam/adapters/filesystem/yaml_loader.rb | 16 ++++++++++++---- locomotivecms_steam.gemspec | 2 +- 6 files changed, 46 insertions(+), 19 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 924ffee2..c7191121 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -21,7 +21,6 @@ PATH compass (~> 1.0.3) dragonfly (~> 1.2.0) duktape (~> 2.0.1.1) - hjson (~> 0.1.2) httparty (~> 0.16.0) kramdown (~> 1.16.2) locomotivecms-solid (~> 4.0.1) @@ -30,6 +29,7 @@ PATH mimetype-fu (~> 0.1.2) moneta (~> 1.0.0) morphine (~> 0.1.1) + multi_json (~> 1.13.1) nokogiri (~> 1.8.2) pony (~> 1.12) rack-cache (~> 1.7.0) @@ -93,13 +93,11 @@ GEM rack (>= 1.3) duktape (2.0.1.1) execjs (2.7.0) - ffi (1.11.1) + ffi (1.10.0) flamegraph (0.9.5) haml (5.0.4) temple (>= 0.8.0) tilt - hjson (0.1.2) - json (>= 1.7.5) httparty (0.16.4) mime-types (~> 3.0) multi_xml (>= 0.5.2) @@ -140,7 +138,7 @@ GEM origin (2.3.1) pony (1.13.1) mail (>= 2.0) - public_suffix (3.1.0) + public_suffix (3.0.3) puma (3.12.0) rack (2.0.6) rack-cache (1.7.2) diff --git a/lib/locomotive/steam.rb b/lib/locomotive/steam.rb index 4183cf3f..62b6ec7a 100644 --- a/lib/locomotive/steam.rb +++ b/lib/locomotive/steam.rb @@ -1,4 +1,8 @@ -require 'hjson' +begin + require 'hjson' +rescue LoadError + +end require 'locomotive/common' diff --git a/lib/locomotive/steam/adapters/filesystem/sanitizers/page.rb b/lib/locomotive/steam/adapters/filesystem/sanitizers/page.rb index 229c552f..5c6200de 100644 --- a/lib/locomotive/steam/adapters/filesystem/sanitizers/page.rb +++ b/lib/locomotive/steam/adapters/filesystem/sanitizers/page.rb @@ -160,11 +160,20 @@ def transform_sections_content(page, locale) if content = page[name][locale] return unless content.is_a?(String) - begin - page[name][locale] = Hjson.parse(content) - rescue Exception => e - raise Locomotive::Steam::JsonParsingError.new(e, page.template_path[locale], content) + if defined?(Hjson) + begin + page[name][locale] = Hjson.parse(content) + rescue Hjson::Error => e + raise Locomotive::Steam::JsonParsingError.new(e, page.template_path[locale], content) + end + else + begin + page[name][locale] = MultiJson.load(content) + rescue MultiJson::ParseError => e + raise Locomotive::Steam::JsonParsingError.new(e, page.template_path[locale], content) + end end + end end end diff --git a/lib/locomotive/steam/adapters/filesystem/sanitizers/section.rb b/lib/locomotive/steam/adapters/filesystem/sanitizers/section.rb index e3dc3d46..2eaa7107 100644 --- a/lib/locomotive/steam/adapters/filesystem/sanitizers/section.rb +++ b/lib/locomotive/steam/adapters/filesystem/sanitizers/section.rb @@ -21,10 +21,18 @@ def parse_json(entity) json, template = match[:json], match[:template] - begin - entity.definition = handle_aliases(Hjson.parse(json)) - rescue Exception => e - raise Locomotive::Steam::JsonParsingError.new(e, entity.template_path, json) + if defined?(Hjson) + begin + entity.definition = handle_aliases(Hjson.parse(json)) + rescue Hjson::Error => e + raise Locomotive::Steam::JsonParsingError.new(e, entity.template_path, json) + end + else + begin + entity.definition = handle_aliases(MultiJson.load(json)) + rescue MultiJson::ParseError => e + raise Locomotive::Steam::JsonParsingError.new(e, entity.template_path, json) + end end entity.template = template diff --git a/lib/locomotive/steam/adapters/filesystem/yaml_loader.rb b/lib/locomotive/steam/adapters/filesystem/yaml_loader.rb index dd49a897..6d963f07 100644 --- a/lib/locomotive/steam/adapters/filesystem/yaml_loader.rb +++ b/lib/locomotive/steam/adapters/filesystem/yaml_loader.rb @@ -51,10 +51,18 @@ def safe_json_load(path) json = File.read(path) - begin - Hjson.parse(json) - rescue Exception => e - raise Locomotive::Steam::JsonParsingError.new(e, path, json) + if defined?(Hjson) + begin + Hjson.parse(json) + rescue Hjson::Error => e + raise Locomotive::Steam::JsonParsingError.new(e, path, json) + end + else + begin + MultiJson.load(json) + rescue MultiJson::ParseError => e + raise Locomotive::Steam::JsonParsingError.new(e, path, json) + end end end diff --git a/locomotivecms_steam.gemspec b/locomotivecms_steam.gemspec index 5751c503..8866a2b5 100644 --- a/locomotivecms_steam.gemspec +++ b/locomotivecms_steam.gemspec @@ -26,7 +26,7 @@ Gem::Specification.new do |spec| spec.add_dependency 'httparty', '~> 0.16.0' spec.add_dependency 'chronic', '~> 0.10.2' spec.add_dependency 'bcrypt', '~> 3.1.11' - spec.add_dependency 'hjson', '~> 0.1.2' + spec.add_dependency 'multi_json', '~> 1.13.1' spec.add_dependency 'rack-rewrite', '~> 1.5.1' spec.add_dependency 'rack-cache', '~> 1.7.0'