Skip to content

Commit

Permalink
Remove support for alternate JSON backends
Browse files Browse the repository at this point in the history
  • Loading branch information
pd authored and iainbeeston committed Jun 29, 2016
1 parent ab96e9a commit 7aa9df1
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 115 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 0 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
-----

Expand Down
3 changes: 2 additions & 1 deletion gemfiles/Gemfile.multi_json.x → gemfiles/Gemfile.oj.x
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ source "https://rubygems.org"

gemspec :path => "../"

gem "multi_json"
gem "oj"
gem "oj_mimic_json"
2 changes: 1 addition & 1 deletion gemfiles/Gemfile.yajl-ruby.x
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ source "https://rubygems.org"

gemspec :path => "../"

gem "yajl-ruby"
gem "yajl-ruby", :require => "yajl/json_gem"
9 changes: 1 addition & 8 deletions lib/json-schema.rb
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
91 changes: 10 additions & 81 deletions lib/json-schema/validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ class Validator
}
@@validators = {}
@@default_validator = nil
@@available_json_backends = []
@@json_backend = nil
@@serializer = nil
@@mutex = Mutex.new

Expand Down Expand Up @@ -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

Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down
4 changes: 0 additions & 4 deletions test/files_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down

0 comments on commit 7aa9df1

Please sign in to comment.