https://github.com/rft0/lightjson
Simple, Light Weight, Header Only, C++11 compliant JSON Library.
https://github.com/rft0/lightjson
cpp header-only json json-decoder json-encoder json-library json-parser json-serialization
Last synced: 5 months ago
JSON representation
Simple, Light Weight, Header Only, C++11 compliant JSON Library.
- Host: GitHub
- URL: https://github.com/rft0/lightjson
- Owner: rft0
- License: gpl-3.0
- Created: 2025-02-04T12:28:45.000Z (11 months ago)
- Default Branch: main
- Last Pushed: 2025-02-07T21:18:03.000Z (11 months ago)
- Last Synced: 2025-04-05T19:37:42.239Z (9 months ago)
- Topics: cpp, header-only, json, json-decoder, json-encoder, json-library, json-parser, json-serialization
- Language: C++
- Homepage:
- Size: 44.9 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# LightJSON
LightJSON is a Simple, Light Weight, Header Only, C++11 compliant JSON Library. It is designed for projects which need a minimalistic JSON solution without the overhead of larger libraries.
## Usage
### Including Header
Just include header file to use it.
```c
#include "jspn.hpp"
```
If you only want to use reading contents of json without dumping / manipulaitng it you can disable those features. (which may help to reduce binary size)
```c
#define JSON_DISABLE_DUMPING
#include "jspn.hpp"
```
### Parsing JSON
For starters you can pass either a raw pointer, std::string or a std::ifstream to parser.
```cpp
const char* data = R"("name": "John", "lastName": "Doe", "age": 30, "something": [1, 2.5, {"three": "four"}])";
JSON json = JSONParser::parse(data)
// or
std::ifstream f("test.json");
JSON json = JSONParser::parse(data)
```
### Reading Values
Reading simple fields.
```cpp
const char* data = R"("name": "John", "lastName": "Doe", "age": 30, "something": [1, 2.5, {"dummy": "dummyvalue"}])";
JSON json = JSONParser::parse(data)
std::string name = json["name"].as(); // John
int age = json["age"].as(); // 30
int someFloat = json["something"][1] // 2.5
std::string dummy = json["something"][2]["dummy"] // dummyvalue
```
Reading arrays.
```cpp
const char* data = R"("intArray": [1, 2, 3, 4, 5], "complexArray": ["something", 1, { "dummy": 2 }] )";
JSON json = JSONParser::parse(data);
// Reading an array when all members are same type
std::vector intArray = json["intArray"].as>(); // [1, 2, 3, 4, 5]
// Reading an array when it contains members with different types.
std::vector complexArray = json["complexArray"].as>();
complexArray[0].as(); // something
complexArray[1].as(); // 1
complexArray[2]["dummy"].as(); // 2
// or we can read value of field "dummy" like this.
JSON anotherRoot = complexArray[2];
anotherRoot["dummy"].as() // 2
```
### Creating / Changing / Dumping Values
Dumping is disabled if `JSON_DISABLE_DUMPING` is defined.
```c
JSON root;
root["something"] = "another thing";
root["exampleArray"] = {1, 2, 3};
root["anotherObject]["smt"] = "value";
// Dumping json
printf("%s", root.dump(4)); // (first argument means indentation space count, default: 4)
// or we can dump json root directly with using overloaded << operator.
std::cout << root << std::endl;
// another way to create json from root with initializer lists
JSON root = JSON::o({
{"something", "another thing"},
{"exampleArray", {1, 2, 3}},
{"anotherObject", { "smt", "value" }}
});
```