JSON

std/json encodes and decodes pyr values.

imp std/json { encode, decode }

struct Config {
  host: str
  port: int
  debug: bool
}

fn main() {

Encode any value to a JSON string.

  config = Config { host: "localhost", port: 8080, debug: true }
  json_str = encode(config)
  println(json_str)

Encode arrays and primitives.

  println(encode([1, 2, 3]))
  println(encode("hello"))

Decode JSON strings back to pyr values.

  data = decode("\{\"name\":\"alice\",\"age\":30\}")
  println(data.name)
  println(data.age)

Round-trip: decode(encode(v)) preserves structure.

  original = [10, 20, 30]
  roundtrip = decode(encode(original))
  println(roundtrip)
}
$ pyr run json.pyr
{"host":"localhost","port":8080,"debug":true}
[1,2,3]
"hello"
alice
30
[10, 20, 30]