yyjson-reduce-unused.patch


Overview

The file `yyjson-reduce-unused.patch` is a patch file containing modifications to the **yyjson** C library source code (`yyjson.c` and `yyjson.h`). This patch primarily removes or disables unused or dead code paths related to:

These removals simplify the codebase by reducing unused code branches, dead code, and redundant complexity, likely improving maintainability and possibly reducing binary size.


Detailed Explanation of Changes

Removed Functions and Code Blocks


Modified Functions and APIs

yyjson_read_opts()

Internal Parsing Functions


Implementation Details and Algorithms

The patch does **not** introduce new algorithms or change the core JSON parsing logic. Instead, it:

This reduces the complexity of the codebase and potentially reduces binary size and maintenance overhead.


Usage Examples

**Before the patch:**

yyjson_read_flag flags = YYJSON_READ_ALLOW_TRAILING_COMMAS | YYJSON_READ_ALLOW_INVALID_UNICODE;
yyjson_doc *doc = yyjson_read_opts(json_data, len, flags, &allocator, &err);

**After the patch:**

yyjson_doc *doc = yyjson_read_opts(json_data, len, &allocator, &err);

The caller no longer passes parsing flags; the parser only supports standard JSON without extensions.


Interaction with Other System Components


Summary of Affected Functions and Entities

Entity/Function

Description

Change Summary

`read_inf_or_nan()`

Parses special float literals (`Inf`, `NaN`)

Fully removed

`read_number_raw()`

Parses numbers as raw string

Disabled/removed

`read_number()`

Parses JSON numbers

Flag parameter removed, simplified

`yyjson_read_opts()`

Main JSON parsing API

Flag parameter removed, simplified

`read_root_single()`, `read_root_minify()`, `read_root_pretty()`

Internal parsers for JSON root

Flags removed, dead code removed

`copy_escape()`, UTF-8 validation

String parsing and escaping

Invalid Unicode acceptance removed

Error handling macros

Handle errors during parsing

Flags removed

Flag-based conditional code

Code branches depending on flags

Removed


Mermaid Diagram: Simplified JSON Parsing Flow After Patch

flowchart TD
    Start[Start Parsing]
    Allocator[Allocate Padded Buffer]
    SkipWS[Skip Whitespace]
    DetectRoot{Root Token}
    ParseObj[Parse Object]
    ParseArr[Parse Array]
    ParseVal[Parse Single Value]
    BuildDoc[Build JSON Document]
    Finish[Return Document]

    Start --> Allocator --> SkipWS --> DetectRoot
    DetectRoot -->|{ Object| ParseObj
    DetectRoot -->|[ Array| ParseArr
    DetectRoot -->|Other| ParseVal
    ParseObj --> BuildDoc
    ParseArr --> BuildDoc
    ParseVal --> BuildDoc
    BuildDoc --> Finish

This diagram shows the high-level flow after the patch simplifies the parser by removing non-standard features and flags.


Conclusion

The `yyjson-reduce-unused.patch` file significantly trims down the **yyjson** C library by removing unused or disabled features related to:

This results in a streamlined codebase focused strictly on standard JSON parsing, with a simplified public API and internal logic. It improves maintainability and reduces complexity, at the cost of dropping some extended JSON parsing capabilities.


Appendix: Example Simplified API Usage (Post-Patch)

#include "yyjson.h"

int main() {
    const char *json = "{\"key\": 123, \"arr\": [1,2,3]}";
    yyjson_read_err err;
    yyjson_doc *doc = yyjson_read_opts((char *)json, strlen(json), NULL, &err);
    if (!doc) {
        printf("Parse error at %zu: %s\n", err.pos, err.msg);
        return 1;
    }
    yyjson_val *root = yyjson_doc_get_root(doc);
    // Use root...
    yyjson_doc_free(doc);
    return 0;
}

End of Documentation for yyjson-reduce-unused.patch