diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 3d6d271..1568a71 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -11,12 +11,12 @@ jobs: strategy: fail-fast: false matrix: - version: ['1.6', '1'] # Test against LTS and current minor release + version: ['1.10', '1'] # Test against LTS and current minor release os: [ubuntu-latest, macOS-latest, windows-latest] arch: [x64] include: # Also test against 32-bit Linux on LTS. - - version: '1.6' + - version: '1.10' os: ubuntu-latest arch: x86 steps: diff --git a/Project.toml b/Project.toml index 09f448e..88aad14 100644 --- a/Project.toml +++ b/Project.toml @@ -1,15 +1,20 @@ name = "JSONSchema" uuid = "7d188eb4-7ad8-530c-ae41-71a32a6d4692" -version = "1.4.1" +version = "1.5.0" [deps] Downloads = "f43a241f-c20a-4ad4-852c-f6b1247861c6" JSON = "682c06a0-de6a-54ab-a142-c8b1cf79cde6" -JSON3 = "0f8b85d8-7281-11e9-16c2-39a750bddbf1" URIs = "5c2747f8-b7ea-4ff2-ba2e-563bfd36b1d4" +[weakdeps] +JSON3 = "0f8b85d8-7281-11e9-16c2-39a750bddbf1" + +[extensions] +JSONSchemaJSON3Ext = "JSON3" + [compat] JSON = "0.21" JSON3 = "1" URIs = "1" -julia = "1.6" +julia = "1.9" diff --git a/ext/JSONSchemaJSON3Ext.jl b/ext/JSONSchemaJSON3Ext.jl new file mode 100644 index 0000000..07cbb3b --- /dev/null +++ b/ext/JSONSchemaJSON3Ext.jl @@ -0,0 +1,38 @@ +# Copyright (c) 2018: fredo-dedup and contributors +# +# Use of this source code is governed by an MIT-style license that can be found +# in the LICENSE.md file or at https://opensource.org/licenses/MIT. + +module JSONSchemaJSON3Ext + +import JSONSchema +import JSON3 + +_to_base_julia(x) = x + +_to_base_julia(x::JSON3.Array) = _to_base_julia.(x) + +# This method unintentionally allows JSON3.Object{Symbol,Any} objects as both +# data and the schema because it converts to Dict{String,Any}. Because we don't +# similarly convert Base.Dict, Dict{Symbol,Any} results in errors. This can be +# confusing to users. +# +# We can't make this method more restrictive because that would break backwards +# compatibility. For more details, see: +# https://github.com/fredo-dedup/JSONSchema.jl/issues/62 +function _to_base_julia(x::JSON3.Object) + return Dict{String,Any}(string(k) => _to_base_julia(v) for (k, v) in x) +end + +function JSONSchema.validate( + schema::JSONSchema.Schema, + x::Union{JSON3.Object,JSON3.Array}, +) + return JSONSchema.validate(schema, _to_base_julia(x)) +end + +function JSONSchema.Schema(schema::JSON3.Object; kwargs...) + return JSONSchema.Schema(_to_base_julia(schema); kwargs...) +end + +end diff --git a/src/JSONSchema.jl b/src/JSONSchema.jl index 7107393..1fec843 100644 --- a/src/JSONSchema.jl +++ b/src/JSONSchema.jl @@ -7,7 +7,6 @@ module JSONSchema import Downloads import JSON -import JSON3 import URIs export Schema, validate diff --git a/src/schema.jl b/src/schema.jl index c7e2406..2e36033 100644 --- a/src/schema.jl +++ b/src/schema.jl @@ -225,15 +225,6 @@ function build_id_map!( return end -# Turning JSON3 read files in to base Julia dicts with string keys -_to_base_julia(x) = x - -_to_base_julia(x::JSON3.Array) = _to_base_julia.(x) - -function _to_base_julia(x::JSON3.Object) - return Dict{String,Any}(string(k) => _to_base_julia(v) for (k, v) in x) -end - """ Schema(schema::AbstractDict; parent_dir::String = abspath(".")) @@ -303,8 +294,4 @@ my_schema = Schema( """ Schema(schema::String; kwargs...) = Schema(JSON.parse(schema); kwargs...) -function Schema(schema::JSON3.Object; kwargs...) - return Schema(_to_base_julia(schema); kwargs...) -end - Base.show(io::IO, ::Schema) = print(io, "A JSONSchema") diff --git a/src/validation.jl b/src/validation.jl index 0e2bb96..ad5d6b1 100644 --- a/src/validation.jl +++ b/src/validation.jl @@ -69,10 +69,6 @@ function validate(schema::Schema, x) return _validate(x, schema.data, "") end -function validate(schema::Schema, x::Union{JSON3.Object,JSON3.Array}) - return validate(schema, _to_base_julia(x)) -end - Base.isvalid(schema::Schema, x) = validate(schema, x) === nothing # Fallbacks for the opposite argument. diff --git a/test/Project.toml b/test/Project.toml index c8eeec0..4d06c99 100644 --- a/test/Project.toml +++ b/test/Project.toml @@ -13,4 +13,4 @@ JSON = "0.21" JSON3 = "1" OrderedCollections = "1" ZipFile = "0.8, 0.9, 0.10" -julia = "1.6" +julia = "1.9"