https://github.com/defini7/json
Single header-file Json library
https://github.com/defini7/json
Last synced: about 1 month ago
JSON representation
Single header-file Json library
- Host: GitHub
- URL: https://github.com/defini7/json
- Owner: defini7
- License: bsd-3-clause
- Created: 2025-03-31T18:05:02.000Z (over 1 year ago)
- Default Branch: master
- Last Pushed: 2025-09-09T15:14:17.000Z (11 months ago)
- Last Synced: 2025-10-27T13:55:42.297Z (9 months ago)
- Language: C++
- Homepage:
- Size: 12.7 KB
- Stars: 1
- Watchers: 0
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Simple C++ JSON Parser
A lightweight, header-only JSON parser and serializer written in C++20.
## Features:
- Header-only library
- Supports objects, arrays, strings, numbers, booleans, null
- Pretty-printed JSON output
- Parse from strings or files
## Installation:
In ONE translation unit:
```cpp
#define JSON_IMPL
#include "json.hpp"
```
In other files:
```cpp
#include "json.hpp"
```
## Basic Usage:
### Parse from raw string:
```cpp
#include "json.hpp"
int main()
{
std::string raw = R"({
"name": "John",
"age": 30,
"active": true
})";
Json::Node root = Json::ParseRaw(raw);
std::cout << root["name"].String() << std::endl;
std::cout << root["age"].Number() << std::endl;
}
```
### Parse from file:
```cpp
auto result = Json::ParseFile("data.json");
if (result)
{
std::cout << result->operator[]("name").String() << std::endl;
}
```
### Accessing Values:
Objects:
```cpp
std::string name = root["name"].String();
double age = root["age"].Number();
bool active = root["active"].Bool();
```
Arrays:
```cpp
Json::Node& arr = root["items"];
std::cout << arr[0].String() << std::endl;
std::cout << arr[1].Number() << std::endl;
```
### Dumping JSON:
Print:
```cpp
root.Dump();
```
To string:
```cpp
std::string pretty = Json::Dump(root);
std::cout << pretty;
```
Custom indentation:
```cpp
std::string pretty = Json::Dump(root, 2);
```
### Supported Types:
- string -> String()
- number -> Number()
- boolean -> Bool()
- null -> IsNull()
- array -> Array()
- object -> Object()
### Type Checking:
```cpp
node.IsObject()
node.IsArray()
node.IsString()
node.IsNumber()
node.IsBool()
node.IsNull()
```
## Notes:
- Keys preserve insertion order
- Numbers are stored as double
- No escape sequence handling yet
- No unicode support
- Minimal error recovery
## License:
BSD 3-Clause License