Skip to content

Commit 51c968d

Browse files
committed
add support for JSON.jl 1.x
This adds support for JSON.jl 1.x. The package will now be compatible with both 0.x and 1.x versions of JSON.
1 parent f553a42 commit 51c968d

6 files changed

Lines changed: 31 additions & 22 deletions

File tree

Project.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ keywords = ["Swagger", "OpenAPI", "REST"]
44
license = "MIT"
55
desc = "OpenAPI server and client helper for Julia"
66
authors = ["JuliaHub Inc."]
7-
version = "0.2.1"
7+
version = "0.2.2"
88

99
[deps]
1010
Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"
@@ -22,7 +22,7 @@ p7zip_jll = "3f19e933-33d8-53b3-aaab-bd5110c3b7a0"
2222
[compat]
2323
Downloads = "1"
2424
HTTP = "1"
25-
JSON = "0.20, 0.21"
25+
JSON = "0.20, 0.21, 1"
2626
LibCURL = "0.6"
2727
MIMEs = "0.1, 1"
2828
MbedTLS = "0.6.8, 0.7, 1"

src/OpenAPI.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,15 @@ using p7zip_jll
77
import Base: getindex, keys, length, iterate, hasproperty
88
import JSON: lower
99

10+
11+
const _JSON_PARSE_ALLOW_TRAILING = try; JSON.parse("1 "; allowtrailing=true); true; catch; false; end
12+
13+
if _JSON_PARSE_ALLOW_TRAILING
14+
_json_parse(io_or_str) = JSON.parse(io_or_str; allowtrailing=true)
15+
else
16+
_json_parse(io_or_str) = JSON.parse(io_or_str)
17+
end
18+
1019
include("commontypes.jl")
1120
include("datetime.jl")
1221
include("val.jl")

src/client.jl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ using MIMEs
1212

1313
import Base: convert, show, summary, getproperty, setproperty!, iterate
1414
import ..OpenAPI: APIModel, UnionAPIModel, OneOfAPIModel, AnyOfAPIModel, APIClientImpl, OpenAPIException, InvocationException, to_json, from_json, validate_property, property_type
15-
import ..OpenAPI: str2zoneddatetime, str2datetime, str2date
15+
import ..OpenAPI: str2zoneddatetime, str2datetime, str2date, _json_parse
1616

1717
include("client/clienttypes.jl")
1818
include("client/chunk_readers.jl")
@@ -159,8 +159,8 @@ response(::Type{DateTime}, data) = str2datetime(data)
159159
response(::Type{Date}, data) = str2date(data)
160160

161161
response(::Type{T}, data) where {T} = convert(T, data)
162-
response(::Type{T}, data::Dict{String,Any}) where {T} = from_json(T, data)::T
163-
response(::Type{T}, data::Dict{String,Any}) where {T<:Dict} = convert(T, data)
162+
response(::Type{T}, data::AbstractDict{String,Any}) where {T} = from_json(T, data)::T
163+
response(::Type{T}, data::AbstractDict{String,Any}) where {T<:Dict} = convert(T, Dict{String,Any}(data))
164164
response(::Type{Vector{T}}, data::Vector{V}) where {T,V} = T[response(T, v) for v in data]
165165

166166
noop_pre_request_hook(ctx::Ctx) = ctx
@@ -299,14 +299,14 @@ end
299299

300300
Base.hasproperty(o::T, name::Symbol) where {T<:APIModel} = ((name in propertynames(o)) && (getproperty(o, name) !== nothing))
301301

302-
convert(::Type{T}, json::Dict{String,Any}) where {T<:APIModel} = from_json(T, json)
302+
convert(::Type{T}, json::AbstractDict{String,Any}) where {T<:APIModel} = from_json(T, json)
303303
convert(::Type{T}, v::Nothing) where {T<:APIModel} = T()
304304
convert(::Type{T}, v::T) where {T<:OneOfAPIModel} = v
305-
convert(::Type{T}, json::Dict{String,Any}) where {T<:OneOfAPIModel} = from_json(T, json)
305+
convert(::Type{T}, json::AbstractDict{String,Any}) where {T<:OneOfAPIModel} = from_json(T, json)
306306
convert(::Type{T}, v) where {T<:OneOfAPIModel} = T(v)
307307
convert(::Type{T}, v::String) where {T<:OneOfAPIModel} = T(v)
308308
convert(::Type{T}, v::T) where {T<:AnyOfAPIModel} = v
309-
convert(::Type{T}, json::Dict{String,Any}) where {T<:AnyOfAPIModel} = from_json(T, json)
309+
convert(::Type{T}, json::AbstractDict{String,Any}) where {T<:AnyOfAPIModel} = from_json(T, json)
310310
convert(::Type{T}, v) where {T<:AnyOfAPIModel} = T(v)
311311
convert(::Type{T}, v::String) where {T<:AnyOfAPIModel} = T(v)
312312

src/client/chunk_readers.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ function Base.iterate(iter::JSONChunkReader, _state=nothing)
3434
end
3535
end
3636
eof(iter.buffered_input) && return nothing
37-
valid_json = JSON.parse(iter.buffered_input)
37+
valid_json = _json_parse(iter.buffered_input)
3838
bytes = convert(Vector{UInt8}, codeunits(JSON.json(valid_json)))
3939
return (bytes, iter)
4040
end

src/json.jl

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ end
4242

4343
is_deep_explode(sctx::StyleCtx) = sctx.name == "deepObject" && sctx.is_explode
4444

