$ show nlohmann/json
nlohmann/json is a widely adopted, header-only C++ library for JSON parsing, serialization, and manipulation. It provides a single-header `json.hpp` (or multi-header variant) that implements a full-featured `basic_json` class template with support for JSON Pointer, JSON Patch, JSON Merge Patch, and binary formats (CBOR, MessagePack, BSON, UBJSON, BJData). The library is designed for modern C++ (C++11 through C++20/23) and emphasizes intuitive syntax, trivial integration, and rigorous testing. It includes a SAX interface for streaming/event-driven parsing, ADL-based customization for user-defined types, and macros for declarative serialization of enums and structs. The architecture is a classic template-based library with a deep hierarchy of internal detail headers under `include/nlohmann/detail/`. The core `basic_json` class is parameterized on object type, array type, string type, boolean type, number types, allocator, and serializer, allowing extensive customization. The library is distributed as both a multi-header tree and an amalgamated single header (generated by a Python script). It has no runtime dependencies beyond the C++ standard library and is compatible with all major compilers and platforms. Key entry points are the `json::parse()` and `json::sax_parse()` functions for deserialization, `json::dump()` for serialization, and the various `to_*`/`from_*` functions for binary formats. The library is extensively tested with over 100 unit test files, fuzz testing (AFL and libFuzzer), and compliance suites (JSONTestSuite, nativejson-benchmark). It is maintained with active CI across Linux, macOS, and Windows, and has a large ecosystem of package manager integrations.
The LLM identified 5 files worth highlighting for reuse:
A minimal, reusable map-like container that preserves insertion order. Implemented as a hybrid of a `std::vector` of key-value pairs and a `std::map` for O(log n) lookups. It provides the same interface as `std::map` (including `find`, `count`, `operator[]`, `emplace`, `erase`) but iterates in insertion order. This is used internally by `ordered_json` and can be extracted for any project needing an ordered associative container without external dependencies.
Provides a customization point for user-defined type serialization via ADL (argument-dependent lookup). The `adl_serializer` template can be specialized to define `to_json` and `from_json` functions for any type, enabling seamless integration with `basic_json` without modifying the library. This pattern is reusable in any C++ serialization framework that wants to support user types without intrusive changes.
Defines the `json_sax` abstract interface for event-driven JSON parsing, with callbacks for `null`, `boolean`, `number_integer`, `number_unsigned`, `number_float`, `string`, `start_object`, `key`, `end_object`, `start_array`, `end_array`, `binary`, and `parse_error`. Also includes two concrete SAX-based DOM parsers (`json_sax_dom_parser` and `json_sax_dom_callback_parser`) that build a `basic_json` value from SAX events. This pattern is reusable for any streaming JSON processing (e.g., filtering, incremental parsing, custom DOM construction).
Implements the Grisu2 algorithm for converting IEEE-754 double-precision floats to the shortest decimal representation that round-trips exactly. Includes a cached powers-of-ten table, digit generation, and rounding logic. This is a self-contained, high-performance float-to-string conversion that can be extracted and used in any C++ project needing locale-independent, fast float formatting without dependencies on `std::to_chars` (which may not be available in C++11/14).
A standalone Python script that merges multiple C/C++ header files into a single header file by resolving `#include` directives and `#pragma once` guards. It handles comments, strings, and conditional includes, and is configurable via a JSON file. This tool is not specific to nlohmann/json and can be reused by any C/C++ project that wants to distribute a single-header version of a multi-header library. It demonstrates a clean, well-documented approach to source amalgamation.