diff --git a/src/JSON3.jl b/src/JSON3.jl index f85527f..94b443f 100644 --- a/src/JSON3.jl +++ b/src/JSON3.jl @@ -12,6 +12,10 @@ end Object() = Object(codeunits(""), UInt64[object(Int64(2)), 0], Dict{Symbol, Int}()) +# show(::IO, ::MIME"text/plain", ::AbstractDict) gets used at the top level +# This method below is used for nested objects +Base.show(io::IO, obj::Object) = JSON3.write(io, obj) + """An immutable (read only) struct which provides an efficient view of a JSON array. Supports the `AbstractArray` interface. See [built in types](#Builtin-types) for more detail on why we have an `Array` type.""" struct Array{T, S <: AbstractVector{UInt8}, TT <: AbstractVector{UInt64}} <: AbstractVector{T} buf::S @@ -167,7 +171,6 @@ Base.copy(arr::Array) = map(x->x isa Object || x isa Array ? copy(x) : x, arr) include("read.jl") include("strings.jl") -include("show.jl") include("structs.jl") include("write.jl") include("pretty.jl") diff --git a/src/show.jl b/src/show.jl deleted file mode 100644 index 1f5c218..0000000 --- a/src/show.jl +++ /dev/null @@ -1,51 +0,0 @@ -Base.show(io::IO, j::Object) = _show(io, j) -# use the Base fallback AbstractArray show method instead -# Base.show(io::IO, j::Array) = _show(io, j) -_show(io::IO, x, indent=0, offset=0) = show(io, x) - -function _show(io::IO, obj::Object, indent=0, offset=0) - if isempty(obj) - print(io, "{}") - return - end - println(io, "{") - indent += 1 - keyvals = collect(obj) - keys = map(x->x[1], keyvals) - vals = map(x->x[2], keyvals) - maxlen = maximum(map(sizeof, keys)) + 5 - # @show maxlen - for i = 1:length(keys) - Base.write(io, " "^indent) - Base.write(io, lpad("\"$(keys[i])\"" * ": ", maxlen + offset, ' ')) - _show(io, vals[i], indent, maxlen + offset) - if i == length(keys) - indent -= 1 - Base.write(io, "\n" * (" "^indent * " "^offset) * "}") - else - Base.write(io, ",\n") - end - end - return -end - -function _show(io::IO, arr::Array, indent=0, offset=0) - if isempty(arr) - print(io, "[]") - return - end - println(io, "[") - indent += 1 - vals = collect(arr) - for (i, val) in enumerate(vals) - Base.write(io, " "^indent * " "^offset) - _show(io, vals[i], indent, offset) - if i == length(vals) - indent -= 1 - Base.write(io, "\n" * (" "^indent * " "^offset) * "]") - else - Base.write(io, ",\n") - end - end - return -end diff --git a/test/runtests.jl b/test/runtests.jl index a036012..85b0c37 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -809,16 +809,14 @@ json = JSON3.write(w) end # @testset "structs.jl" -@testset "show.jl" begin +@testset "repr" begin @test repr(JSON3.read("{}")) == "{}" -@test repr(JSON3.read("{\"a\": 1}")) == "{\n \"a\": 1\n}" -@test repr(JSON3.read("{\"a\": {\"b\": 2}}")) == "{\n \"a\": {\n \"b\": 2\n }\n}" -# @test repr(JSON3.read("[]")) == "[]" -# @test repr(JSON3.read("[1,2,3]")) == "[\n 1,\n 2,\n 3\n]" -# @test repr(JSON3.read("[1,[2.1,2.2,2.3],3]")) == "[\n 1,\n [\n 2.1,\n 2.2,\n 2.3\n ],\n 3\n]" +@test repr(JSON3.read("{\"a\": 1}")) == "{\"a\":1}" +@test repr(JSON3.read("{\"a\": {\"b\": 2}}")) == "{\"a\":{\"b\":2}}" +@test repr(JSON3.read("[1,2,3]")) == "[1, 2, 3]" -end # @testset "show.jl" +end # @testset "repr" include("json.jl")