/// Create a `{{ type_name }}` from a JSON string. Returns null on failure.
/// # Safety
/// JSON string must be valid UTF-8 and null-terminated.
/// Returned handle must be freed with `{{ prefix }}_{{ type_snake }}_free`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn {{ prefix }}_{{ type_snake }}_from_json(json: *const c_char) -> *mut {{ qualified }} {
    clear_last_error();
    if json.is_null() {
        set_last_error(1, "Null pointer passed for JSON string");
        return std::ptr::null_mut();
    }
    // SAFETY: null check above guarantees json is a valid pointer; string is valid UTF-8 from caller.
    let c_str = match unsafe { CStr::from_ptr(json) }.to_str() {
        Ok(s) => s,
        Err(_) => {
            set_last_error(1, "Invalid UTF-8 in JSON string");
            return std::ptr::null_mut();
        }
    };
    match serde_json::from_str::<{{ qualified }}>(c_str) {
        Ok(val) => Box::into_raw(Box::new(val)),
        Err(e) => {
            set_last_error(2, &e.to_string());
            std::ptr::null_mut()
        }
    }
}
