Skip to content

Encoding with ard/encode

The ard/encode module provides a focused JSON encoder for primitive values. It exposes a small Encodable trait implemented only by the core primitives.

use ard/encode
use ard/io
fn main() {
io::print(encode::json("hello").expect("")) // "hello"
io::print(encode::json(42).expect("")) // 42
io::print(encode::json(3.14).expect("")) // 3.14
io::print(encode::json(true).expect("")) // true
}

The trait required by the encoder. It is implemented only by the primitive types.

trait Encodable {
fn to_dyn() Dynamic
}

Encode a primitive value as a JSON string. Returns Ok with the JSON string or Err with an error message.

use ard/encode
let json = encode::json(200).expect("Failed to encode")