@@ -8,23 +8,51 @@ macro pretty(json)
88end
99
1010"""
11- JSON3.pretty(x; kw...)
12- JSON3.pretty(io, x; kw...)
11+ JSON3.AlignmentContext(alignment=:Left, indent=4, level=0, offset=0)
12+
13+ Specifies the indentation of a pretty JSON string.
14+
15+ ## Keyword Args
16+ * `alignment`: A Symbol specifying the alignment type. Can be `:Left` to left-align
17+ everything or `:Colon` to align at the `:`.
18+ * `indent`: The number of spaces to indent each new level with.
19+ * `level`: The indentation level.
20+ * `offset`: The indentation offset.
21+ """
22+ Base. @kwdef mutable struct AlignmentContext
23+ alignment:: Symbol = :Left
24+ indent:: UInt16 = 4
25+ level:: UInt16 = 0
26+ offset:: UInt16 = 0
27+ function AlignmentContext (alignment, indent, level, offset)
28+ if alignment != :Left && alignment != :Colon
29+ throw (ArgumentError (" Alignment :$(alignment) is not supported. " *
30+ " Only `:Left` and `:Colon` are supported so far" ))
31+ end
32+ new (alignment, indent, level, offset)
33+ end
34+ end
35+
36+ """
37+ JSON3.pretty(x, ac=JSON3.AlignmentContext(); kw...)
38+ JSON3.pretty(io, x, ac=JSON3.AlignmentContext(); kw...)
1339
1440Pretty print a JSON string.
1541
1642## Args
1743
1844* `x`: A JSON string, or an object to write to JSON then pretty print.
1945* `io`: The `IO` object to write the pretty printed string to. [default `stdout`]
46+ * `ac`: The `AlignmentContext` for the pretty printing. Defaults to left-aligned
47+ with 4 spaces indent. See [`JSON3.AlignmentContext`](@ref) for more options.
2048
2149## Keyword Args
2250
2351See [`JSON3.write`](@ref) and [`JSON3.read`](@ref).
2452"""
25- pretty (str; kw... ) = pretty (stdout , str; kw... )
26- pretty (out:: IO , x; kw... ) = pretty (out, JSON3. write (x; kw... ); kw... )
27- function pretty (out:: IO , str:: String , indent = 0 , offset = 0 ; kw... )
53+ pretty (str, ac = AlignmentContext () ; kw... ) = pretty (stdout , str, ac ; kw... )
54+ pretty (out:: IO , x, ac = AlignmentContext () ; kw... ) = pretty (out, JSON3. write (x; kw... ), ac ; kw... )
55+ function pretty (out:: IO , str:: String , ac = AlignmentContext () ; kw... )
2856 buf = codeunits (str)
2957 len = length (buf)
3058 if len == 0
@@ -40,22 +68,24 @@ function pretty(out::IO, str::String, indent=0, offset=0; kw...)
4068 obj = JSON3. read (str; kw... )
4169
4270 if length (obj) == 0
43- Base. write (out, " }" )
71+ Base. write (out, ' ' ^ (ac . indent * ac . level + ac . offset) * " }" )
4472 return
4573 end
4674
4775 ks = collect (keys (obj))
4876 maxlen = maximum (map (sizeof, ks)) + 5
49- indent += 1
77+ ac. alignment == :Colon && (ac. offset += maxlen)
78+ ac. level += 1
5079
5180 i = 1
5281 for (key, value) in obj
53- Base. write (out, " " ^ indent)
54- Base. write (out, lpad (" \" $(key) \" " * " : " , maxlen + offset, ' ' ))
55- pretty (out, JSON3. write (value; kw... ), indent, maxlen + offset ; kw... )
82+ Base. write (out, ' ' ^ (ac . level * ac . indent) )
83+ Base. write (out, lpad (" \" $(key) \" " * " : " , ac . offset, ' ' ))
84+ pretty (out, JSON3. write (value; kw... ), ac ; kw... )
5685 if i == length (obj)
57- indent -= 1
58- Base. write (out, " \n " * (" " ^ indent * " " ^ offset) * " }" )
86+ ac. level -= 1
87+ ac. alignment == :Colon && (ac. offset -= maxlen)
88+ Base. write (out, " \n " * ' ' ^ (ac. indent * ac. level + ac. offset) * " }" )
5989 else
6090 Base. write (out, " ,\n " )
6191 i += 1
@@ -68,18 +98,18 @@ function pretty(out::IO, str::String, indent=0, offset=0; kw...)
6898 arr = JSON3. read (str; kw... )
6999
70100 if length (arr) == 0
71- Base. write (out, " ]" )
101+ Base. write (out, ' ' ^ (ac . indent * ac . level + ac . offset) * " ]" )
72102 return
73103 end
74104
75- indent += 1
105+ ac . level += 1
76106
77107 for (i, val) in enumerate (arr)
78- Base. write (out, " " ^ indent * " " ^ offset)
79- pretty (out, JSON3. write (val; kw... ), indent, offset ; kw... )
108+ Base. write (out, ' ' ^ (ac . indent * ac . level + ac . offset) )
109+ pretty (out, JSON3. write (val; kw... ), ac ; kw... )
80110 if i == length (arr)
81- indent -= 1
82- Base. write (out, " \n " * ( " " ^ indent * " " ^ offset) * " ]" )
111+ ac . level -= 1
112+ Base. write (out, " \n " * ' ' ^ (ac . indent * ac . level + ac . offset) * " ]" )
83113 else
84114 Base. write (out, " ,\n " )
85115 end
0 commit comments