mirror of
https://github.com/nlohmann/json.git
synced 2026-09-27 18:20:32 +00:00
* Add BON8 support Add to_bon8/from_bon8 and input_format_t::bon8 for BON8, a binary format that uses the byte values that cannot begin a UTF-8 character as type markers, so strings need no length prefix. It is the most compact of the supported binary formats on the benchmark files. The reader is non-recursive like the other binary readers. A string ends at the first byte that cannot continue it, so the reader hands the one or two bytes it reads past a string back to the value that follows. The writer produces the canonical representation of the specification, except for NFC normalization; its output is identical to that of the reference implementation (HikoGUI) on all files of the test data. The round-trip tests need the .bon8 files of json_test_data 3.2.0. Signed-off-by: Niels Lohmann <mail@nlohmann.me> * Address review comments - Reuse detail::validate_one_utf8 to check strings in to_bon8; the error now names the first byte of the invalid sequence. - Document that to_bon8 leaves bytes in the output adapter on an exception, and that string_open is only an output of write_bon8_marker. - Explain why the pushback buffer of the BON8 reader cannot overflow. Signed-off-by: Niels Lohmann <mail@nlohmann.me> * Select the BON8 float prefix by type get_bon8_float_prefix only depends on the type of its argument, so make the type a template parameter instead of passing an unused value. Signed-off-by: Niels Lohmann <mail@nlohmann.me> * Rename a test variable that Flawfinder mistakes for read() Signed-off-by: Niels Lohmann <mail@nlohmann.me> * Fix the BON8 CI failures - compare the float in write_bon8_float with number_float_t constants, so GCC does not warn about a float-to-double conversion - mark check_bon8_utf8's context as used when exceptions are disabled - choose the compact float prefix in a helper rather than with nested conditional operators (clang-tidy) - use auto for the cast in the BON8 integer reader (clang-tidy) - write the int32 minimum test values as long long literals (MSVC C4146) Signed-off-by: Niels Lohmann <mail@nlohmann.me> * Amalgamate Signed-off-by: Niels Lohmann <mail@nlohmann.me> * Read BON8 strings in bulk from contiguous input - copy the valid UTF-8 of a string in one step when the input is contiguous (twitter.json is read in 1.68 instead of 2.52 ms, jeopardy.json in 196 instead of 297 ms, close to CBOR and MessagePack) - share the new valid_utf8_prefix() with the writer's UTF-8 check, which now skips ASCII 8 bytes at a time - let the fuzzer check that contiguous and stream input give the same value or error, and test both paths in the unit tests - clarify that a second 0xFF after a string is an empty string Signed-off-by: Niels Lohmann <mail@nlohmann.me> * Link the BON8 functions from the other binary format pages Signed-off-by: Niels Lohmann <mail@nlohmann.me> * Name the bulk scan flag after the input, not BON8 Signed-off-by: Niels Lohmann <mail@nlohmann.me> * Read BSON keys in bulk from contiguous input BSON keys (and array indices) are C-style strings, which were read byte by byte. For contiguous input they are now read up to their \x00-byte in one step, using the same bulk_scan flag as BON8 strings: twitter.json is read in 1.46 instead of 2.01 ms, citm_catalog.json in 2.93 instead of 3.33 ms, jeopardy.json in 182 instead of 207 ms. canada.json, whose keys are almost all one-digit array indices, takes 2 % longer. Signed-off-by: Niels Lohmann <mail@nlohmann.me> * Fix the BON8 CI failures of the bulk-read tests - skip the contiguous-versus-stream tests of BON8 strings and BSON keys when exceptions are disabled: they catch the parse errors of invalid input, and without exceptions the library aborts instead - use static_cast for the int64 test value (google-readability-casting) Signed-off-by: Niels Lohmann <mail@nlohmann.me> * Move the explicit basic_json instantiation into its own test file Linking test-regression3_cpp20 with clang and MinGW failed with "relocation truncated to fit: IMAGE_REL_AMD64_REL32 against `.rdata'", as test-regression2 did before #5511. The explicit instantiation of basic_json<> for #4825 compiles every member function, including the BON8 reader and writer, into that object, and it was already close to the limit (2,226,104 bytes on develop, 2,234,960 with BON8; clang -O1, C++20). Give the instantiation a file of its own: unit-regression3 is now 1,594,736 bytes and unit-explicit_instantiation 1,095,064. The new file mentions JSON_HAS_CPP_17 and JSON_HAS_CPP_20 so it keeps being built for the C++17 standard the regression was about. Signed-off-by: Niels Lohmann <mail@nlohmann.me> * Convert the bytes of the BON8 test strings explicitly The str() helper constructed a std::string from a byte range, which converts each unsigned char implicitly; -fsanitize=integer reports that for bytes of 0x80 and above (ci_test_clang_sanitizer). Signed-off-by: Niels Lohmann <mail@nlohmann.me> --------- Signed-off-by: Niels Lohmann <mail@nlohmann.me>
297 lines
15 KiB
Makefile
297 lines
15 KiB
Makefile
.PHONY: pretty clean ChangeLog.md release update_hedley update_hedley_undef BUILD.bazel
|
|
|
|
##########################################################################
|
|
# configuration
|
|
##########################################################################
|
|
|
|
# find GNU sed to use `-i` parameter
|
|
SED:=$(shell command -v gsed || which sed)
|
|
|
|
|
|
##########################################################################
|
|
# source files
|
|
##########################################################################
|
|
|
|
# the list of sources in the include folder
|
|
SRCS=$(shell find include -type f | sort)
|
|
|
|
# the list of sources in the tests folder
|
|
TESTS_SRCS=$(shell find tests -type f \( -name '*.hpp' -o -name '*.cpp' -o -name '*.cu' \) -not -path 'tests/thirdparty/*' -not -path 'tests/abi/include/nlohmann/*' | sort)
|
|
|
|
# the single headers (amalgamated from the source files)
|
|
AMALGAMATED_FILE=single_include/nlohmann/json.hpp
|
|
AMALGAMATED_FWD_FILE=single_include/nlohmann/json_fwd.hpp
|
|
|
|
|
|
##########################################################################
|
|
# documentation of the Makefile's targets
|
|
##########################################################################
|
|
|
|
# main target
|
|
all:
|
|
@echo "amalgamate - amalgamate files single_include/nlohmann/json{,_fwd}.hpp from the include/nlohmann sources"
|
|
@echo "BUILD.bazel - regenerate the Bazel BUILD file from the include/nlohmann sources"
|
|
@echo "ChangeLog.md - generate ChangeLog file"
|
|
@echo "check-amalgamation - check whether sources have been amalgamated and BUILD.bazel is up to date"
|
|
@echo "clean - remove built files"
|
|
@echo "doctest - compile example files and check their output"
|
|
@echo "fuzz_testing - prepare fuzz testing of the JSON parser"
|
|
@echo "fuzz_testing_bon8 - prepare fuzz testing of the BON8 parser"
|
|
@echo "fuzz_testing_bson - prepare fuzz testing of the BSON parser"
|
|
@echo "fuzz_testing_cbor - prepare fuzz testing of the CBOR parser"
|
|
@echo "fuzz_testing_msgpack - prepare fuzz testing of the MessagePack parser"
|
|
@echo "fuzz_testing_ubjson - prepare fuzz testing of the UBJSON parser"
|
|
@echo "pretty - beautify code with Artistic Style"
|
|
@echo "run_benchmarks - build and run benchmarks"
|
|
@echo "update_hedley - download Hedley and regenerate hedley.hpp / hedley_undef.hpp"
|
|
@echo "update_hedley_undef - rebuild hedley_undef.hpp from the JSON_HEDLEY_* #define names in hedley.hpp"
|
|
|
|
|
|
##########################################################################
|
|
# benchmarks
|
|
##########################################################################
|
|
|
|
run_benchmarks:
|
|
rm -fr cmake-build-benchmarks
|
|
mkdir cmake-build-benchmarks
|
|
cd cmake-build-benchmarks ; cmake ../tests/benchmarks -GNinja -DCMAKE_BUILD_TYPE=Release
|
|
cd cmake-build-benchmarks ; ninja
|
|
cd cmake-build-benchmarks ; ./json_benchmarks
|
|
|
|
|
|
##########################################################################
|
|
# fuzzing
|
|
##########################################################################
|
|
|
|
# the overall fuzz testing target
|
|
fuzz_testing:
|
|
rm -fr fuzz-testing
|
|
mkdir -p fuzz-testing fuzz-testing/testcases fuzz-testing/out
|
|
$(MAKE) parse_afl_fuzzer -C tests CXX=afl-clang++
|
|
mv tests/parse_afl_fuzzer fuzz-testing/fuzzer
|
|
find tests/data/json_tests -size -5k -name *json | xargs -I{} cp "{}" fuzz-testing/testcases
|
|
@echo "Execute: afl-fuzz -i fuzz-testing/testcases -o fuzz-testing/out fuzz-testing/fuzzer"
|
|
|
|
fuzz_testing_bon8:
|
|
rm -fr fuzz-testing
|
|
mkdir -p fuzz-testing fuzz-testing/testcases fuzz-testing/out
|
|
$(MAKE) parse_bon8_fuzzer -C tests CXX=afl-clang++
|
|
mv tests/parse_bon8_fuzzer fuzz-testing/fuzzer
|
|
find tests/data -size -5k -name *.bon8 | xargs -I{} cp "{}" fuzz-testing/testcases
|
|
@echo "Execute: afl-fuzz -i fuzz-testing/testcases -o fuzz-testing/out fuzz-testing/fuzzer"
|
|
|
|
fuzz_testing_bson:
|
|
rm -fr fuzz-testing
|
|
mkdir -p fuzz-testing fuzz-testing/testcases fuzz-testing/out
|
|
$(MAKE) parse_bson_fuzzer -C tests CXX=afl-clang++
|
|
mv tests/parse_bson_fuzzer fuzz-testing/fuzzer
|
|
find tests/data -size -5k -name *.bson | xargs -I{} cp "{}" fuzz-testing/testcases
|
|
@echo "Execute: afl-fuzz -i fuzz-testing/testcases -o fuzz-testing/out fuzz-testing/fuzzer"
|
|
|
|
fuzz_testing_cbor:
|
|
rm -fr fuzz-testing
|
|
mkdir -p fuzz-testing fuzz-testing/testcases fuzz-testing/out
|
|
$(MAKE) parse_cbor_fuzzer -C tests CXX=afl-clang++
|
|
mv tests/parse_cbor_fuzzer fuzz-testing/fuzzer
|
|
find tests/data -size -5k -name *.cbor | xargs -I{} cp "{}" fuzz-testing/testcases
|
|
@echo "Execute: afl-fuzz -i fuzz-testing/testcases -o fuzz-testing/out fuzz-testing/fuzzer"
|
|
|
|
fuzz_testing_msgpack:
|
|
rm -fr fuzz-testing
|
|
mkdir -p fuzz-testing fuzz-testing/testcases fuzz-testing/out
|
|
$(MAKE) parse_msgpack_fuzzer -C tests CXX=afl-clang++
|
|
mv tests/parse_msgpack_fuzzer fuzz-testing/fuzzer
|
|
find tests/data -size -5k -name *.msgpack | xargs -I{} cp "{}" fuzz-testing/testcases
|
|
@echo "Execute: afl-fuzz -i fuzz-testing/testcases -o fuzz-testing/out fuzz-testing/fuzzer"
|
|
|
|
fuzz_testing_ubjson:
|
|
rm -fr fuzz-testing
|
|
mkdir -p fuzz-testing fuzz-testing/testcases fuzz-testing/out
|
|
$(MAKE) parse_ubjson_fuzzer -C tests CXX=afl-clang++
|
|
mv tests/parse_ubjson_fuzzer fuzz-testing/fuzzer
|
|
find tests/data -size -5k -name *.ubjson | xargs -I{} cp "{}" fuzz-testing/testcases
|
|
@echo "Execute: afl-fuzz -i fuzz-testing/testcases -o fuzz-testing/out fuzz-testing/fuzzer"
|
|
|
|
fuzzing-start:
|
|
afl-fuzz -S fuzzer1 -i fuzz-testing/testcases -o fuzz-testing/out fuzz-testing/fuzzer > /dev/null &
|
|
afl-fuzz -S fuzzer2 -i fuzz-testing/testcases -o fuzz-testing/out fuzz-testing/fuzzer > /dev/null &
|
|
afl-fuzz -S fuzzer3 -i fuzz-testing/testcases -o fuzz-testing/out fuzz-testing/fuzzer > /dev/null &
|
|
afl-fuzz -S fuzzer4 -i fuzz-testing/testcases -o fuzz-testing/out fuzz-testing/fuzzer > /dev/null &
|
|
afl-fuzz -S fuzzer5 -i fuzz-testing/testcases -o fuzz-testing/out fuzz-testing/fuzzer > /dev/null &
|
|
afl-fuzz -S fuzzer6 -i fuzz-testing/testcases -o fuzz-testing/out fuzz-testing/fuzzer > /dev/null &
|
|
afl-fuzz -S fuzzer7 -i fuzz-testing/testcases -o fuzz-testing/out fuzz-testing/fuzzer > /dev/null &
|
|
afl-fuzz -M fuzzer0 -i fuzz-testing/testcases -o fuzz-testing/out fuzz-testing/fuzzer
|
|
|
|
fuzzing-stop:
|
|
-killall fuzzer
|
|
-killall afl-fuzz
|
|
|
|
|
|
##########################################################################
|
|
# Static analysis
|
|
##########################################################################
|
|
|
|
# call PVS-Studio Analyzer <https://www.viva64.com/en/pvs-studio/>
|
|
pvs_studio:
|
|
rm -fr cmake-build-pvs-studio
|
|
mkdir cmake-build-pvs-studio
|
|
cd cmake-build-pvs-studio ; cmake .. -DCMAKE_EXPORT_COMPILE_COMMANDS=On -DJSON_MultipleHeaders=ON
|
|
cd cmake-build-pvs-studio ; pvs-studio-analyzer analyze -j 10
|
|
cd cmake-build-pvs-studio ; plog-converter -a'GA:1,2;64:1;CS' -t fullhtml PVS-Studio.log -o pvs
|
|
open cmake-build-pvs-studio/pvs/index.html
|
|
|
|
|
|
##########################################################################
|
|
# Code format and source amalgamation
|
|
##########################################################################
|
|
|
|
ASTYLE=tools/astyle/venv/bin/astyle
|
|
|
|
install_astyle:
|
|
@test -d tools/astyle/venv || python3 -mvenv tools/astyle/venv ; tools/astyle/venv/bin/pip3 install --quiet --upgrade pip
|
|
@test -f $(ASTYLE) || tools/astyle/venv/bin/pip3 install --quiet -r tools/astyle/requirements.txt
|
|
@$(ASTYLE) --version
|
|
|
|
# call the Artistic Style pretty printer on all source files
|
|
pretty: install_astyle
|
|
$(ASTYLE) --project=tools/astyle/.astylerc $(SRCS) $(TESTS_SRCS) $(AMALGAMATED_FILE) $(AMALGAMATED_FWD_FILE) docs/mkdocs/docs/examples/*.cpp
|
|
|
|
# call the Clang-Format on all source files
|
|
pretty_format:
|
|
for FILE in $(SRCS) $(TESTS_SRCS) $(AMALGAMATED_FILE) docs/mkdocs/docs/examples/*.cpp; do echo $$FILE; clang-format -i $$FILE; done
|
|
|
|
# create single header files and pretty print
|
|
amalgamate: $(AMALGAMATED_FILE) $(AMALGAMATED_FWD_FILE)
|
|
$(MAKE) pretty
|
|
|
|
# call the amalgamation tool for json.hpp
|
|
$(AMALGAMATED_FILE): $(SRCS)
|
|
tools/amalgamate/amalgamate.py -c tools/amalgamate/config_json.json -s . --verbose=yes
|
|
|
|
# call the amalgamation tool for json_fwd.hpp
|
|
$(AMALGAMATED_FWD_FILE): $(SRCS)
|
|
tools/amalgamate/amalgamate.py -c tools/amalgamate/config_json_fwd.json -s . --verbose=yes
|
|
|
|
# check if file single_include/nlohmann/json.hpp has been amalgamated from the nlohmann sources
|
|
# Note: this target is called by Travis
|
|
check-amalgamation:
|
|
@mv $(AMALGAMATED_FILE) $(AMALGAMATED_FILE)~
|
|
@mv $(AMALGAMATED_FWD_FILE) $(AMALGAMATED_FWD_FILE)~
|
|
@$(MAKE) amalgamate
|
|
@diff $(AMALGAMATED_FILE) $(AMALGAMATED_FILE)~ || (echo "===================================================================\n Amalgamation required! Please read the contribution guidelines\n in file .github/CONTRIBUTING.md.\n===================================================================" ; mv $(AMALGAMATED_FILE)~ $(AMALGAMATED_FILE) ; false)
|
|
@diff $(AMALGAMATED_FWD_FILE) $(AMALGAMATED_FWD_FILE)~ || (echo "===================================================================\n Amalgamation required! Please read the contribution guidelines\n in file .github/CONTRIBUTING.md.\n===================================================================" ; mv $(AMALGAMATED_FWD_FILE)~ $(AMALGAMATED_FWD_FILE) ; false)
|
|
@mv $(AMALGAMATED_FILE)~ $(AMALGAMATED_FILE)
|
|
@mv $(AMALGAMATED_FWD_FILE)~ $(AMALGAMATED_FWD_FILE)
|
|
@mv BUILD.bazel BUILD.bazel~
|
|
@$(MAKE) BUILD.bazel
|
|
@diff BUILD.bazel BUILD.bazel~ || (echo "===================================================================\n BUILD.bazel is out of date! Please run 'make BUILD.bazel'.\n===================================================================" ; mv BUILD.bazel~ BUILD.bazel ; false)
|
|
@mv BUILD.bazel~ BUILD.bazel
|
|
|
|
# generate the Bazel BUILD file; phony, because a removed header would not trigger a rebuild
|
|
BUILD.bazel:
|
|
cmake -P cmake/scripts/gen_bazel_build_file.cmake
|
|
|
|
##########################################################################
|
|
# ChangeLog
|
|
##########################################################################
|
|
|
|
# Create a ChangeLog based on the git log using the GitHub Changelog Generator
|
|
# (<https://github.com/github-changelog-generator/github-changelog-generator>).
|
|
|
|
# variable to control the diffs between the last released version and the current repository state
|
|
NEXT_VERSION ?= "unreleased"
|
|
|
|
ChangeLog.md:
|
|
github_changelog_generator -o ChangeLog.md --user nlohmann --project json --simple-list --release-url https://github.com/nlohmann/json/releases/tag/%s --future-release $(NEXT_VERSION)
|
|
$(SED) -i 's|https://github.com/nlohmann/json/releases/tag/HEAD|https://github.com/nlohmann/json/tree/HEAD|' ChangeLog.md
|
|
$(SED) -i '2i All notable changes to this project will be documented in this file. This project adheres to [Semantic Versioning](http://semver.org/).' ChangeLog.md
|
|
|
|
|
|
##########################################################################
|
|
# Release files
|
|
##########################################################################
|
|
|
|
# Create a tar.xz archive that contains sufficient files to be used as CMake project (e.g., using FetchContent). The
|
|
# archive is created according to the advices of <https://reproducible-builds.org/docs/archives/>.
|
|
json.tar.xz:
|
|
mkdir json
|
|
rsync -R $(shell find LICENSE.MIT nlohmann_json.natvis CMakeLists.txt cmake/*.in include single_include -type f) json
|
|
gtar --sort=name --mtime="@$(shell git log -1 --pretty=%ct)" --owner=0 --group=0 --numeric-owner --pax-option=exthdr.name=%d/PaxHeaders/%f,delete=atime,delete=ctime --create --file - json | xz --compress -9e --threads=2 - > json.tar.xz
|
|
rm -fr json
|
|
|
|
# We use `-X` to make the resulting ZIP file reproducible, see
|
|
# <https://content.pivotal.io/blog/barriers-to-deterministic-reproducible-zip-files>.
|
|
include.zip: BUILD.bazel
|
|
zip -9 --recurse-paths -X include.zip $(SRCS) $(AMALGAMATED_FILE) $(AMALGAMATED_FWD_FILE) BUILD.bazel MODULE.bazel meson.build LICENSE.MIT
|
|
|
|
# Create the files for a release and add signatures and hashes.
|
|
release: include.zip json.tar.xz
|
|
rm -fr release_files
|
|
mkdir release_files
|
|
gpg --armor --detach-sig include.zip
|
|
gpg --armor --detach-sig $(AMALGAMATED_FILE)
|
|
gpg --armor --detach-sig $(AMALGAMATED_FWD_FILE)
|
|
gpg --armor --detach-sig json.tar.xz
|
|
cp $(AMALGAMATED_FILE) release_files
|
|
cp $(AMALGAMATED_FWD_FILE) release_files
|
|
mv $(AMALGAMATED_FILE).asc $(AMALGAMATED_FWD_FILE).asc json.tar.xz json.tar.xz.asc include.zip include.zip.asc release_files
|
|
cd release_files ; shasum -a 256 json.hpp include.zip json.tar.xz > hashes.txt
|
|
|
|
|
|
##########################################################################
|
|
# Maintenance
|
|
##########################################################################
|
|
|
|
# clean up
|
|
clean:
|
|
rm -fr fuzz fuzz-testing *.dSYM tests/*.dSYM
|
|
rm -fr benchmarks/files/numbers/*.json
|
|
rm -fr cmake-build-benchmarks fuzz-testing cmake-build-pvs-studio release_files
|
|
$(MAKE) clean -Cdocs
|
|
|
|
|
|
##########################################################################
|
|
# Thirdparty code
|
|
##########################################################################
|
|
|
|
update_hedley:
|
|
rm -f include/nlohmann/thirdparty/hedley/hedley.hpp include/nlohmann/thirdparty/hedley/hedley_undef.hpp
|
|
curl https://raw.githubusercontent.com/nemequ/hedley/master/hedley.h -o include/nlohmann/thirdparty/hedley/hedley.hpp
|
|
$(SED) -i 's/HEDLEY_/JSON_HEDLEY_/g' include/nlohmann/thirdparty/hedley/hedley.hpp
|
|
$(SED) -i '1s/^/#pragma once\n\n/' include/nlohmann/thirdparty/hedley/hedley.hpp
|
|
$(MAKE) update_hedley_undef
|
|
$(MAKE) amalgamate
|
|
|
|
# Rebuild hedley_undef.hpp from every JSON_HEDLEY_* name that hedley.hpp
|
|
# #defines. Hedley does not #undef all of its public macros internally (see
|
|
# #5408), so grepping those #undef lines misses names such as
|
|
# JSON_HEDLEY_PRAGMA. cmake/scripts/gen_hedley_undef_check.cmake is the
|
|
# single source of truth for this extraction (tests/CMakeLists.txt uses the
|
|
# same script, in MODE=checks, to generate the matching leak-check test), so
|
|
# the vendored header, the generated #undef list, and the regression test
|
|
# cannot drift apart.
|
|
update_hedley_undef:
|
|
cmake -DHEDLEY_HPP=include/nlohmann/thirdparty/hedley/hedley.hpp \
|
|
-DOUTPUT=include/nlohmann/thirdparty/hedley/hedley_undef.hpp \
|
|
-DMODE=undef \
|
|
-P cmake/scripts/gen_hedley_undef_check.cmake
|
|
|
|
##########################################################################
|
|
# serve_header.py
|
|
##########################################################################
|
|
|
|
serve_header:
|
|
./tools/serve_header/serve_header.py --make $(MAKE)
|
|
|
|
##########################################################################
|
|
# REUSE
|
|
##########################################################################
|
|
|
|
reuse:
|
|
pipx run reuse annotate --recursive single_include include -tjson --license MIT --copyright "Niels Lohmann <https://nlohmann.me>" --year "2013-2026" --merge-copyrights
|
|
pipx run reuse annotate $(TESTS_SRCS) -tjson_support --license MIT --copyright "Niels Lohmann <https://nlohmann.me>" --year "2013-2026" --merge-copyrights
|
|
pipx run reuse lint
|
|
|
|
spdx:
|
|
pipx run reuse spdx --output nlohmann_json.spdx --creator-person "Niels Lohmann" --add-license-concluded
|