https://github.com/ibob/jout
A fast minimalistic single-header JSON writer for C++
https://github.com/ibob/jout
Last synced: 6 months ago
JSON representation
A fast minimalistic single-header JSON writer for C++
- Host: GitHub
- URL: https://github.com/ibob/jout
- Owner: iboB
- License: mit
- Created: 2023-09-27T06:40:55.000Z (almost 3 years ago)
- Default Branch: master
- Last Pushed: 2024-05-14T09:04:12.000Z (about 2 years ago)
- Last Synced: 2025-06-17T01:43:22.975Z (about 1 year ago)
- Language: C++
- Size: 21.5 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# jout
[](https://isocpp.org/) [](https://en.wikipedia.org/wiki/C%2B%2B#Standardization) [](https://opensource.org/licenses/MIT)
A fast minimalistic single-header json writer for C++17.
It's essentially a trimmed-down version of [huse](https://github.com/iboB/huse) where only the basic JSON serialization is kept. It might be useful if one only wants to write a simple json without having to rely on all deps and features of huse.
## Usage
Just include `jout.hpp` in your project and you're good to go.
Optionally make use of the provided `CMakeLists.txt` which will add the dependency [mscharconv](https://github.com/iboB/mscharconv] in case the one provided by the standard library doesn't supprt `to_chars` for floating point numbers (which is the case for gcc 10, clang 14 and their previous versions).
## Features
* Single header
* No allocations
* No dependencies besides the standard library (well, unless you use an old one in which case you'll need mscharconv)
* Sequential. Values are added as they are encountered in the code. There is no way to go back and modify a value.
* Optional extension headers to serialize common types
## Example
```cpp
// create a document with an ostream and an optional pretty print flag
jout::Document doc(std::cout, true);
// create a root object
auto root = doc.obj();
// add some values
root.val("hello", "world");
root.val("number", 3.14);
// add an array
{
// block, array is closed when ar goes out of scope
auto ar = root.ar("array");
ar.val(1);
ar.val("str");
ar.val(true);
ar.val(nullptr);
}
// add a subobject
{
// block, object is closed when obj goes out of scope
auto obj = root.obj("object");
obj.val("key", "value");
}
```
### Writing Custom Types
Create an overload of `joutWrite` as per the example below:
```c++
struct Person {
std::string name;
int age;
std::vector pets;
};
// create an overload of joutWrite for your type
// be sure that it's either in namespace jout
// or in the namespace of your type
void joutWrite(jout::Node& node, const Person& p) {
auto obj = node.obj();
obj.val("name", p.name);
obj.val("age", p.age);
obj.val("pets", p.pets);
}
```
Then you can write instances of your type with `val`, like any other value:
```c++
Person alice = {
"Alice B.",
35,
{"Lucky", "Fido", "Goldie"}
};
doc.val(alice);
```
## License
[](https://opensource.org/licenses/MIT)
This software is distributed under the MIT Software License.
See accompanying file LICENSE or copy [here](https://opensource.org/licenses/MIT).
Copyright © 2023 [Borislav Stanimirov](http://github.com/iboB)