def pretty.go

emerging-ezjson@1.3.0.0 · src/print.bend:104

import emerging-ezjson@1.3.0.0/src/print.bend as Print
import Base
import emerging-ezjson@1.3.0.0/src/value.bend as V

104# a value as indented JSON. `pad` is the spaces a value's lines start with,
105# after their newline. For a chain of cells, a newline and `pad` start each
106# element or member, and `close` follows the last one, before the bracket
107def pretty.go(val: V.Json, +pad: String, +close: String) -> String:
108  match val:
109    case V.JNull{}:
110      "null"
111    case V.JBool{True{}}:
112      "true"
113    case V.JBool{False{}}:
114      "false"
115    case V.JNum{raw}:
116      raw
117    case V.JStr{s}:
118      quote(s)
119    case V.JSpan{src, +n}:
120      quote(V.span.str(src, n, U32.is_zero(n)))
121    case V.JArr{V.JNil{}}:
122      "[]"
123    case V.JArr{items}:
124      +inner = "  " ++ pad
125      "[\n" ++ inner ++ pretty.go(items, inner, "\n" ++ pad) ++ "]"
126    case V.JObj{V.JNil{}}:
127      "{}"
128    case V.JObj{pairs}:
129      +inner = "  " ++ pad
130      "{\n" ++ inner ++ pretty.go(pairs, inner, "\n" ++ pad) ++ "}"
131    case V.JNil{}:
132      ""
133    case V.JCons{h, V.JNil{}}:
134      pretty.go(h, pad, "") ++ close
135    case V.JCons{h, t}:
136      pretty.go(h, pad, "") ++ ",\n" ++ pad ++ pretty.go(t, pad, close)
137    case V.JPair{k, v, V.JNil{}}:
138      quote(k) ++ ": " ++ pretty.go(v, pad, "") ++ close
139    case V.JPair{k, v, r}:
140      quote(k) ++ ": " ++ pretty.go(v, pad, "") ++ ",\n" ++ pad ++ pretty.go(r, pad, close)
141    case V.JBind{src, +n, v, V.JNil{}}:
142      quote(V.span.str(src, n, U32.is_zero(n))) ++ ": " ++ pretty.go(v, pad, "") ++ close
143    case V.JBind{src, +n, v, r}:
144      quote(V.span.str(src, n, U32.is_zero(n))) ++ ": " ++ pretty.go(v, pad, "") ++ ",\n" ++ pad ++
145        pretty.go(r, pad, close)

Uses (7)

Used by (1)