Json module

json_encode

procedureextensionSource

Writes the JSON of the value value points to to the writer w. This and json_decode are what with= handlers call for the parts they do not encode themselves.

Example↗
Price :: struct {
    cents: s64; @json(with=dollars)

    dollars_encode :: (w: *Json_Writer, value: *s64) {
        json_write_float64(w, cast(float64) value.* / 100.0);
    }

    dollars_decode :: (r: *Json_Reader, out: *s64) -> bool {
        dollars: float64;
        if !json_read_float64(r, *dollars) return false;
        out.* = cast(s64) (dollars * 100.0 + 0.5);
        return true;
    }
}

assert(to_json(Price.{ 1250 }) == "{\"cents\":12.5}");
price, _ := from_json(Price, "{\"cents\": 3.25}");
assert(price.cents == 325);
json_encode :: (w: *Json_Writer, value: *$T)

Parameters

value: *$T

See also