From 62a29f9cecfe3b6ec982fd61c536d9ab24353d90 Mon Sep 17 00:00:00 2001 From: Ian Date: Wed, 15 Apr 2020 15:32:00 -0400 Subject: [PATCH 1/3] print any array of length > 5 without newlines --- src/Writer.jl | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/Writer.jl b/src/Writer.jl index 6ebea04..c7472d5 100644 --- a/src/Writer.jl +++ b/src/Writer.jl @@ -129,18 +129,18 @@ write(io::StringContext, char::Char) = =# """ - indent(io::StructuralContext) + indent(io::StructuralContext; newline = true) If appropriate, write a newline to the given context, then indent it by the appropriate number of spaces. Otherwise, do nothing. """ -@inline function indent(io::PrettyContext) - write(io, NEWLINE) +@inline function indent(io::PrettyContext; newline = true) + newline && write(io, NEWLINE) for _ in 1:io.state write(io, SPACE) end end -@inline indent(io::CompactContext) = nothing +@inline indent(io::CompactContext; newline = true) = nothing """ separate(io::StructuralContext) @@ -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) + indent(io, newline = newline) show_json(io, s, x) end @@ -289,8 +289,9 @@ end function show_json(io::SC, s::CS, x::Union{AbstractVector, Tuple}) begin_array(io) + do_newlines = length(x) <= 5 for elt in x - show_element(io, s, elt) + show_element(io, s, elt, newline = do_newlines) end end_array(io) end From e1234e0312b51c1ab2a082c4b165a339991f7e78 Mon Sep 17 00:00:00 2001 From: Ian Date: Wed, 15 Apr 2020 15:55:38 -0400 Subject: [PATCH 2/3] simplify --- src/Writer.jl | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/src/Writer.jl b/src/Writer.jl index c7472d5..777e144 100644 --- a/src/Writer.jl +++ b/src/Writer.jl @@ -129,18 +129,18 @@ write(io::StringContext, char::Char) = =# """ - indent(io::StructuralContext; newline = true) + indent(io::StructuralContext) If appropriate, write a newline to the given context, then indent it by the appropriate number of spaces. Otherwise, do nothing. """ -@inline function indent(io::PrettyContext; newline = true) - newline && write(io, NEWLINE) +@inline function indent(io::PrettyContext) + write(io, NEWLINE) for _ in 1:io.state write(io, SPACE) end end -@inline indent(io::CompactContext; newline = true) = nothing +@inline indent(io::CompactContext) = nothing """ separate(io::StructuralContext) @@ -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 """ @@ -214,7 +214,7 @@ defined by serialization `s`. """ function show_element(io::JSONContext, s, x; newline = true) delimit(io) - indent(io, newline = newline) + newline && indent(io) show_json(io, s, x) end @@ -287,13 +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}; do_newlines = length(x) <= 5) begin_array(io) - do_newlines = length(x) <= 5 for elt in x show_element(io, s, elt, newline = do_newlines) end - end_array(io) + end_array(io, newline = do_newlines) end """ From 191b7275dae34c08802a80158cb8be3644eece76 Mon Sep 17 00:00:00 2001 From: Ian Date: Wed, 15 Apr 2020 19:39:19 -0400 Subject: [PATCH 3/3] add newline as kwarg to array print methods --- src/Writer.jl | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/src/Writer.jl b/src/Writer.jl index 777e144..2ed3f93 100644 --- a/src/Writer.jl +++ b/src/Writer.jl @@ -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}; do_newlines = length(x) <= 5) +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, newline = do_newlines) + show_element(io, s, elt, newline = newline) end - end_array(io, newline = do_newlines) + end_array(io, newline = newline) end """ @@ -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) @@ -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)