Skip to content

Commit cd6f619

Browse files
committed
Add API reference to readme
1 parent 0b2430b commit cd6f619

File tree

2 files changed

+89
-2
lines changed

2 files changed

+89
-2
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# author Eray Ozturk | erayozturk1@gmail.com
1+
# Copyright (c) 2025 Eray Ozturk <erayozturk1@gmail.com>
22
# url github.com/diffstorm
33
cmake_minimum_required(VERSION 3.12)
44
project(json_parser LANGUAGES C CXX)

README.md

Lines changed: 88 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
# JSON Parser Library
1+
# JSON Parser Library [![Awesome](https://awesome.re/badge.svg)](https://github.com/diffstorm/json_parser)
2+
3+
[![Build Status](https://github.com/diffstorm/json_parser/actions/workflows/c-cpp.yml/badge.svg)](https://github.com/diffstorm/json_parser/actions)
4+
[![License](https://img.shields.io/github/license/diffstorm/json_parser)](https://github.com/diffstorm/json_parser/blob/main/LICENSE)
5+
[![Language](https://img.shields.io/github/languages/top/diffstorm/json_parser)](https://github.com/diffstorm/json_parser)
6+
[![Code Coverage](https://codecov.io/gh/diffstorm/json_parser/branch/main/graph/badge.svg)](https://codecov.io/gh/diffstorm/json_parser)
7+
![GitHub Stars](https://img.shields.io/github/stars/diffstorm/json_parser?style=social)
8+
![Platforms](https://img.shields.io/badge/Platform-Linux%20%7C%20Windows%20%7C%20macOS-lightgrey)
29

310
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.
411

@@ -30,12 +37,92 @@ The library is implemented as a single-header file (`json_parser.h`) with option
3037
1. **As a Header-Only Library**: Define `JSON_PARSER_IMPLEMENTATION` in one source file to include the implementation.
3138
2. **As a Static Library**: Use the provided CMake configuration to compile a static library, simplifying linking in larger projects.
3239

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+
3353
## Usage
3454
1. **Initialize the Parser**: Provide the JSON input string and its length.
3555
2. **Parse the Input**: Execute the parsing routine and check for errors.
3656
3. **Retrieve Tokens**: Access the parsed tokens to read JSON structure and values.
3757
4. **Cleanup**: Release parser resources after processing.
3858

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+
39126
## :snowman: Author
40127
41128
Eray Öztürk ([@diffstorm](https://github.com/diffstorm))

0 commit comments

Comments
 (0)