diff --git a/examples/generated/AcmeFlat.jl b/examples/generated/AcmeFlat.jl index c34118c..29b6142 100644 --- a/examples/generated/AcmeFlat.jl +++ b/examples/generated/AcmeFlat.jl @@ -459,7 +459,7 @@ function fromjson(::Type{T}, json::AbstractString) where {T} F = _jsfloattype(T) # numbers are parsed as `Float64` by JSON.jl, except when another floating-point type # is asked for: then they are parsed with full precision - return fromdict(T, F === Float64 ? JSON.parse(json) : _jsparsebig(JSON.lazy(json))) + return fromdict(T, F === Float64 ? JSON.parse(json) : _jsparsebig(json)) end _jsfloattype(::Type) = Float64 @@ -467,23 +467,26 @@ _jsfloattype(::Type{T}) where {T <: AbstractSchemaObject} = T isa DataType && length(T.parameters) == 2 ? T.parameters[2] : Float64 _jsfloattype(::Type{Vector{T}}) where {T} = _jsfloattype(T) -# Materialize a lazily parsed JSON value, parsing non-integer numbers as `BigFloat` (at the -# current precision of `BigFloat`), so that they can be converted to any real type. -function _jsparsebig(x) - t = JSON.gettype(x) - if t == JSON.JSONTypes.OBJECT +# Parse the JSON text `json`, parsing non-integer numbers as `BigFloat` (at the current +# precision of `BigFloat`), so that they can be converted to any real type. Objects and +# arrays are read one level at a time, keeping the raw text of their values (`JSONText`), +# which is then parsed in turn, so only the public API of JSON.jl is used. +function _jsparsebig(json::AbstractString) + s = lstrip(json) + c = isempty(s) ? ' ' : first(s) + if c == '{' obj = JSON.Object{String, Any}() - JSON.applyobject((k, v) -> (obj[String(k)] = _jsparsebig(v); nothing), x) + for (k, v) in JSON.parse(s, JSON.Object{String, JSON.JSONText}) + obj[k] = _jsparsebig(v.value) + end return obj - elseif t == JSON.JSONTypes.ARRAY - arr = Any[] - JSON.applyarray((i, v) -> (push!(arr, _jsparsebig(v)); nothing), x) - return arr - elseif t == JSON.JSONTypes.NUMBER - v = JSON.parse(x) - return v isa Integer ? v : JSON.parse(x, BigFloat) + elseif c == '[' + return Any[_jsparsebig(v.value) for v in JSON.parse(s, Vector{JSON.JSONText})] + elseif c == '-' || isdigit(c) + v = JSON.parse(s) + return v isa Integer ? v : JSON.parse(s, BigFloat) end - return JSON.parse(x) + return JSON.parse(s) end """ diff --git a/examples/generated/AcmeNested.jl b/examples/generated/AcmeNested.jl index e84bafd..d27729a 100644 --- a/examples/generated/AcmeNested.jl +++ b/examples/generated/AcmeNested.jl @@ -459,7 +459,7 @@ function fromjson(::Type{T}, json::AbstractString) where {T} F = _jsfloattype(T) # numbers are parsed as `Float64` by JSON.jl, except when another floating-point type # is asked for: then they are parsed with full precision - return fromdict(T, F === Float64 ? JSON.parse(json) : _jsparsebig(JSON.lazy(json))) + return fromdict(T, F === Float64 ? JSON.parse(json) : _jsparsebig(json)) end _jsfloattype(::Type) = Float64 @@ -467,23 +467,26 @@ _jsfloattype(::Type{T}) where {T <: AbstractSchemaObject} = T isa DataType && length(T.parameters) == 2 ? T.parameters[2] : Float64 _jsfloattype(::Type{Vector{T}}) where {T} = _jsfloattype(T) -# Materialize a lazily parsed JSON value, parsing non-integer numbers as `BigFloat` (at the -# current precision of `BigFloat`), so that they can be converted to any real type. -function _jsparsebig(x) - t = JSON.gettype(x) - if t == JSON.JSONTypes.OBJECT +# Parse the JSON text `json`, parsing non-integer numbers as `BigFloat` (at the current +# precision of `BigFloat`), so that they can be converted to any real type. Objects and +# arrays are read one level at a time, keeping the raw text of their values (`JSONText`), +# which is then parsed in turn, so only the public API of JSON.jl is used. +function _jsparsebig(json::AbstractString) + s = lstrip(json) + c = isempty(s) ? ' ' : first(s) + if c == '{' obj = JSON.Object{String, Any}() - JSON.applyobject((k, v) -> (obj[String(k)] = _jsparsebig(v); nothing), x) + for (k, v) in JSON.parse(s, JSON.Object{String, JSON.JSONText}) + obj[k] = _jsparsebig(v.value) + end return obj - elseif t == JSON.JSONTypes.ARRAY - arr = Any[] - JSON.applyarray((i, v) -> (push!(arr, _jsparsebig(v)); nothing), x) - return arr - elseif t == JSON.JSONTypes.NUMBER - v = JSON.parse(x) - return v isa Integer ? v : JSON.parse(x, BigFloat) + elseif c == '[' + return Any[_jsparsebig(v.value) for v in JSON.parse(s, Vector{JSON.JSONText})] + elseif c == '-' || isdigit(c) + v = JSON.parse(s) + return v isa Integer ? v : JSON.parse(s, BigFloat) end - return JSON.parse(x) + return JSON.parse(s) end """ diff --git a/examples/generated/AcmeNestedMinimal.jl b/examples/generated/AcmeNestedMinimal.jl index de86e76..34ebe6d 100644 --- a/examples/generated/AcmeNestedMinimal.jl +++ b/examples/generated/AcmeNestedMinimal.jl @@ -459,7 +459,7 @@ function fromjson(::Type{T}, json::AbstractString) where {T} F = _jsfloattype(T) # numbers are parsed as `Float64` by JSON.jl, except when another floating-point type # is asked for: then they are parsed with full precision - return fromdict(T, F === Float64 ? JSON.parse(json) : _jsparsebig(JSON.lazy(json))) + return fromdict(T, F === Float64 ? JSON.parse(json) : _jsparsebig(json)) end _jsfloattype(::Type) = Float64 @@ -467,23 +467,26 @@ _jsfloattype(::Type{T}) where {T <: AbstractSchemaObject} = T isa DataType && length(T.parameters) == 2 ? T.parameters[2] : Float64 _jsfloattype(::Type{Vector{T}}) where {T} = _jsfloattype(T) -# Materialize a lazily parsed JSON value, parsing non-integer numbers as `BigFloat` (at the -# current precision of `BigFloat`), so that they can be converted to any real type. -function _jsparsebig(x) - t = JSON.gettype(x) - if t == JSON.JSONTypes.OBJECT +# Parse the JSON text `json`, parsing non-integer numbers as `BigFloat` (at the current +# precision of `BigFloat`), so that they can be converted to any real type. Objects and +# arrays are read one level at a time, keeping the raw text of their values (`JSONText`), +# which is then parsed in turn, so only the public API of JSON.jl is used. +function _jsparsebig(json::AbstractString) + s = lstrip(json) + c = isempty(s) ? ' ' : first(s) + if c == '{' obj = JSON.Object{String, Any}() - JSON.applyobject((k, v) -> (obj[String(k)] = _jsparsebig(v); nothing), x) + for (k, v) in JSON.parse(s, JSON.Object{String, JSON.JSONText}) + obj[k] = _jsparsebig(v.value) + end return obj - elseif t == JSON.JSONTypes.ARRAY - arr = Any[] - JSON.applyarray((i, v) -> (push!(arr, _jsparsebig(v)); nothing), x) - return arr - elseif t == JSON.JSONTypes.NUMBER - v = JSON.parse(x) - return v isa Integer ? v : JSON.parse(x, BigFloat) + elseif c == '[' + return Any[_jsparsebig(v.value) for v in JSON.parse(s, Vector{JSON.JSONText})] + elseif c == '-' || isdigit(c) + v = JSON.parse(s) + return v isa Integer ? v : JSON.parse(s, BigFloat) end - return JSON.parse(x) + return JSON.parse(s) end """ diff --git a/examples/generated/AcmeNestedMod.jl b/examples/generated/AcmeNestedMod.jl index d09a1eb..b99a808 100644 --- a/examples/generated/AcmeNestedMod.jl +++ b/examples/generated/AcmeNestedMod.jl @@ -459,7 +459,7 @@ function fromjson(::Type{T}, json::AbstractString) where {T} F = _jsfloattype(T) # numbers are parsed as `Float64` by JSON.jl, except when another floating-point type # is asked for: then they are parsed with full precision - return fromdict(T, F === Float64 ? JSON.parse(json) : _jsparsebig(JSON.lazy(json))) + return fromdict(T, F === Float64 ? JSON.parse(json) : _jsparsebig(json)) end _jsfloattype(::Type) = Float64 @@ -467,23 +467,26 @@ _jsfloattype(::Type{T}) where {T <: AbstractSchemaObject} = T isa DataType && length(T.parameters) == 2 ? T.parameters[2] : Float64 _jsfloattype(::Type{Vector{T}}) where {T} = _jsfloattype(T) -# Materialize a lazily parsed JSON value, parsing non-integer numbers as `BigFloat` (at the -# current precision of `BigFloat`), so that they can be converted to any real type. -function _jsparsebig(x) - t = JSON.gettype(x) - if t == JSON.JSONTypes.OBJECT +# Parse the JSON text `json`, parsing non-integer numbers as `BigFloat` (at the current +# precision of `BigFloat`), so that they can be converted to any real type. Objects and +# arrays are read one level at a time, keeping the raw text of their values (`JSONText`), +# which is then parsed in turn, so only the public API of JSON.jl is used. +function _jsparsebig(json::AbstractString) + s = lstrip(json) + c = isempty(s) ? ' ' : first(s) + if c == '{' obj = JSON.Object{String, Any}() - JSON.applyobject((k, v) -> (obj[String(k)] = _jsparsebig(v); nothing), x) + for (k, v) in JSON.parse(s, JSON.Object{String, JSON.JSONText}) + obj[k] = _jsparsebig(v.value) + end return obj - elseif t == JSON.JSONTypes.ARRAY - arr = Any[] - JSON.applyarray((i, v) -> (push!(arr, _jsparsebig(v)); nothing), x) - return arr - elseif t == JSON.JSONTypes.NUMBER - v = JSON.parse(x) - return v isa Integer ? v : JSON.parse(x, BigFloat) + elseif c == '[' + return Any[_jsparsebig(v.value) for v in JSON.parse(s, Vector{JSON.JSONText})] + elseif c == '-' || isdigit(c) + v = JSON.parse(s) + return v isa Integer ? v : JSON.parse(s, BigFloat) end - return JSON.parse(x) + return JSON.parse(s) end """ diff --git a/examples/generated/AcmeWithReference.jl b/examples/generated/AcmeWithReference.jl index 81caf21..7b15c06 100644 --- a/examples/generated/AcmeWithReference.jl +++ b/examples/generated/AcmeWithReference.jl @@ -459,7 +459,7 @@ function fromjson(::Type{T}, json::AbstractString) where {T} F = _jsfloattype(T) # numbers are parsed as `Float64` by JSON.jl, except when another floating-point type # is asked for: then they are parsed with full precision - return fromdict(T, F === Float64 ? JSON.parse(json) : _jsparsebig(JSON.lazy(json))) + return fromdict(T, F === Float64 ? JSON.parse(json) : _jsparsebig(json)) end _jsfloattype(::Type) = Float64 @@ -467,23 +467,26 @@ _jsfloattype(::Type{T}) where {T <: AbstractSchemaObject} = T isa DataType && length(T.parameters) == 2 ? T.parameters[2] : Float64 _jsfloattype(::Type{Vector{T}}) where {T} = _jsfloattype(T) -# Materialize a lazily parsed JSON value, parsing non-integer numbers as `BigFloat` (at the -# current precision of `BigFloat`), so that they can be converted to any real type. -function _jsparsebig(x) - t = JSON.gettype(x) - if t == JSON.JSONTypes.OBJECT +# Parse the JSON text `json`, parsing non-integer numbers as `BigFloat` (at the current +# precision of `BigFloat`), so that they can be converted to any real type. Objects and +# arrays are read one level at a time, keeping the raw text of their values (`JSONText`), +# which is then parsed in turn, so only the public API of JSON.jl is used. +function _jsparsebig(json::AbstractString) + s = lstrip(json) + c = isempty(s) ? ' ' : first(s) + if c == '{' obj = JSON.Object{String, Any}() - JSON.applyobject((k, v) -> (obj[String(k)] = _jsparsebig(v); nothing), x) + for (k, v) in JSON.parse(s, JSON.Object{String, JSON.JSONText}) + obj[k] = _jsparsebig(v.value) + end return obj - elseif t == JSON.JSONTypes.ARRAY - arr = Any[] - JSON.applyarray((i, v) -> (push!(arr, _jsparsebig(v)); nothing), x) - return arr - elseif t == JSON.JSONTypes.NUMBER - v = JSON.parse(x) - return v isa Integer ? v : JSON.parse(x, BigFloat) + elseif c == '[' + return Any[_jsparsebig(v.value) for v in JSON.parse(s, Vector{JSON.JSONText})] + elseif c == '-' || isdigit(c) + v = JSON.parse(s) + return v isa Integer ? v : JSON.parse(s, BigFloat) end - return JSON.parse(x) + return JSON.parse(s) end """ diff --git a/examples/generated/BeerJSONTypes/runtime.jl b/examples/generated/BeerJSONTypes/runtime.jl index d564392..cb66172 100644 --- a/examples/generated/BeerJSONTypes/runtime.jl +++ b/examples/generated/BeerJSONTypes/runtime.jl @@ -446,7 +446,7 @@ function fromjson(::Type{T}, json::AbstractString) where {T} F = _jsfloattype(T) # numbers are parsed as `Float64` by JSON.jl, except when another floating-point type # is asked for: then they are parsed with full precision - return fromdict(T, F === Float64 ? JSON.parse(json) : _jsparsebig(JSON.lazy(json))) + return fromdict(T, F === Float64 ? JSON.parse(json) : _jsparsebig(json)) end _jsfloattype(::Type) = Float64 @@ -454,23 +454,26 @@ _jsfloattype(::Type{T}) where {T <: AbstractSchemaObject} = T isa DataType && length(T.parameters) == 2 ? T.parameters[2] : Float64 _jsfloattype(::Type{Vector{T}}) where {T} = _jsfloattype(T) -# Materialize a lazily parsed JSON value, parsing non-integer numbers as `BigFloat` (at the -# current precision of `BigFloat`), so that they can be converted to any real type. -function _jsparsebig(x) - t = JSON.gettype(x) - if t == JSON.JSONTypes.OBJECT +# Parse the JSON text `json`, parsing non-integer numbers as `BigFloat` (at the current +# precision of `BigFloat`), so that they can be converted to any real type. Objects and +# arrays are read one level at a time, keeping the raw text of their values (`JSONText`), +# which is then parsed in turn, so only the public API of JSON.jl is used. +function _jsparsebig(json::AbstractString) + s = lstrip(json) + c = isempty(s) ? ' ' : first(s) + if c == '{' obj = JSON.Object{String, Any}() - JSON.applyobject((k, v) -> (obj[String(k)] = _jsparsebig(v); nothing), x) + for (k, v) in JSON.parse(s, JSON.Object{String, JSON.JSONText}) + obj[k] = _jsparsebig(v.value) + end return obj - elseif t == JSON.JSONTypes.ARRAY - arr = Any[] - JSON.applyarray((i, v) -> (push!(arr, _jsparsebig(v)); nothing), x) - return arr - elseif t == JSON.JSONTypes.NUMBER - v = JSON.parse(x) - return v isa Integer ? v : JSON.parse(x, BigFloat) + elseif c == '[' + return Any[_jsparsebig(v.value) for v in JSON.parse(s, Vector{JSON.JSONText})] + elseif c == '-' || isdigit(c) + v = JSON.parse(s) + return v isa Integer ? v : JSON.parse(s, BigFloat) end - return JSON.parse(x) + return JSON.parse(s) end """ diff --git a/examples/generated/Combined.jl b/examples/generated/Combined.jl index 102502a..59b83a4 100644 --- a/examples/generated/Combined.jl +++ b/examples/generated/Combined.jl @@ -460,7 +460,7 @@ function fromjson(::Type{T}, json::AbstractString) where {T} F = _jsfloattype(T) # numbers are parsed as `Float64` by JSON.jl, except when another floating-point type # is asked for: then they are parsed with full precision - return fromdict(T, F === Float64 ? JSON.parse(json) : _jsparsebig(JSON.lazy(json))) + return fromdict(T, F === Float64 ? JSON.parse(json) : _jsparsebig(json)) end _jsfloattype(::Type) = Float64 @@ -468,23 +468,26 @@ _jsfloattype(::Type{T}) where {T <: AbstractSchemaObject} = T isa DataType && length(T.parameters) == 2 ? T.parameters[2] : Float64 _jsfloattype(::Type{Vector{T}}) where {T} = _jsfloattype(T) -# Materialize a lazily parsed JSON value, parsing non-integer numbers as `BigFloat` (at the -# current precision of `BigFloat`), so that they can be converted to any real type. -function _jsparsebig(x) - t = JSON.gettype(x) - if t == JSON.JSONTypes.OBJECT +# Parse the JSON text `json`, parsing non-integer numbers as `BigFloat` (at the current +# precision of `BigFloat`), so that they can be converted to any real type. Objects and +# arrays are read one level at a time, keeping the raw text of their values (`JSONText`), +# which is then parsed in turn, so only the public API of JSON.jl is used. +function _jsparsebig(json::AbstractString) + s = lstrip(json) + c = isempty(s) ? ' ' : first(s) + if c == '{' obj = JSON.Object{String, Any}() - JSON.applyobject((k, v) -> (obj[String(k)] = _jsparsebig(v); nothing), x) + for (k, v) in JSON.parse(s, JSON.Object{String, JSON.JSONText}) + obj[k] = _jsparsebig(v.value) + end return obj - elseif t == JSON.JSONTypes.ARRAY - arr = Any[] - JSON.applyarray((i, v) -> (push!(arr, _jsparsebig(v)); nothing), x) - return arr - elseif t == JSON.JSONTypes.NUMBER - v = JSON.parse(x) - return v isa Integer ? v : JSON.parse(x, BigFloat) + elseif c == '[' + return Any[_jsparsebig(v.value) for v in JSON.parse(s, Vector{JSON.JSONText})] + elseif c == '-' || isdigit(c) + v = JSON.parse(s) + return v isa Integer ? v : JSON.parse(s, BigFloat) end - return JSON.parse(x) + return JSON.parse(s) end """ diff --git a/examples/generated/Enum.jl b/examples/generated/Enum.jl index d4609cf..0873747 100644 --- a/examples/generated/Enum.jl +++ b/examples/generated/Enum.jl @@ -460,7 +460,7 @@ function fromjson(::Type{T}, json::AbstractString) where {T} F = _jsfloattype(T) # numbers are parsed as `Float64` by JSON.jl, except when another floating-point type # is asked for: then they are parsed with full precision - return fromdict(T, F === Float64 ? JSON.parse(json) : _jsparsebig(JSON.lazy(json))) + return fromdict(T, F === Float64 ? JSON.parse(json) : _jsparsebig(json)) end _jsfloattype(::Type) = Float64 @@ -468,23 +468,26 @@ _jsfloattype(::Type{T}) where {T <: AbstractSchemaObject} = T isa DataType && length(T.parameters) == 2 ? T.parameters[2] : Float64 _jsfloattype(::Type{Vector{T}}) where {T} = _jsfloattype(T) -# Materialize a lazily parsed JSON value, parsing non-integer numbers as `BigFloat` (at the -# current precision of `BigFloat`), so that they can be converted to any real type. -function _jsparsebig(x) - t = JSON.gettype(x) - if t == JSON.JSONTypes.OBJECT +# Parse the JSON text `json`, parsing non-integer numbers as `BigFloat` (at the current +# precision of `BigFloat`), so that they can be converted to any real type. Objects and +# arrays are read one level at a time, keeping the raw text of their values (`JSONText`), +# which is then parsed in turn, so only the public API of JSON.jl is used. +function _jsparsebig(json::AbstractString) + s = lstrip(json) + c = isempty(s) ? ' ' : first(s) + if c == '{' obj = JSON.Object{String, Any}() - JSON.applyobject((k, v) -> (obj[String(k)] = _jsparsebig(v); nothing), x) + for (k, v) in JSON.parse(s, JSON.Object{String, JSON.JSONText}) + obj[k] = _jsparsebig(v.value) + end return obj - elseif t == JSON.JSONTypes.ARRAY - arr = Any[] - JSON.applyarray((i, v) -> (push!(arr, _jsparsebig(v)); nothing), x) - return arr - elseif t == JSON.JSONTypes.NUMBER - v = JSON.parse(x) - return v isa Integer ? v : JSON.parse(x, BigFloat) + elseif c == '[' + return Any[_jsparsebig(v.value) for v in JSON.parse(s, Vector{JSON.JSONText})] + elseif c == '-' || isdigit(c) + v = JSON.parse(s) + return v isa Integer ? v : JSON.parse(s, BigFloat) end - return JSON.parse(x) + return JSON.parse(s) end """ diff --git a/examples/generated/Order.jl b/examples/generated/Order.jl index 70b8327..a1901a6 100644 --- a/examples/generated/Order.jl +++ b/examples/generated/Order.jl @@ -459,7 +459,7 @@ function fromjson(::Type{T}, json::AbstractString) where {T} F = _jsfloattype(T) # numbers are parsed as `Float64` by JSON.jl, except when another floating-point type # is asked for: then they are parsed with full precision - return fromdict(T, F === Float64 ? JSON.parse(json) : _jsparsebig(JSON.lazy(json))) + return fromdict(T, F === Float64 ? JSON.parse(json) : _jsparsebig(json)) end _jsfloattype(::Type) = Float64 @@ -467,23 +467,26 @@ _jsfloattype(::Type{T}) where {T <: AbstractSchemaObject} = T isa DataType && length(T.parameters) == 2 ? T.parameters[2] : Float64 _jsfloattype(::Type{Vector{T}}) where {T} = _jsfloattype(T) -# Materialize a lazily parsed JSON value, parsing non-integer numbers as `BigFloat` (at the -# current precision of `BigFloat`), so that they can be converted to any real type. -function _jsparsebig(x) - t = JSON.gettype(x) - if t == JSON.JSONTypes.OBJECT +# Parse the JSON text `json`, parsing non-integer numbers as `BigFloat` (at the current +# precision of `BigFloat`), so that they can be converted to any real type. Objects and +# arrays are read one level at a time, keeping the raw text of their values (`JSONText`), +# which is then parsed in turn, so only the public API of JSON.jl is used. +function _jsparsebig(json::AbstractString) + s = lstrip(json) + c = isempty(s) ? ' ' : first(s) + if c == '{' obj = JSON.Object{String, Any}() - JSON.applyobject((k, v) -> (obj[String(k)] = _jsparsebig(v); nothing), x) + for (k, v) in JSON.parse(s, JSON.Object{String, JSON.JSONText}) + obj[k] = _jsparsebig(v.value) + end return obj - elseif t == JSON.JSONTypes.ARRAY - arr = Any[] - JSON.applyarray((i, v) -> (push!(arr, _jsparsebig(v)); nothing), x) - return arr - elseif t == JSON.JSONTypes.NUMBER - v = JSON.parse(x) - return v isa Integer ? v : JSON.parse(x, BigFloat) + elseif c == '[' + return Any[_jsparsebig(v.value) for v in JSON.parse(s, Vector{JSON.JSONText})] + elseif c == '-' || isdigit(c) + v = JSON.parse(s) + return v isa Integer ? v : JSON.parse(s, BigFloat) end - return JSON.parse(x) + return JSON.parse(s) end """ diff --git a/playground/read_schema.jl b/playground/read_schema.jl deleted file mode 100644 index 0217cf6..0000000 --- a/playground/read_schema.jl +++ /dev/null @@ -1,24 +0,0 @@ -using JSON3 -using HTTP - -json_string = read("../examples/beerjson-1.0.1/json/beer.json", String) -json_schema = JSON3.read(json_string) - -@show keys(json_schema) -for key in (Symbol("\$schema"), Symbol("\$id")) - if key in keys(json_schema) - println("$key: $(json_schema[key])") - end -end - -uri = "https://raw.githubusercontent.com/beerjson/beerjson/master/json/beer.json" -@assert isvalid(HTTP.URI(uri)) "Invalid URI" -json_string = String(HTTP.get(uri).body) -json_schema = JSON3.read(json_string) - -@show keys(json_schema) -for key in (Symbol("\$schema"), Symbol("\$id")) - if key in keys(json_schema) - println("$key: $(json_schema[key])") - end -end diff --git a/playground/uri_explorer.jl b/playground/uri_explorer.jl deleted file mode 100644 index b3bab0a..0000000 --- a/playground/uri_explorer.jl +++ /dev/null @@ -1,73 +0,0 @@ -# https://pretty-rfc.herokuapp.com/RFC3986#base-uri -# https://www.ietf.org/rfc/rfc3986.txt (official but not html, not friendly) -# -using HTTP - -u = HTTP.URI("http://a/b/c/d;p?q") -@show isvalid(u); - -T = typeof(u) - -for (name, typ) in zip(fieldnames(T), T.types) - println("type of the fieldname $name is $typ") -end - -for name in fieldnames(T) - println("field $name: $(getfield(u, name))") -end - -u = HTTP.URI("https://raw.githubusercontent.com/beerjson/beerjson/master/json/beer.json") -for name in fieldnames(T) - println("field $name: $(getfield(u, name))") -end - -u = HTTP.URI("https://raw.githubusercontent.com/beerjson/beerjson/master/json/measureable_units.json") -@show isvalid(u); -#r=HTTP.URI("#/definitions/VolumeUnitType") -#@show isvalid(r); -s = "https://raw.githubusercontent.com/beerjson/beerjson/master/json/measureable_units.json" * - "#/definitions/VolumeUnitType" -@show isvalid(s); - -path = "https://raw.githubusercontent.com/beerjson/beerjson/master/json/misc.json" -rel = "measureable_units.json#/definitions/VolumeType" -resolved = joinpath(dirname(path), rel) -ur = HTTP.URI(resolved) -@show isvalid(u); - -for name in fieldnames(T) - println("field $name: $(getfield(ur, name))") -end - -# Taken from JSONSchema.jl -# https://github.com/fredo-dedup/JSONSchema.jl/blob/master/src/schema.jl/#L13 -function type_to_dict(x) - return Dict(name => getfield(x, name) for name in fieldnames(typeof(x))) -end - -# Taken from JSONSchema.jl -# https://github.com/fredo-dedup/JSONSchema.jl/blob/master/src/schema.jl#L17 -function update_id(uri::HTTP.URI, s::String) - id2 = HTTP.URI(s) - if !isempty(id2.scheme) - return id2 - end - els = type_to_dict(uri) - delete!(els, :uri) - els[:fragment] = id2.fragment - if !isempty(id2.path) - oldpath = match(r"^(.*/).*$", uri.path) - els[:path] = oldpath == nothing ? id2.path : oldpath.captures[1] * id2.path - end - return HTTP.URI(; els...) -end - -u = HTTP.URI("https://raw.githubusercontent.com/beerjson/beerjson/master/json/beer.json") -rel = "measureable_units.json#/definitions/VolumeType" -update_id(u, rel) -isvalid(update_id(u, rel)) - -u = HTTP.URI("https://raw.githubusercontent.com/beerjson/beerjson/master/json/measureable_units.json") -rel = "#/definitions/VolumeUnitType" -update_id(u, rel) -isvalid(update_id(u, rel)) \ No newline at end of file diff --git a/src/runtime.jl b/src/runtime.jl index 5c10dea..9dfd451 100644 --- a/src/runtime.jl +++ b/src/runtime.jl @@ -443,7 +443,7 @@ function fromjson(::Type{T}, json::AbstractString) where {T} F = _jsfloattype(T) # numbers are parsed as `Float64` by JSON.jl, except when another floating-point type # is asked for: then they are parsed with full precision - return fromdict(T, F === Float64 ? JSON.parse(json) : _jsparsebig(JSON.lazy(json))) + return fromdict(T, F === Float64 ? JSON.parse(json) : _jsparsebig(json)) end _jsfloattype(::Type) = Float64 @@ -451,23 +451,26 @@ _jsfloattype(::Type{T}) where {T <: AbstractSchemaObject} = T isa DataType && length(T.parameters) == 2 ? T.parameters[2] : Float64 _jsfloattype(::Type{Vector{T}}) where {T} = _jsfloattype(T) -# Materialize a lazily parsed JSON value, parsing non-integer numbers as `BigFloat` (at the -# current precision of `BigFloat`), so that they can be converted to any real type. -function _jsparsebig(x) - t = JSON.gettype(x) - if t == JSON.JSONTypes.OBJECT +# Parse the JSON text `json`, parsing non-integer numbers as `BigFloat` (at the current +# precision of `BigFloat`), so that they can be converted to any real type. Objects and +# arrays are read one level at a time, keeping the raw text of their values (`JSONText`), +# which is then parsed in turn, so only the public API of JSON.jl is used. +function _jsparsebig(json::AbstractString) + s = lstrip(json) + c = isempty(s) ? ' ' : first(s) + if c == '{' obj = JSON.Object{String, Any}() - JSON.applyobject((k, v) -> (obj[String(k)] = _jsparsebig(v); nothing), x) + for (k, v) in JSON.parse(s, JSON.Object{String, JSON.JSONText}) + obj[k] = _jsparsebig(v.value) + end return obj - elseif t == JSON.JSONTypes.ARRAY - arr = Any[] - JSON.applyarray((i, v) -> (push!(arr, _jsparsebig(v)); nothing), x) - return arr - elseif t == JSON.JSONTypes.NUMBER - v = JSON.parse(x) - return v isa Integer ? v : JSON.parse(x, BigFloat) + elseif c == '[' + return Any[_jsparsebig(v.value) for v in JSON.parse(s, Vector{JSON.JSONText})] + elseif c == '-' || isdigit(c) + v = JSON.parse(s) + return v isa Integer ? v : JSON.parse(s, BigFloat) end - return JSON.parse(x) + return JSON.parse(s) end """