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:
Parsing of special floating-point literals such as
Inf,Infinity, andNaN.Handling of raw number strings as an alternative to direct numeric parsing.
Parsing with flags for non-standard JSON features like allowing invalid Unicode, comments, or trailing commas.
The
yyjson_read_flagparameter from various API functions.Conditional code paths guarded by
if (false)or similar constructs.Additional in-situ parsing features and related memory management.
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
read_inf_or_nan():
A static inline function that attempted to parseInf,Infinity, orNaNliterals in a case-insensitive manner has been removed. This function was only invoked in dead code blocks (guarded byif (false)), so it's fully eliminated.Raw Number Reading Support:
Code paths that offered reading numbers as raw strings (read_number_raw()) and related macros (return_raw()) have been disabled or removed. This includes disabling raw number parsing flags and logic.Unused Flags and Parameters:
The yyjson_read_flag flg parameter is removed from many internal parsing functions and the public API functionyyjson_read_opts(). The patch removes all references to this flag and related conditional code that depended on it.Comments and Invalid Unicode Handling:
Code for skipping C-style comments and allowing invalid Unicode in strings is disabled or removed. This includes related error handling and conditional branches guarded byif (false).Trailing Commas Handling:
Checks and allowances for trailing commas in JSON arrays and objects are removed.In-situ Parsing and String Pool Handling:
Slight changes were made to how the string pool (doc->str_pool) is assigned, removing conditional logic based on flags.Error Handling Adjustments:
The patch modifies error reporting macros to no longer depend on the removed flag parameters.Memory Allocation and Input Validation:
The patch removes certain input validation checks and simplifies the memory allocation logic by always allocating a padded buffer copy of the input JSON data, instead of conditionally using in-situ parsing.
Modified Functions and APIs
yyjson_read_opts()
Signature change:
Removed the yyjson_read_flag flg parameter from the API:yyjson_doc *yyjson_read_opts(char *dat, size_t len, const yyjson_alc *alc, yyjson_read_err *err);Behavioral simplifications:
Always copies the input JSON data into a new padded buffer (no in-situ parsing).
Skips whitespace without comment-skipping support.
Chooses the parsing mode (pretty/minify/single) based on input content but without flags.
Frees allocated buffer on error.
Simplifies error messages related to Unicode BOM and unsupported encodings.
Internal Parsing Functions
Removed
flgparameters and all conditional branches depending on flags.Removed all
if (false)blocks (dead code).Removed all calls to
read_inf_or_nan(),read_nan(), and related literal parsing beyond standard JSON literals (true,false,null).Removed handling for raw number strings.
Simplified UTF-8 validation code by removing optional invalid Unicode acceptance.
Removed trailing commas handling in arrays and objects.
Removed support for skipping comments.
Implementation Details and Algorithms
The patch does **not** introduce new algorithms or change the core JSON parsing logic. Instead, it:
Cleans up the code by removing unused or disabled code paths.
Simplifies the API by removing the flags parameter, making the parser less configurable but more streamlined.
Ensures only standard JSON features are supported, dropping support for non-standard features like
NaN,Inf, comments, trailing commas, and raw numbers.
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
The patch affects the yyjson embedded C library used by higher-level Rust and Python layers.
By removing flags and related features, the Rust deserialization backend and Python bindings relying on
yyjsonwill have reduced parsing options.The removal of non-standard JSON features means that upper layers must ensure input JSON conforms strictly to the standard.
Simplifies integration due to fewer parsing modes and options.
May affect existing tests or benchmarks relying on extended parsing features.
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:
Special floating-point literals (
Inf,NaN).Raw number parsing.
Parsing flags and options.
Non-standard JSON features like comments, trailing commas, invalid Unicode.
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;
}