https://github.com/m6vrm/json.hpp
Naive JSON encdoing and decoding for C++
https://github.com/m6vrm/json.hpp
cpp json
Last synced: 2 months ago
JSON representation
Naive JSON encdoing and decoding for C++
- Host: GitHub
- URL: https://github.com/m6vrm/json.hpp
- Owner: m6vrm
- Created: 2025-06-22T02:07:05.000Z (about 1 year ago)
- Default Branch: master
- Last Pushed: 2025-06-23T21:04:58.000Z (about 1 year ago)
- Last Synced: 2025-06-23T22:23:07.408Z (about 1 year ago)
- Topics: cpp, json
- Language: C++
- Homepage:
- Size: 4.88 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
json.hpp
========
Naive JSON encoding and decoding for C++.
* No Unicode support
* Comments and trailing comma support
* Minimal implementation
> Inspired by https://github.com/jart/json.cpp.
Usage
-----
Add `json.hpp` to your project.
```cpp
#include
#include
#include
#define JSON_IMPLEMENTATION
#define JSON_STRICT // strict parsing is a bit slower, but doesn't allow invalid json
#include "json.hpp"
int main() {
// parse json
JSON::Status status; // status is optional
JSON json1;
std::string string = R"(
{
// comments are ignored
"key": "value", // trailing comma is allowed
}
)";
bool success = json1.parse(string, &status);
assert(success);
std::cout << JSON::status_string(status) << std::endl;
// get values
assert(json1.empty() == false);
assert(json1.size() == 1);
assert(json1.has("key"));
assert(json1["key"].get_string() == "value");
// create json
JSON json2;
json2["key"] = "value";
// dump to string
std::cout << json2.dump(true) << std::endl;
assert(json1.dump() == json2.dump());
return 0;
}
```