mirror of
https://github.com/nlohmann/json.git
synced 2026-10-01 14:35:18 +00:00
tools/macro_builder generates the NLOHMANN_JSON_EXPAND, NLOHMANN_JSON_GET_MACRO and NLOHMANN_JSON_PASTE* macros in macro_scope.hpp, but nothing referred to it. Add a README that explains what it generates, how to run it and where the output goes, and which dependent tables (NLOHMANN_JSON_DOUBLE_PASTE, NLOHMANN_JSON_TYPE_BODY) are maintained by hand. Point to it from a comment above NLOHMANN_JSON_EXPAND. The generator itself is unchanged; following the README reproduces the header byte for byte. In serve_header.py, drop the LGTM suppression (LGTM.com shut down in 2022), replace the """.""" placeholder docstrings with real ones, and import socket and ssl at module level. DualStackServer.server_bind uses socket, which was only imported under __main__; when the module was imported instead, the NameError was swallowed and IPV6_V6ONLY was not cleared. The header change is a comment only; behavior, API and ABI are unchanged. Part of #5717 Signed-off-by: Niels Lohmann <mail@nlohmann.me>
macro_builder
Generates the argument-counting macros behind the NLOHMANN_DEFINE_TYPE_* and NLOHMANN_DEFINE_DERIVED_TYPE_*
macros in include/nlohmann/detail/macro_scope.hpp:
NLOHMANN_JSON_EXPANDNLOHMANN_JSON_GET_MACRO, which selects a macro by the number of its arguments (64 slots)NLOHMANN_JSON_PASTE, which calls a function-like macro for each member, and its helpersNLOHMANN_JSON_PASTE2toNLOHMANN_JSON_PASTE64
The number of slots (max_args in main.cpp) sets the member limit of these macros.
NLOHMANN_JSON_PASTE takes the function as its first argument, so 64 slots allow 63 members.
Usage
From the project root:
c++ -std=c++11 tools/macro_builder/main.cpp -o macro_builder
./macro_builder
- In
include/nlohmann/detail/macro_scope.hpp, replace the lines from#define NLOHMANN_JSON_EXPAND( x ) xto the#define NLOHMANN_JSON_PASTE64(...)line with the output, without its trailing empty line. - Run
make amalgamate. It updatessingle_include/nlohmann/json.hppand runsmake pretty, which indents the continuation lines ofNLOHMANN_JSON_PASTEthat the tool writes unindented.
With an unchanged main.cpp, these steps reproduce the header byte for byte.
Maintained by hand
The tool does not generate everything that depends on the number of slots. When changing max_args, also update:
- the
NLOHMANN_JSON_DOUBLE_PASTEtable right after the generated block (NLOHMANN_JSON_DOUBLE_PASTEandNLOHMANN_JSON_DOUBLE_PASTE3,NLOHMANN_JSON_DOUBLE_PASTE5, ...,NLOHMANN_JSON_DOUBLE_PASTE63), which the*_WITH_NAMESmacros use - the slot table of
NLOHMANN_JSON_TYPE_BODY, which chooses between the implementations for zero members and for one or more members - the documented limit of 63 members in
docs/mkdocs/docsand the tests at that limit intests/src/unit-udt_macro.cpp
Both tables pass one macro name per slot to NLOHMANN_JSON_GET_MACRO, so they need exactly as many entries as it has
slots.