/// Serialize a heap-allocated `{{ enum_name }}` to a JSON string.
/// # Safety
/// `ptr` must be a valid, non-null pointer returned by a `{{ prefix }}` function.
/// The returned string must be freed with `{{ prefix }}_free_string`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn {{ prefix }}_{{ enum_snake }}_to_json(ptr: *const {{ qualified }}) -> *mut c_char {
    if ptr.is_null() {
        set_last_error(1, "Null pointer passed to {{ prefix }}_{{ enum_snake }}_to_json");
        return std::ptr::null_mut();
    }
    // SAFETY: null check above guarantees ptr is valid; no mutable aliases held.
    let val = unsafe { &*ptr };
    match serde_json::to_string(val) {
        Ok(s) => match CString::new(s) {
            Ok(cs) => cs.into_raw(),
            Err(_) => std::ptr::null_mut(),
        },
        Err(e) => {
            set_last_error(2, &e.to_string());
            std::ptr::null_mut()
        }
    }
}
