yyjson.rs


Overview

The [yyjson.rs](/projects/287/67743) file provides Rust FFI (Foreign Function Interface) bindings and related data structures for interacting with the underlying `yyjson` C library, a high-performance JSON parser and writer. The primary purpose of this file is to define the low-level Rust representations of core `yyjson` types and provide access to crucial functions for JSON reading and memory management via unsafe extern C calls.

This file exposes:

The functionality in this file enables Rust code to utilize `yyjson`'s fast parsing capabilities with control over memory allocation, error handling, and document lifecycle management.


Detailed Documentation

Structs

yyjson_alc

#[repr(C)]
pub(crate) struct yyjson_alc {
    pub malloc: Option<unsafe extern "C" fn(ctx: *mut c_void, size: usize) -> *mut c_void>,
    pub realloc: Option<unsafe extern "C" fn(ctx: *mut c_void, ptr: *mut c_void, size: usize) -> *mut c_void>,
    pub free: Option<unsafe extern "C" fn(ctx: *mut c_void, ptr: *mut c_void)>,
    pub ctx: *mut c_void,
}
extern "C" fn my_malloc(ctx: *mut c_void, size: usize) -> *mut c_void {
    // Custom allocation logic here
}

extern "C" fn my_free(ctx: *mut c_void, ptr: *mut c_void) {
    // Custom free logic here
}

let alc = yyjson_alc {
    malloc: Some(my_malloc),
    realloc: None,
    free: Some(my_free),
    ctx: std::ptr::null_mut(),
};

yyjson_read_code

pub(crate) type yyjson_read_code = u32;
pub(crate) const YYJSON_READ_SUCCESS: yyjson_read_code = 0;

yyjson_read_err

#[repr(C)]
pub(crate) struct yyjson_read_err {
    pub code: yyjson_read_code,
    pub msg: *const c_char,
    pub pos: usize,
}

yyjson_val_uni (Union)

#[repr(C)]
pub(crate) union yyjson_val_uni {
    pub u64_: u64,
    pub i64_: i64,
    pub f64_: f64,
    pub str_: *const c_char,
    pub ptr: *mut c_void,
    pub ofs: usize,
}

yyjson_val

#[repr(C)]
pub(crate) struct yyjson_val {
    pub tag: u64,
    pub uni: yyjson_val_uni,
}

yyjson_doc

#[repr(C)]
pub(crate) struct yyjson_doc {
    pub root: *mut yyjson_val,
    pub alc: yyjson_alc,
    pub dat_read: usize,
    pub val_read: usize,
    pub str_pool: *mut c_char,
}

Functions (Unsafe Extern "C")

yyjson_read_opts

pub unsafe extern "C" fn yyjson_read_opts(
    dat: *mut c_char,
    len: usize,
    alc: *const yyjson_alc,
    err: *mut yyjson_read_err,
) -> *mut yyjson_doc;
let mut error = yyjson_read_err {
    code: 0,
    msg: std::ptr::null(),
    pos: 0,
};
let doc_ptr = unsafe {
    yyjson_read_opts(json_buffer_ptr, json_len, &alc as *const yyjson_alc, &mut error as *mut _)
};
if doc_ptr.is_null() {
    eprintln!("Parse error at position {}: {:?}", error.pos, CStr::from_ptr(error.msg));
} else {
    // Use doc_ptr
}

yyjson_alc_pool_init

pub unsafe extern "C" fn yyjson_alc_pool_init(
    alc: *mut yyjson_alc,
    buf: *mut c_void,
    size: usize,
) -> bool;

Important Implementation Details


Interaction with Other System Components


Visual Diagram

classDiagram
    class yyjson_alc {
        +malloc(ctx: *mut c_void, size: usize) -> *mut c_void
        +realloc(ctx: *mut c_void, ptr: *mut c_void, size: usize) -> *mut c_void
        +free(ctx: *mut c_void, ptr: *mut c_void)
        +ctx: *mut c_void
    }

    class yyjson_read_err {
        +code: yyjson_read_code
        +msg: *const c_char
        +pos: usize
    }

    class yyjson_val_uni {
        +u64_: u64
        +i64_: i64
        +f64_: f64
        +str_: *const c_char
        +ptr: *mut c_void
        +ofs: usize
    }

    class yyjson_val {
        +tag: u64
        +uni: yyjson_val_uni
    }

    class yyjson_doc {
        +root: *mut yyjson_val
        +alc: yyjson_alc
        +dat_read: usize
        +val_read: usize
        +str_pool: *mut c_char
    }

    classFunctions <|.. yyjson_alc : uses
    yyjson_doc --> yyjson_val : root
    yyjson_val --> yyjson_val_uni : contains

    %% Functions
    class yyjson_functions {
        +yyjson_read_opts(dat: *mut c_char, len: usize, alc: *const yyjson_alc, err: *mut yyjson_read_err) -> *mut yyjson_doc
        +yyjson_alc_pool_init(alc: *mut yyjson_alc, buf: *mut c_void, size: usize) -> bool
    }

Summary

The [yyjson.rs](/projects/287/67743) file provides essential low-level Rust bindings to the `yyjson` C library, wrapping its core data structures, error handling, and memory allocation interfaces. It enables Rust programs to efficiently parse JSON data with customizable memory management, exposing raw pointers and unsafe functions that facilitate high-performance JSON processing in Rust applications.