Skip to content

Commit

Permalink
improve show for nested Objects
Browse files Browse the repository at this point in the history
  • Loading branch information
joshday committed Dec 8, 2024
1 parent 02e2ecf commit 2018843
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 55 deletions.
20 changes: 19 additions & 1 deletion src/JSON3.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,25 @@ end

Object() = Object(codeunits(""), UInt64[object(Int64(2)), 0], Dict{Symbol, Int}())

# show(::IO, ::MIME"text/plain", ::AbstractDict) gets used at the top level
# The method below is used for nested objects
function Base.show(io::IO, obj::Object)
isempty(obj) && return print(io, "{}")
print(io, "{ ")
for (i, (k, v)) in enumerate(obj)
i == 1 || print(io, ", ")
print(io, repr(k), "=>")
if v isa Object
print(io, isempty(v) ? "{}" : "{…}")
elseif v isa Array
print(io, isempty(v) ? "[]" : "[…]")
else
show(io, v)
end
end
print(io, " }")
end

"""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
Expand Down Expand Up @@ -167,7 +186,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")
Expand Down
51 changes: 0 additions & 51 deletions src/show.jl

This file was deleted.

6 changes: 3 additions & 3 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -811,9 +811,9 @@ end # @testset "structs.jl"

@testset "show.jl" 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("{\"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]"
Expand Down

0 comments on commit 2018843

Please sign in to comment.