dhall-nix-lib/JSON/renderCompact.dhall
2022-09-02 10:03:27 +01:00

52 lines
1.6 KiB
Text

--| This renders JSON on a single line
let JSON = ./core.dhall
let Text/concatMapSep = ../Text/concatMapSep.dhall
let renderInteger = ./renderInteger.dhall
let renderCompact
: JSON.Type → Text
= λ(j : JSON.Type) →
j
Text
{ string = Text/show
, double = Double/show
, integer = renderInteger
, object =
λ(x : List { mapKey : Text, mapValue : Text }) →
let body =
Text/concatMapSep
","
{ mapKey : Text, mapValue : Text }
( λ(e : { mapKey : Text, mapValue : Text }) →
" ${Text/show e.mapKey}: ${e.mapValue}"
)
x
in "{${body} }"
, array =
λ(x : List Text) →
let body = Text/concatMapSep "," Text (λ(y : Text) → " ${y}") x
in "[${body} ]"
, bool = λ(x : Bool) → if x then "true" else "false"
, null = "null"
}
let example =
assert
: renderCompact
( JSON.array
[ JSON.bool True
, JSON.string "Hello"
, JSON.object
[ { mapKey = "foo", mapValue = JSON.null }
, { mapKey = "bar", mapValue = JSON.double 1.1 }
, { mapKey = "baz", mapValue = JSON.integer +2 }
]
]
)
≡ "[ true, \"Hello\", { \"foo\": null, \"bar\": 1.1, \"baz\": 2 } ]"
in renderCompact