vendor-yyjson


Overview

The `vendor-yyjson` file is a shell script designed to automate the **embedding and customization** of the **yyjson** C JSON parsing library into a larger software project. It fetches a specific fixed commit version of the `yyjson` source files (`yyjson.c` and `yyjson.h`) from the official GitHub repository, applies a series of source-code patches and textual modifications to adapt the library to the project’s specific needs, and applies additional patches that address recursion limits and reduce unused code.

This integration approach ensures the project uses a **stable, customized version** of `yyjson` with tailored behavior, memory management, and feature set suitable for the surrounding system (notably a Rust-based JSON parsing backend). The script is idempotent and can be rerun to update or reset the embedded codebase to the exact specified commit and patch state.


File Functionality Summary

This process results in a **trimmed, hardened, and controlled version of the embedded `yyjson` library** that fits the project’s performance, feature, and safety requirements.


Detailed Explanation of Script Steps

#!/usr/bin/env bash

set -eou pipefail
yyjson_version="b21c02904188add942d3c7cd4885422e4335f115"
curl -Ls -o include/yyjson/yyjson.c "https://raw.githubusercontent.com/ibireme/yyjson/${yyjson_version}/src/yyjson.c"
curl -Ls -o include/yyjson/yyjson.h "https://raw.githubusercontent.com/ibireme/yyjson/${yyjson_version}/src/yyjson.h"

Source Code Modifications via sed

These inline edits customize the embedded `yyjson` code:

sed -i 's/yyjson_api_inline void yyjson_doc_free/yyjson_api void yyjson_doc_free/g' include/yyjson/yyjson.h

The following `sed` commands **disable or simplify conditional features and error handling** by replacing complex flag checks or runtime conditionals with `false` or `true` constants to remove code paths:

sed -i 's/(flg & (YYJSON_READ_NUMBER_AS_RAW | YYJSON_READ_BIGNUM_AS_RAW)) != 0/false/g' include/yyjson/yyjson.c
sed -i 's/if (pre)/if (false)/g' include/yyjson/yyjson.c
sed -i 's/!false/true/g' include/yyjson/yyjson.c
sed -i 's/ && true//g' include/yyjson/yyjson.c
sed -i 's/true && //g' include/yyjson/yyjson.c
sed -i 's/unlikely(false)/false/g' include/yyjson/yyjson.c
sed -i 's/YYJSON_TYPE_STR | YYJSON_SUBTYPE_NOESC/YYJSON_TYPE_STR/g' include/yyjson/yyjson.c
sed -i 's/unlikely(pos == src)/false/g' include/yyjson/yyjson.c
sed -i 's/    yyjson_read_err dummy_err;//g' include/yyjson/yyjson.c
sed -i 's/    if (!err) err = &dummy_err;//g' include/yyjson/yyjson.c
sed -i 's/likely(!alc_ptr)/!alc_ptr/g' include/yyjson/yyjson.c
sed -i 's/unlikely(read_flag_eq(flg, YYJSON_READ_##_flag))/false/g' include/yyjson/yyjson.c
sed -i 's/has_read_flag(ALLOW_INF_AND_NAN)/false/g' include/yyjson/yyjson.c
sed -i 's/has_read_flag(ALLOW_COMMENTS)/false/g' include/yyjson/yyjson.c
sed -i 's/has_read_flag(BIGNUM_AS_RAW)/false/g' include/yyjson/yyjson.c
sed -i 's/if (pre && \*pre)/if (false)/g' include/yyjson/yyjson.c
sed -i 's/(pre && !false)/(false)/g' include/yyjson/yyjson.c

Applying Git Patches

git apply include/yyjson-recursion-limit.patch
git apply include/yyjson-reduce-unused.patch

Usage and Integration


Important Implementation Details


Example Usage

To update or embed the customized `yyjson` library, run:

./vendor-yyjson

This will:


Interaction with Other System Parts


Mermaid Diagram: Script Workflow and yyjson Integration

flowchart TD
    A[Start vendor-yyjson Script] --> B[Set yyjson_version Commit]
    B --> C[Download yyjson.c and yyjson.h]
    C --> D[Apply sed Modifications to yyjson.h]
    D --> E[Apply sed Modifications to yyjson.c]
    E --> F[Apply yyjson-recursion-limit.patch]
    F --> G[Apply yyjson-reduce-unused.patch]
    G --> H[Source Ready for Compilation]
    H --> I[Build System Compiles Patched yyjson]
    I --> J[Rust Backend Calls yyjson C API]
    J --> K[High-Performance JSON Parsing]

Summary

The `vendor-yyjson` file is a **vendor script** that automates fetching, customizing, and patching the `yyjson` C library for integration into a larger JSON parsing system. Through fixed version fetching, targeted source modifications, and patch application, it ensures the embedded `yyjson` library matches the project’s requirements for speed, safety, and feature set. This forms the foundational JSON parsing engine used primarily by the Rust deserialization backend, enabling blazing-fast, memory-safe JSON processing within a multi-language ecosystem.