func encodeVisitResult(r {{ result_type }}, outResult **C.char) C.int32_t {
	// Encode the result as serde-native JSON so the Rust trait bridge's
	// serde_json deserialiser can decode it correctly.
	var jsonStr string
	switch r.Code {
{% for variant in result_variants %}
	case {{ variant.code }}:
{% if variant.has_payload %}
		if r.Value != nil {
			b, err := json.Marshal(*r.Value)
			if err != nil {
				b = []byte(`""`)
			}
			jsonStr = `{"{{ variant.wire_name }}":` + string(b) + `}`
		} else {
			jsonStr = `{"{{ variant.wire_name }}":""}`
		}
{% else %}
		jsonStr = `"{{ variant.wire_name }}"`
{% endif %}
{% endfor %}
	default:
		jsonStr = `"{{ default_result_wire_name }}"`
	}
	*outResult = C.CString(jsonStr)
	return C.int32_t(r.Code)
}
