|
1 |
| -# JSON Parser Library |
| 1 | +# JSON Parser Library [](https://github.com/diffstorm/json_parser) |
| 2 | + |
| 3 | +[](https://github.com/diffstorm/json_parser/actions) |
| 4 | +[](https://github.com/diffstorm/json_parser/blob/main/LICENSE) |
| 5 | +[](https://github.com/diffstorm/json_parser) |
| 6 | +[](https://codecov.io/gh/diffstorm/json_parser) |
| 7 | + |
| 8 | + |
2 | 9 |
|
3 | 10 | 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 | 11 |
|
@@ -30,12 +37,92 @@ The library is implemented as a single-header file (`json_parser.h`) with option
|
30 | 37 | 1. **As a Header-Only Library**: Define `JSON_PARSER_IMPLEMENTATION` in one source file to include the implementation.
|
31 | 38 | 2. **As a Static Library**: Use the provided CMake configuration to compile a static library, simplifying linking in larger projects.
|
32 | 39 |
|
| 40 | + ```bash |
| 41 | + sudo apt-get install -y build-essential cmake libgtest-dev googletest |
| 42 | + git clone https://github.com/diffstorm/json_parser.git |
| 43 | + cd json_parser |
| 44 | + mkdir build |
| 45 | + cd build |
| 46 | + cmake .. |
| 47 | + make |
| 48 | + ./demo |
| 49 | + ./demo_cpp |
| 50 | + ./json_parser_test |
| 51 | + ``` |
| 52 | + |
33 | 53 | ## Usage
|
34 | 54 | 1. **Initialize the Parser**: Provide the JSON input string and its length.
|
35 | 55 | 2. **Parse the Input**: Execute the parsing routine and check for errors.
|
36 | 56 | 3. **Retrieve Tokens**: Access the parsed tokens to read JSON structure and values.
|
37 | 57 | 4. **Cleanup**: Release parser resources after processing.
|
38 | 58 |
|
| 59 | + |
| 60 | +## API Reference |
| 61 | + |
| 62 | +### Data Structures |
| 63 | +#### `json_parser_t` |
| 64 | +Manages the parser's state and configuration. |
| 65 | +- **Fields**: |
| 66 | + - `const char *json`: Input JSON string (not copied; must remain valid during parsing). |
| 67 | + - `size_t length`: Length of the JSON input. |
| 68 | + - `size_t max_depth`: Maximum allowed nesting depth (default: `JSON_DEFAULT_MAX_DEPTH`). |
| 69 | + - `size_t max_string`: Maximum allowed string length (default: `JSON_DEFAULT_MAX_STRING`). |
| 70 | + - `json_error_t error`: Current error code (`JSON_ERROR_NONE` if no error). |
| 71 | +
|
| 72 | +#### `json_token_t` |
| 73 | +Represents a parsed JSON token. |
| 74 | +- **Fields**: |
| 75 | + - `json_token_type_t type`: Token type (e.g., `JSON_TOKEN_STRING`, `JSON_TOKEN_NUMBER`). |
| 76 | + - Union `value`: |
| 77 | + - `char *string`: String value (valid if `type` is `JSON_TOKEN_STRING`). |
| 78 | + - `double number`: Numeric value (valid if `type` is `JSON_TOKEN_NUMBER`). |
| 79 | + - `size_t start`, `end`: Start and end positions in the original JSON string. |
| 80 | +
|
| 81 | +#### `json_error_t` |
| 82 | +Enumerates parsing error codes (e.g., `JSON_ERROR_INVALID_TOKEN`, `JSON_ERROR_ALLOCATION_FAILED`). |
| 83 | +
|
| 84 | +--- |
| 85 | +
|
| 86 | +### Functions |
| 87 | +#### `void json_parser_init(json_parser_t *parser, const char *json, size_t length)` |
| 88 | +Initializes the parser with a JSON input string. |
| 89 | +- `parser`: Uninitialized parser instance. |
| 90 | +- `json`: Pointer to the JSON string. |
| 91 | +- `length`: Length of the JSON string. |
| 92 | +- **Note**: Sets default limits and allocates initial token memory. |
| 93 | +
|
| 94 | +#### `void json_parser_free(json_parser_t *parser)` |
| 95 | +Releases all memory allocated by the parser (tokens, strings, etc.). |
| 96 | +- Must be called after parsing to avoid leaks. |
| 97 | +
|
| 98 | +#### `json_error_t json_parser_parse(json_parser_t *parser)` |
| 99 | +Parses the JSON input and populates tokens. |
| 100 | +- Returns the first encountered error (or `JSON_ERROR_NONE` on success). |
| 101 | +- Tokens are accessible via `json_get_tokens` after parsing. |
| 102 | +
|
| 103 | +#### `const char *json_error_string(json_error_t error)` |
| 104 | +Converts an error code to a human-readable message (e.g., `JSON_ERROR_INVALID_NUMBER` ? `"Invalid number format"`). |
| 105 | +
|
| 106 | +#### `const json_token_t *json_get_tokens(const json_parser_t *parser, size_t *count)` |
| 107 | +Retrieves the parsed tokens. |
| 108 | +- `count`: Output parameter for the number of tokens. |
| 109 | +- Returns a pointer to the token array (valid until `json_parser_free` is called). |
| 110 | +
|
| 111 | +--- |
| 112 | +
|
| 113 | +### Enums |
| 114 | +#### `json_token_type_t` |
| 115 | +Defines token types: |
| 116 | +- `JSON_TOKEN_OBJECT`, `JSON_TOKEN_ARRAY`, `JSON_TOKEN_STRING`, `JSON_TOKEN_NUMBER`, |
| 117 | + `JSON_TOKEN_TRUE`, `JSON_TOKEN_FALSE`, `JSON_TOKEN_NULL`, `JSON_TOKEN_INVALID`. |
| 118 | +
|
| 119 | +--- |
| 120 | +
|
| 121 | +### Configuration Macros |
| 122 | +- `JSON_DEFAULT_MAX_TOKENS`: Initial token array capacity. |
| 123 | +- `JSON_DEFAULT_MAX_DEPTH`: Default maximum nesting depth. |
| 124 | +- `JSON_DEFAULT_MAX_STRING`: Default maximum string length. |
| 125 | +
|
39 | 126 | ## :snowman: Author
|
40 | 127 |
|
41 | 128 | Eray Öztürk ([@diffstorm](https://github.com/diffstorm))
|
|
0 commit comments