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 Oct 5, 2016
1 parent 8e47deb commit 4400f02
Show file tree
Hide file tree
Showing 10 changed files with 42 additions and 125 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ matrix:
- rvm: "1.9"
gemfile: "gemfiles/Gemfile.json_v1.x"
- 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"
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ This project adheres to [Semantic Versioning](http://semver.org/).
- Made the `:clear_cache` option for `validate` also clear the URI parse cache
- Moved `JSON::Validator.absolutize_ref` and the ref manipulating code in
`JSON::Schema::RefAttribute` into `JSON::Util::URI`
- Support for `multi_json` and `yajl-ruby` have been dropped; the standard `json` library
(included in Ruby 1.9+) is always used.

## [2.7.0] - 2016-09-29

Expand Down
19 changes: 0 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -407,25 +407,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"
10 changes: 2 additions & 8 deletions lib/json-schema.rb
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
2 changes: 1 addition & 1 deletion lib/json-schema/schema/reader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,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]
Expand Down
17 changes: 17 additions & 0 deletions lib/json-schema/util/parser.rb
Original file line number Diff line number Diff line change
@@ -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
106 changes: 16 additions & 90 deletions lib/json-schema/validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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={})
Expand Down Expand Up @@ -378,54 +375,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)
Expand All @@ -447,37 +407,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 @@ -505,12 +434,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
Expand All @@ -522,7 +448,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
Expand All @@ -543,14 +469,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?
Expand All @@ -567,17 +493,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
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 4400f02

Please sign in to comment.