45-
function deep_object_to_array(src::Dict)
45+
function deep_object_to_array(src::AbstractDict)
4646
keys_are_int = all(key -> occursin(r"^\d+$", key), keys(src))
4747
if keys_are_int
4848
sorted_keys = sort(collect(keys(src)), by=x->parse(Int, x))
@@ -58,14 +58,14 @@ end
5858

5959
to_json(o) = JSON.json(o)
6060

61-
from_json(::Type{Union{Nothing,T}}, json::Dict{String,Any}; stylectx=nothing) where {T} = from_json(T, json; stylectx)
62-
from_json(::Type{T}, json::Dict{String,Any}; stylectx=nothing) where {T} = from_json(T(), json; stylectx)
63-
from_json(::Type{T}, json::Dict{String,Any}; stylectx=nothing) where {T <: Dict} = convert(T, json)
64-
from_json(::Type{T}, j::Dict{String,Any}; stylectx=nothing) where {T <: String} = to_json(j)
65-
from_json(::Type{Any}, j::Dict{String,Any}; stylectx=nothing) = j
61+
from_json(::Type{Union{Nothing,T}}, json::AbstractDict{String,Any}; stylectx=nothing) where {T} = from_json(T, json; stylectx)
62+
from_json(::Type{T}, json::AbstractDict{String,Any}; stylectx=nothing) where {T} = from_json(T(), json; stylectx)
63+
from_json(::Type{T}, json::AbstractDict{String,Any}; stylectx=nothing) where {T <: Dict} = convert(T, Dict{String,Any}(json))
64+
from_json(::Type{T}, j::AbstractDict{String,Any}; stylectx=nothing) where {T <: String} = to_json(j)
65+
from_json(::Type{Any}, j::AbstractDict{String,Any}; stylectx=nothing) = j
6666
from_json(::Type{Vector{T}}, j::Vector{Any}; stylectx=nothing) where {T} = j
6767

68-
function from_json(::Type{Vector{T}}, json::Dict{String, Any}; stylectx=nothing) where {T}
68+
function from_json(::Type{Vector{T}}, json::AbstractDict{String, Any}; stylectx=nothing) where {T}
6969
if !isnothing(stylectx) && is_deep_explode(stylectx)
7070
cvt = deep_object_to_array(json)
7171
if isa(cvt, Vector)
@@ -78,7 +78,7 @@ function from_json(::Type{Vector{T}}, json::Dict{String, Any}; stylectx=nothing)
7878
end
7979
end
8080

81-
function from_json(o::T, json::Dict{String,Any};stylectx=nothing) where {T <: UnionAPIModel}
81+
function from_json(o::T, json::AbstractDict{String,Any};stylectx=nothing) where {T <: UnionAPIModel}
8282
return from_json(o, :value, json;stylectx)
8383
end
8484

@@ -88,16 +88,16 @@ function from_json(o::T, val::Union{String,Real};stylectx=nothing) where {T <: U
8888
return o
8989
end
9090

91-
function from_json(o::T, json::Dict{String,Any};stylectx=nothing) where {T <: APIModel}
91+
function from_json(o::T, json::AbstractDict{String,Any};stylectx=nothing) where {T <: APIModel}
9292
jsonkeys = [Symbol(k) for k in keys(json)]
9393
for name in intersect(propertynames(o), jsonkeys)
9494
from_json(o, name, json[String(name)];stylectx)
9595
end
9696
return o
9797
end
9898

99-
function from_json(o::T, name::Symbol, json::Dict{String,Any};stylectx=nothing) where {T <: APIModel}
100-
ftype = (T <: UnionAPIModel) ? property_type(T, name, json) : property_type(T, name)
99+
function from_json(o::T, name::Symbol, json::AbstractDict{String,Any};stylectx=nothing) where {T <: APIModel}
100+
ftype = (T <: UnionAPIModel) ? property_type(T, name, Dict{String,Any}(json)) : property_type(T, name)
101101
fval = from_json(ftype, json; stylectx)
102102
setfield!(o, name, convert(ftype, fval))
103103
return o

src/server.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ to_param_type(::Type{Vector{UInt8}}, val::String; stylectx=nothing) = convert(Ve
8989
to_param_type(::Type{Vector{T}}, val::Vector{T}, _collection_format::Union{String,Nothing}; stylectx=nothing) where {T} = val
9090
to_param_type(::Type{Vector{T}}, json::Vector{Any}; stylectx=nothing) where {T} = [to_param_type(T, x; stylectx) for x in json]
9191

92-
function to_param_type(::Type{Vector{T}}, json::Dict{String, Any}; stylectx=nothing) where {T}
92+
function to_param_type(::Type{Vector{T}}, json::AbstractDict{String, Any}; stylectx=nothing) where {T}
9393
if !isnothing(stylectx) && is_deep_explode(stylectx)
9494
cvt = deep_object_to_array(json)
9595
if isa(cvt, Vector)
@@ -103,7 +103,7 @@ function to_param_type(::Type{T}, strval::String; stylectx=nothing) where {T <:
103103
from_json(T, JSON.parse(strval); stylectx)
104104
end
105105

106-
function to_param_type(::Type{T}, json::Dict{String,Any}; stylectx=nothing) where {T <: APIModel}
106+
function to_param_type(::Type{T}, json::AbstractDict{String,Any}; stylectx=nothing) where {T <: APIModel}
107107
from_json(T, json; stylectx)
108108
end
109109

0 commit comments

Comments
 (0)