Created
March 12, 2024 16:35
-
-
Save erikcorry/a8742dd560b5a1c758d800b1deef6a71 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// Simple pretty-printer for JSON-compatible objects (maps, lists, strings, | |
/// numbers, booleans, and null). | |
pretty-print data --indent/string="" --prefix/string?=null --suffix/string?="" -> none: | |
str := json.stringify data | |
if str.size < 80: | |
print "$(prefix or indent)$str$suffix" | |
return | |
if data is Map: | |
print "$(prefix or indent){" | |
i := 0 | |
data.do: | key value | | |
suffix2 := i == data.size - 1 ? null : "," | |
pretty-print value --indent="$indent " --prefix="$indent \"$key\": " --suffix=suffix2 | |
i++ | |
print "$indent}$suffix" | |
else if data is List: | |
print "$(prefix or indent)[" | |
for i := 0; i < data.size; i++: | |
value := data[i] | |
suffix2 := i == data.size - 1 ? null : "," | |
pretty-print value --indent="$indent " --suffix=suffix2 | |
print "$indent]$suffix" | |
else: | |
print "$(prefix or indent)$str$suffix" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment