- C++23 compiler
- only tested with GCC 13/14
- CMake (CMake-GUI for easier configuration)
- Node.js/NPM (for the NAPI wrapper only)
- SIMD Json
- Nlohmann Json
- Google Test
Add wrapped libraries, at least one is needed:
- SIMD Json
- Nlohmann Json
For a basic build, with both SIMD Json and Nlohmann Json, run:
mkdir -p build
cd build
cmake .. -DENABLE_SIMD_JSON=On -DENABLE_NLOH_JSON=OnExtra options:
-DBUILD_WRITER=Onto build a serializer from the any of the external library-DBUILD_TEST=Offto disable the tests-DBUILD_READER=Offto disable the deserializer
Built libraries are located in lib/<CMAKE_BUILD_TYPE>.
Using the Nix shell is a simple way to install the tested version of gcc, cmake, simdjson, and nlohmann_json.
Once in the shell, it's the same building step from C++ static library.
In the cpp directory:
npm install
npm run build-writerThis creates the static library libjson_wrapper_napi_static.a.
Extra scripts/options:
buildto only include the deserializer part.testto run some quick tests.- the
-devsuffix is there to build in debug mode.
Using the same example as in the base README (example.jtd.json):
{
"properties": {
"name": { "type": "string" },
"isAdmin": { "type": "boolean" },
"favoriteNumbers": { "elements": { "type": "float64" } }
}
}and this C++ configuration file (cpp_config.json):
{
"namespace": "Test",
"guard": "pragma",
"output": "both"
}You can generate this C++ interface in a header file example.hpp:
#pragma once
#include <json_data.hpp>
#include <json_reader.hpp>
#include <json_writer.hpp>
#include <string>
#include <vector>
namespace Test {
// forward declarations
struct Example;
// declarations
struct Example {
std::vector<double> favorite_numbers;
bool is_admin;
std::string name;
};
// prototypes
JsonTypedefCodeGen::ExpType<Example> deserialize_Example(const JsonTypedefCodeGen::Reader::JsonValue& value);
JsonTypedefCodeGen::ExpType<void> serialize_Example(JsonTypedefCodeGen::Writer::Serializer& serializer, const Example& value);
} // namespace TestThe CLI to create this C++ interface in the current directory is:
./jtd-codegen example.jtd.json --cpp-out . --cpp-props cpp_config.jsonBecause there are multiple ways to write C++, a configuration file is necessary to support them all instead of relying on more arguments on the CLI. This configuration file is optional, in that case, the default values of the various properties is used.
This is to control the header guards. There's two values:
"pragma"which puts a#pragma onceat the top of every header file{"name": "GUARD_NAME"}which creates a header guard like this, with the matching#endifat the bottom:
#ifndef GUARD_NAME
#define GUARD_NAME
// ...
#endifHow header files should be included in the generated code. include_data refers to "json_data.hpp", include_reader to "json_reader.hpp, and include_writer to "json_writer.hpp".
Here are the possible values:
"ignore": don't include them"local": use#include "header.hpp"(relative to the header file)"system"(default): use#include <header.hpp>{"path": "where/it/is"}: usewhere/it/isas a prefix to the file name,#include "where/it/is/header.hpp
Wraps the generated code in a namespace, see example above with the namespace Test.
Which operations should be generated:
"serialize"- generate serialization code only"deserialize"- generate deserialization code only"both"(default) - generate both serialization and deserialization code
- Single header file
- C++ Modules
- CMake function to help compile a list of
*.jtd.jsonfiles - Include comments from
*.jtd.jsonfiles - Refine the quality of the generated C++ code
- Other wrappers:
- Rapid JSON
- BSON (
bsoncxx)