/Prelude-v14.0.0/JSON/render

Copy path to clipboard

Examples

  render
( JSON.array
[ JSON.bool True
, JSON.string "Hello"
, JSON.object
[ { mapKey = "foo", mapValue = JSON.null }
, { mapKey = "bar", mapValue = JSON.double 1.0 }
]
]
)
≡ "[ true, \"Hello\", { \"foo\": null, \"bar\": 1.0 } ]"

Source

{- Render a `JSON` value as `Text`

This is useful for debugging `JSON` values or for tests. For anything
more sophisticated you should use `dhall-to-json` or `dhall-to-yaml`
-}
let JSON =
./core.dhall sha256:5dc1135d5481cfd6fde625aaed9fcbdb7aa7c14f2e76726aa5fdef028a5c10f5
? ./core.dhall

let Text/concatMapSep =
../Text/concatMapSep sha256:c272aca80a607bc5963d1fcb38819e7e0d3e72ac4d02b1183b1afb6a91340840
? ../Text/concatMapSep

let render
: JSON.Type → Text
= λ(j : JSON.Type)
j
Text
{ string = λ(x : Text) → Text/show x
, double = λ(x : Double) → Double/show x
, integer = λ(x : Integer) → JSON.renderInteger x
, 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 example0 =
assert
: render
( JSON.array
[ JSON.bool True
, JSON.string "Hello"
, JSON.object
[ { mapKey = "foo", mapValue = JSON.null }
, { mapKey = "bar", mapValue = JSON.double 1.0 }
]
]
)
≡ "[ true, \"Hello\", { \"foo\": null, \"bar\": 1.0 } ]"

in render