Esc

Json_Any

structextensionsince 0.36.1Source
  • macOS: Json_Any works on macOS
  • Linux: Json_Any works on Linux
  • Windows: Json_Any works on Windows
  • Wasm: Json_Any works on Wasm

Declaration

Json_Any :: struct(T: Type) {
    run :: (out: *Json_Out, input: *Json_In, bytes: [] u8, result: *[] u8) -> bool {
        if out {
            value, error := decode(T, bytes);

            defer T.proto_free(*value);
            defer json_free_error(*error);

            if error.kind != .NONE return json_write_fail(out, .BAD_VALUE, "an Any holds bytes that don't decode as its type");

            first := false;

            return json_write_any_body(out, *value, *first);
        }

        value: T;
        saved := input.pr;

        input.pr = Reader.{ bytes = saved.bytes, pos = saved.pos, storage = *value.proto_storage };
        ok := json_read_any_body(input, *value);

        input.pr = saved;

        if ok {
            encoded, error := encode(value);

            if error.kind != .NONE {
                json_free_error(*error);
                ok = Json.json_fail(*input.r, .INVALID_VALUE, "an Any's message doesn't encode");
            } else result.* = encoded;
        }

        T.proto_free(*value);

        return ok;
    }
}

The JSON codec of one message type, for Any. Generated schemas list one per message in proto_registry.

See also