Home / Docs / Guides

JSON

Release v0.4.0

parse_json, to_json and quote_json read and write JSON (spec/23, edition 0.2).

una
module config

--- The port of a configuration, or 8080 when it is missing or not a number.
pub port(text: internal Text) -> internal Int
=
  let doc = text.parse_json()
    if error use Null
  when doc
    is Object(fields) ->
      let value = fields.get(key: "port")
        if none use Null
      when value
        is Number(n) -> n
        else -> 8080
    else -> 8080

--- The configuration written back, with the port replaced.
pub with_port(fields: Map[Text, Json], port: Int) -> Text = Object(fields.with(key: "port", value: Number(port))).to_json()