From 7aa9df13f9072b827430ddd863f0be1c581b5923 Mon Sep 17 00:00:00 2001 From: Kyle Hargraves Date: Tue, 2 Dec 2014 11:49:05 -0600 Subject: [PATCH] Remove support for alternate JSON backends --- .travis.yml | 2 +- CHANGELOG.md | 5 + README.md | 19 ---- .../{Gemfile.multi_json.x => Gemfile.oj.x} | 3 +- gemfiles/Gemfile.yajl-ruby.x | 2 +- lib/json-schema.rb | 9 +- lib/json-schema/validator.rb | 91 ++----------------- test/files_test.rb | 4 - 8 files changed, 20 insertions(+), 115 deletions(-) rename gemfiles/{Gemfile.multi_json.x => Gemfile.oj.x} (65%) diff --git a/.travis.yml b/.travis.yml index e027c911..8848e3f2 100644 --- a/.travis.yml +++ b/.travis.yml @@ -18,7 +18,7 @@ install: matrix: include: - rvm: "2.3.0" - gemfile: "gemfiles/Gemfile.multi_json.x" + gemfile: "gemfiles/Gemfile.oj.x" - rvm: "2.3.0" gemfile: "gemfiles/Gemfile.yajl-ruby.x" - rvm: "2.3.0" diff --git a/CHANGELOG.md b/CHANGELOG.md index 3610dc7a..8380087f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,11 @@ This project adheres to [Semantic Versioning](http://semver.org/). ## [Unreleased] +### Changed + +- Support for `multi_json` and `yajl-ruby` have been dropped; the standard `json` library + (included in Ruby 1.9+) is always used. + ## [2.6.2] - 2016-05-13 ### Fixed diff --git a/README.md b/README.md index b32fafa6..fd66fee9 100644 --- a/README.md +++ b/README.md @@ -389,25 +389,6 @@ The `JSON::Schema::Reader` interface requires only an object which responds to documentation](http://www.rubydoc.info/github/ruby-json-schema/json-schema/master/JSON/Schema/Reader) for more information. -JSON Backends -------------- - -The JSON Schema library currently supports the `json` and `yajl-ruby` backend -JSON parsers. If either of these libraries are installed, they will be -automatically loaded and used to parse any JSON strings supplied by the user. - -If more than one of the supported JSON backends are installed, the `yajl-ruby` -parser is used by default. This can be changed by issuing the following before -validation: - -```ruby -JSON::Validator.json_backend = :json -``` - -Optionally, the JSON Schema library supports using the MultiJSON library for -selecting JSON backends. If the MultiJSON library is installed, it will be -autoloaded. - Notes ----- diff --git a/gemfiles/Gemfile.multi_json.x b/gemfiles/Gemfile.oj.x similarity index 65% rename from gemfiles/Gemfile.multi_json.x rename to gemfiles/Gemfile.oj.x index 349d18d8..e6a5f5a3 100644 --- a/gemfiles/Gemfile.multi_json.x +++ b/gemfiles/Gemfile.oj.x @@ -2,4 +2,5 @@ source "https://rubygems.org" gemspec :path => "../" -gem "multi_json" +gem "oj" +gem "oj_mimic_json" diff --git a/gemfiles/Gemfile.yajl-ruby.x b/gemfiles/Gemfile.yajl-ruby.x index c9f10cca..9a3c93cf 100644 --- a/gemfiles/Gemfile.yajl-ruby.x +++ b/gemfiles/Gemfile.yajl-ruby.x @@ -2,4 +2,4 @@ source "https://rubygems.org" gemspec :path => "../" -gem "yajl-ruby" +gem "yajl-ruby", :require => "yajl/json_gem" diff --git a/lib/json-schema.rb b/lib/json-schema.rb index 630001d7..41aba41f 100644 --- a/lib/json-schema.rb +++ b/lib/json-schema.rb @@ -1,13 +1,6 @@ require 'rubygems' -if Gem::Specification::find_all_by_name('multi_json').any? - require 'multi_json' - - # Force MultiJson to load an engine before we define the JSON constant here; otherwise, - # it looks for things that are under the JSON namespace that aren't there (since we have defined it here) - MultiJson.respond_to?(:adapter) ? MultiJson.adapter : MultiJson.engine -end - +require 'json' require 'json-schema/util/array_set' require 'json-schema/util/uri' require 'json-schema/schema' diff --git a/lib/json-schema/validator.rb b/lib/json-schema/validator.rb index 9b947ff8..8b697040 100644 --- a/lib/json-schema/validator.rb +++ b/lib/json-schema/validator.rb @@ -32,8 +32,6 @@ class Validator } @@validators = {} @@default_validator = nil - @@available_json_backends = [] - @@json_backend = nil @@serializer = nil @@mutex = Mutex.new @@ -389,53 +387,19 @@ def restore_default_formats(versions = ["draft1", "draft2", "draft3", "draft4", end def json_backend - if defined?(MultiJson) - MultiJson.respond_to?(:adapter) ? MultiJson.adapter : MultiJson.engine - else - @@json_backend - end + warn "[DEPRECATION NOTICE] Alternate JSON backends for json-schema are no longer used. If you would like to use a non-standard JSON backend, please use it in compatibility mode." + nil end def json_backend=(backend) - if defined?(MultiJson) - backend = backend == 'json' ? 'json_gem' : backend - MultiJson.respond_to?(:use) ? MultiJson.use(backend) : MultiJson.engine = backend - else - backend = backend.to_s - if @@available_json_backends.include?(backend) - @@json_backend = backend - else - raise JSON::Schema::JsonParseError.new("The JSON backend '#{backend}' could not be found.") - end - end + warn "[DEPRECATION NOTICE] Alternate JSON backends for json-schema are no longer used. If you would like to use a non-standard JSON backend, please use it in compatibility mode." end - def parse(s) - if defined?(MultiJson) - begin - MultiJson.respond_to?(:adapter) ? MultiJson.load(s) : MultiJson.decode(s) - rescue MultiJson::ParseError => e - raise JSON::Schema::JsonParseError.new(e.message) - end - else - case @@json_backend.to_s - when 'json' - begin - JSON.parse(s, :quirks_mode => true) - rescue JSON::ParserError => e - raise JSON::Schema::JsonParseError.new(e.message) - end - when 'yajl' - begin - json = StringIO.new(s) - parser = Yajl::Parser.new - parser.parse(json) or raise JSON::Schema::JsonParseError.new("The JSON could not be parsed by yajl") - rescue Yajl::ParseError => e - raise JSON::Schema::JsonParseError.new(e.message) - end - else - raise JSON::Schema::JsonParseError.new("No supported JSON parsers found. The following parsers are suported:\n * yajl-ruby\n * json") - end + def parse(string) + begin + JSON.parse(string, :quirks_mode => true) + rescue JSON::ParserError => e + raise JSON::Schema::JsonParseError.new(e.message) end end @@ -458,37 +422,6 @@ def merge_missing_values(source, destination) end end - if !defined?(MultiJson) - if Gem::Specification::find_all_by_name('json').any? - require 'json' - @@available_json_backends << 'json' - @@json_backend = 'json' - else - # Try force-loading json for rubies > 1.9.2 - begin - require 'json' - @@available_json_backends << 'json' - @@json_backend = 'json' - rescue LoadError - end - end - - - if Gem::Specification::find_all_by_name('yajl-ruby').any? - require 'yajl' - @@available_json_backends << 'yajl' - @@json_backend = 'yajl' - end - - if @@json_backend == 'yajl' - @@serializer = lambda{|o| Yajl::Encoder.encode(o) } - elsif @@json_backend == 'json' - @@serializer = lambda{|o| JSON.dump(o) } - else - @@serializer = lambda{|o| YAML.dump(o) } - end - end - private def validators_for_names(names) @@ -516,12 +449,8 @@ def validators_for_names(names) @@fake_uuid_generator = lambda{|s| JSON::Util::UUID.create_v5(s,JSON::Util::UUID::Nil).to_s } end - def serialize schema - if defined?(MultiJson) - MultiJson.respond_to?(:dump) ? MultiJson.dump(schema) : MultiJson.encode(schema) - else - @@serializer.call(schema) - end + def serialize(schema) + JSON.dump(schema) end def fake_uuid schema diff --git a/test/files_test.rb b/test/files_test.rb index 193a19de..505d0322 100644 --- a/test/files_test.rb +++ b/test/files_test.rb @@ -2,10 +2,6 @@ class FilesTest < Minitest::Test - # - # These tests are ONLY run if there is an appropriate JSON backend parser available - # - def test_schema_from_file assert_valid schema_fixture_path('good_schema_1.json'), { "a" => 5 } refute_valid schema_fixture_path('good_schema_1.json'), { "a" => "bad" }