|
1 | | -# json_parser |
2 | | -Header only lightweight JSON parser library |
| 1 | +# JSON Parser Library |
| 2 | + |
| 3 | +A lightweight, single-header C library for parsing JSON data. Designed for simplicity and portability, this parser provides a low-footprint solution to decode JSON-formatted strings into structured tokens while adhering to core JSON specifications. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +- **Standard Compliance**: Supports parsing of JSON objects, arrays, strings, numbers, and literals (`true`, `false`, `null`). |
| 8 | +- **Unicode Support**: Handles UTF-16 surrogate pairs and encodes Unicode escape sequences into valid UTF-8. |
| 9 | +- **Configurable Limits**: Tunable thresholds for maximum nesting depth, token count, and string length. |
| 10 | +- **Error Reporting**: Detailed error codes and human-readable error messages for troubleshooting parsing issues. |
| 11 | +- **Memory Safety**: Cleanup functions ensure allocated resources are properly released. |
| 12 | + |
| 13 | +## Components |
| 14 | + |
| 15 | +### Core Structures |
| 16 | +- **`json_parser_t`**: Manages the parser state, including input data, token storage, and error tracking. |
| 17 | +- **`json_token_t`**: Represents a parsed JSON token, storing its type (object, array, string, number, etc.) and associated value. |
| 18 | +- **`json_error_t`**: Enumerates all possible parsing errors, such as invalid tokens, nesting depth exceeded, or allocation failures. |
| 19 | + |
| 20 | +### Key Functions |
| 21 | +- **Initialization & Cleanup**: `json_parser_init` prepares the parser, while `json_parser_free` releases allocated memory. |
| 22 | +- **Parsing**: `json_parser_parse` processes the input JSON string and populates tokens. |
| 23 | +- **Utilities**: `json_get_tokens` retrieves parsed tokens, and `json_error_string` converts error codes to descriptive messages. |
| 24 | + |
| 25 | +## Error Handling |
| 26 | +The parser tracks errors during execution and halts on the first encountered issue. Errors range from syntax violations (e.g., unexpected characters) to resource constraints (e.g., exceeding token limits). Users can programmatically check the error type and respond accordingly. |
| 27 | + |
| 28 | +## Building |
| 29 | +The library is implemented as a single-header file (`json_parser.h`) with optional embedded implementation. It can be integrated into projects in two ways: |
| 30 | +1. **As a Header-Only Library**: Define `JSON_PARSER_IMPLEMENTATION` in one source file to include the implementation. |
| 31 | +2. **As a Static Library**: Use the provided CMake configuration to compile a static library, simplifying linking in larger projects. |
| 32 | + |
| 33 | +## Usage |
| 34 | +1. **Initialize the Parser**: Provide the JSON input string and its length. |
| 35 | +2. **Parse the Input**: Execute the parsing routine and check for errors. |
| 36 | +3. **Retrieve Tokens**: Access the parsed tokens to read JSON structure and values. |
| 37 | +4. **Cleanup**: Release parser resources after processing. |
| 38 | + |
| 39 | +## :snowman: Author |
| 40 | + |
| 41 | +Eray Öztürk ([@diffstorm](https://github.com/diffstorm)) |
| 42 | + |
| 43 | +## License |
| 44 | + |
| 45 | +This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details. |
0 commit comments