https://github.com/gflegar/json-printer
KISS header-only library for printing JSON objects to C++ streams.
https://github.com/gflegar/json-printer
json
Last synced: about 2 months ago
JSON representation
KISS header-only library for printing JSON objects to C++ streams.
- Host: GitHub
- URL: https://github.com/gflegar/json-printer
- Owner: gflegar
- License: mit
- Created: 2018-03-16T13:11:29.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2018-05-20T12:28:34.000Z (about 8 years ago)
- Last Synced: 2025-01-26T09:29:02.489Z (over 1 year ago)
- Topics: json
- Language: C++
- Size: 8.79 KB
- Stars: 0
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
JSON printer
============
[](https://gitlab.com/gflegar/json-printer-ci/commits/master)
JSON printer is a header-only C++11 library for printing JSON objects to C++
streams.
It tries to follow the KISS principle, so the entire library consists of 1
class with 2 methods implemented in roughly ~150 lines of code.
Installation
------------
Just copy `json_printer.hpp` somewhere in you include path (e.g. you program's
directory).
Documentation
-------------
The [example](example.cpp) covers all the features in the library:
```c++
#include
#include
#include "json_printer.hpp"
int main()
{
using json_printer = jpr::json_printer;
const auto pretty_print = true; // trade output size for readability
const auto indent = 2; // indentation level when pretty-printing
json_printer out(std::cout, pretty_print, indent);
out.print("Tesla", json_printer::array);
out.print(json_printer::object); // inside array, pass just the value
out.print("name", "K20"); // inside object, pass name & value
// out.print("K20"); // error: need name inside of object
out.print("cores", 2496);
out.finalize();
// out.print("name", "value"); // error: can't have name in array
// json_printer also supports chaining
out.print(json_printer::object)
.print("name", "K40")
.print("cores", 2880)
.finalize()
.print(json_printer::object)
.print("name", "K80")
.print("cores", 4992)
.finalize()
.print(json_printer::object)
.print("name", "P100")
.print("cores", 3584)
.finalize()
.print(json_printer::object)
.print("name", "V100")
.print("cores", 5120)
.finalize()
.finalize();
// let's just list a couple of GeForces
out.print("GeForce", json_printer::array);
for (auto num : {960, 970, 980, 1050, 1060, 1070, 1080}) {
out.print(std::string("GTX") + std::to_string(num));
}
out.finalize();
// and forget all about Quadros
out.print("Quadro", nullptr);
}
```
### Output
```json
{
"Tesla" : [
{
"name" : "K20",
"cores" : 2496
},
{
"name" : "K40",
"cores" : 2880
},
{
"name" : "K80",
"cores" : 4992
},
{
"name" : "P100",
"cores" : 3584
},
{
"name" : "V100",
"cores" : 5120
}
],
"GeForce" : [
"GTX960",
"GTX970",
"GTX980",
"GTX1050",
"GTX1060",
"GTX1070",
"GTX1080"
],
"Quadro" : null
}
```