ibireme / yyjson

The fastest JSON library in C
https://ibireme.github.io/yyjson/doc/doxygen/html/
MIT License
3.04k stars 262 forks source link

Add yyjson_read_fd and yyjson_write_fd #122

Closed TkTech closed 1 year ago

TkTech commented 1 year ago

There are many scenarios where I have access to a file descriptor (or HANDLE on win32) but not a path. For example, a secure temporary file on Linux can be just a floating inode - it doesn't actually have a file path at all.

This would make supporting high-level wrappers such as Python simpler, since just the file handle can be pushed down to yyjson from any file-like object that supports it. We don't want to read the file handle in Python and then pass the data to yyjson, as this requires a double allocation.

ibireme commented 1 year ago

I think yyjson_read_fp(FILE *fp, ...) might be better because FILE is ANSI C and file descriptor is platform-dependent.

The caller could use it like this:

// POSIX
yyjson_doc *read_fd(int fd) {
    FILE *fp = fdopen(fd, "r")
    yyjson_doc *doc = yyjson_read_fp(fd);
    fclose(fp);
    return doc;
}
ibireme commented 1 year ago

Added: https://github.com/ibireme/yyjson/commit/ac1191f9139b325177b766906feeff4639e8c015