diff --git a/.travis.yml b/.travis.yml index 832bfd18..e0de9d8f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -18,7 +18,7 @@ install: matrix: include: - rvm: "2.3.1" - gemfile: "gemfiles/Gemfile.multi_json.x" + gemfile: "gemfiles/Gemfile.oj.x" - rvm: "2.3.1" gemfile: "gemfiles/Gemfile.yajl-ruby.x" - rvm: "2.3.1" 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..76ca1c78 100644 --- a/lib/json-schema.rb +++ b/lib/json-schema.rb @@ -1,14 +1,8 @@ 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/parser' require 'json-schema/util/uri' require 'json-schema/schema' require 'json-schema/schema/reader' diff --git a/lib/json-schema/schema/reader.rb b/lib/json-schema/schema/reader.rb index 1493d1f0..805df924 100644 --- a/lib/json-schema/schema/reader.rb +++ b/lib/json-schema/schema/reader.rb @@ -67,7 +67,7 @@ def read(location) read_uri(uri) end - JSON::Schema.new(JSON::Validator.parse(body), uri) + JSON::Schema.new(JSON::Util::Parser.parse(body), uri) end # @param uri [Addressable::URI] diff --git a/lib/json-schema/util/parser.rb b/lib/json-schema/util/parser.rb new file mode 100644 index 00000000..736a45a8 --- /dev/null +++ b/lib/json-schema/util/parser.rb @@ -0,0 +1,17 @@ +module JSON + module Util + module Parser + def self.parse(string) + begin + JSON.parse(string, :quirks_mode => true) + rescue JSON::ParserError => e + raise JSON::Schema::JsonParseError.new(e.message) + end + end + + def self.serialize(hash) + JSON.dump(hash) + end + end + end +end diff --git a/lib/json-schema/validator.rb b/lib/json-schema/validator.rb index 9b947ff8..e55d0e27 100644 --- a/lib/json-schema/validator.rb +++ b/lib/json-schema/validator.rb @@ -32,9 +32,6 @@ class Validator } @@validators = {} @@default_validator = nil - @@available_json_backends = [] - @@json_backend = nil - @@serializer = nil @@mutex = Mutex.new def initialize(schema_data, data, opts={}) @@ -389,54 +386,17 @@ 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 - end + def parse(string) + warn "[DEPRECATION NOTICE] JSON::Validator#parse has been moved to JSON::Util::Parser." + JSON::Util::Parser.parse(string) end def merge_missing_values(source, destination) @@ -458,37 +418,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 +445,9 @@ 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) + warn "[DEPRECATION NOTICE] JSON::Validator#serialize has been moved to JSON::Util::Parser." + JSON::Util::Parser.serialize(schema) end def fake_uuid schema @@ -533,7 +459,7 @@ def initialize_schema(schema) begin # Build a fake URI for this schema_uri = JSON::Util::URI.parse(fake_uuid(schema)) - schema = JSON::Schema.new(JSON::Validator.parse(schema), schema_uri, @options[:version]) + schema = JSON::Schema.new(JSON::Util::Parser.parse(schema), schema_uri, @options[:version]) if @options[:list] && @options[:fragment].nil? schema = schema.to_array_schema end @@ -554,14 +480,14 @@ def initialize_schema(schema) schema = self.class.schema_for_uri(schema_uri) if @options[:list] && @options[:fragment].nil? schema = schema.to_array_schema - schema.uri = JSON::Util::URI.parse(fake_uuid(serialize(schema.schema))) + schema.uri = JSON::Util::URI.parse(fake_uuid(JSON::Util::Parser.serialize(schema.schema))) Validator.add_schema(schema) end schema end end elsif schema.is_a?(Hash) - schema_uri = JSON::Util::URI.parse(fake_uuid(serialize(schema))) + schema_uri = JSON::Util::URI.parse(fake_uuid(JSON::Util::Parser.serialize(schema))) schema = JSON::Schema.stringify(schema) schema = JSON::Schema.new(schema, schema_uri, @options[:version]) if @options[:list] && @options[:fragment].nil? @@ -578,17 +504,17 @@ def initialize_schema(schema) def initialize_data(data) if @options[:parse_data] if @options[:json] - data = JSON::Validator.parse(data) + data = JSON::Util::Parser.parse(data) elsif @options[:uri] json_uri = Util::URI.normalized_uri(data) - data = JSON::Validator.parse(custom_open(json_uri)) + data = JSON::Util::Parser.parse(custom_open(json_uri)) elsif data.is_a?(String) begin - data = JSON::Validator.parse(data) + data = JSON::Util::Parser.parse(data) rescue JSON::Schema::JsonParseError begin json_uri = Util::URI.normalized_uri(data) - data = JSON::Validator.parse(custom_open(json_uri)) + data = JSON::Util::Parser.parse(custom_open(json_uri)) rescue JSON::Schema::JsonLoadError # Silently discard the error - use the data as-is end 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" }