Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Print longer arrays without newlines #305

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 22 additions & 8 deletions src/Writer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -176,15 +176,15 @@ for kind in ("object", "array")
io.first = true
end
@eval $beginfn(io::CompactContext) = (write(io, $beginsym); io.first = true)
@eval function $endfn(io::PrettyContext)
@eval function $endfn(io::PrettyContext; newline = true)
io.state -= io.step
if !io.first
if newline && !io.first
indent(io)
end
write(io, $endsym)
io.first = false
end
@eval $endfn(io::CompactContext) = (write(io, $endsym); io.first = false)
@eval $endfn(io::CompactContext; newline = true) = (write(io, $endsym); io.first = false)
end

"""
Expand Down Expand Up @@ -212,9 +212,9 @@ show_null(io::IO) = Base.print(io, "null")
Print object `x` as an element of a JSON array to context `io` using rules
defined by serialization `s`.
"""
function show_element(io::JSONContext, s, x)
function show_element(io::JSONContext, s, x; newline = true)
delimit(io)
indent(io)
newline && indent(io)
show_json(io, s, x)
end

Expand Down Expand Up @@ -287,12 +287,12 @@ function show_json(io::SC, s::CS, x::CompositeTypeWrapper)
end_object(io)
end

function show_json(io::SC, s::CS, x::Union{AbstractVector, Tuple})
function show_json(io::SC, s::CS, x::Union{AbstractVector, Tuple}; newline::Bool = true)
begin_array(io)
for elt in x
show_element(io, s, elt)
show_element(io, s, elt, newline = newline)
end
end_array(io)
end_array(io, newline = newline)
end

"""
Expand Down Expand Up @@ -327,6 +327,14 @@ function show_json(io::IO, s::Serialization, obj; indent=nothing)
println(io)
end
end
function show_json(io::IO, s::Serialization, obj::Union{AbstractVector, Tuple}; indent=nothing, newline=true)
ctx = indent === nothing ? CompactContext(io) : PrettyContext(io, indent)
show_json(ctx, s, obj, newline=newline)
if indent !== nothing
println(io)
end
end


"""
JSONText(s::AbstractString)
Expand All @@ -348,6 +356,12 @@ print(io::IO, obj, indent) =
show_json(io, StandardSerialization(), obj; indent=indent)
print(io::IO, obj) = show_json(io, StandardSerialization(), obj)

print(io::IO, obj::Union{AbstractVector, Tuple}, indent; newline::Bool = length(obj) <= 5) =
show_json(io, StandardSerialization(), obj; indent=indent, newline=newline)
print(io::IO, obj::Union{AbstractVector, Tuple}; newline::Bool = length(obj) <= 5) =
show_json(io, StandardSerialization(), obj, newline=newline)


print(a, indent) = print(stdout, a, indent)
print(a) = print(stdout, a)

Expand Down