diff --git a/src/read.jl b/src/read.jl index 50354d4..f36b264 100644 --- a/src/read.jl +++ b/src/read.jl @@ -29,8 +29,11 @@ Read JSON. """ function read(json::AbstractString; jsonlines::Bool=false, numbertype::Union{DataType, Nothing}=nothing, kw...) + return parse(read_json_str(json); jsonlines, numbertype, kw...) +end - str = read_json_str(json) +function parse(str::AbstractString; jsonlines::Bool=false, + numbertype::Union{DataType, Nothing}=nothing, kw...) buf = codeunits(str) len = length(buf) if len == 0 @@ -72,6 +75,11 @@ function read(json::AbstractString; jsonlines::Bool=false, invalid(error, buf, pos, Any) end +function parsefile(fname::AbstractString; jsonlines::Bool=false, + numbertype::Union{DataType, Nothing}=nothing, kw...) + return parse(VectorString(Mmap.mmap(fname)); jsonlines, numbertype, kw...) +end + macro check() esc(quote if (tapeidx + 1) > length(tape) diff --git a/src/structs.jl b/src/structs.jl index 9a42ff2..3fff1c3 100644 --- a/src/structs.jl +++ b/src/structs.jl @@ -13,8 +13,7 @@ function rawbytes end read(io::Union{IO, Base.AbstractCmd}, ::Type{T}; kw...) where {T} = read(Base.read(io, String), T; kw...) read(bytes::AbstractVector{UInt8}, ::Type{T}; kw...) where {T} = read(VectorString(bytes), T; kw...) -function _prepare_read(json::AbstractString, ::Type{T}) where {T} - str = read_json_str(json) +function _prepare_read(str::AbstractString, ::Type{T}) where {T} buf = codeunits(str) len = length(buf) if len == 0 @@ -30,7 +29,7 @@ function _prepare_read(json::AbstractString, ::Type{T}) where {T} invalid(error, buf, pos, T) end -function read(str::AbstractString, ::Type{T}; jsonlines::Bool=false, kw...) where {T} +function parse(str::AbstractString, ::Type{T}; jsonlines::Bool=false, kw...) where {T} buf, pos, len, b = _prepare_read(str, T) if jsonlines if StructType(T) != ArrayType() @@ -43,13 +42,21 @@ function read(str::AbstractString, ::Type{T}; jsonlines::Bool=false, kw...) wher return x end +function read(str::AbstractString, ::Type{T}; jsonlines::Bool=false, kw...) where {T} + return parse(read_json_str(str), T; jsonlines, kw...) +end + +function parsefile(fname::AbstractString, ::Type{T}; jsonlines::Bool=false, kw...) where {T} + return parse(VectorString(Mmap.mmap(fname)), T; jsonlines, kw...) +end + """ JSON3.read!(json_str, x; kw...) Incrementally update an instance of a mutable object `x` with the contents of `json_str`. See [`JSON3.read`](@ref) for more details. """ function read!(str::AbstractString, x::T; kw...) where {T} - buf, pos, len, b = _prepare_read(str, T) + buf, pos, len, b = _prepare_read(read_json_str(str), T) pos, x = read!(StructType(T), buf, pos, len, b, T, x; kw...) return x end diff --git a/test/runtests.jl b/test/runtests.jl index a036012..f26a9a1 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -167,18 +167,20 @@ StructTypes.StructType(::Type{UndefGuy}) = StructTypes.Mutable() @testset "read.jl" begin -@test_throws ArgumentError JSON3.read("") -@test JSON3.read("{\"hey\":1}").hey == 1 -@test JSON3.read("[\"hey\",1]") == ["hey",1] -@test JSON3.read("1.0") === Int64(1) -@test JSON3.read("1") === Int64(1) -@test JSON3.read("1.1") === 1.1 -@test JSON3.read("+1.1") === 1.1 -@test JSON3.read("-1.1") === -1.1 -@test JSON3.read("\"hey\"") == "hey" -@test JSON3.read("null") === nothing -@test JSON3.read("true") === true -@test JSON3.read("false") === false +for readfunc in [JSON3.read, JSON3.parse] + @test_throws ArgumentError readfunc("") + @test readfunc("{\"hey\":1}").hey == 1 + @test readfunc("[\"hey\",1]") == ["hey",1] + @test readfunc("1.0") === Int64(1) + @test readfunc("1") === Int64(1) + @test readfunc("1.1") === 1.1 + @test readfunc("+1.1") === 1.1 + @test readfunc("-1.1") === -1.1 + @test readfunc("\"hey\"") == "hey" + @test readfunc("null") === nothing + @test readfunc("true") === true + @test readfunc("false") === false +end # numbertype @test JSON3.read("1.0", numbertype=Float64) === 1.0