mirror of
https://github.com/nlohmann/json.git
synced 2026-07-12 13:35:13 +00:00
Add AST-based public API checker and fix documentation gaps it found (#3691)
Adds tools/api_checker/: extract_api.py derives the public API surface directly from the libclang AST (independent of documentation status), check_docs.py flags public entries missing @sa links (and @sa on non-public ones), diff_api.py does an overload-aware breaking/feature diff between two refs, check_macros.py cross- checks documented macros against #define sites, and snapshot_release.py backfills immutable per-release surface snapshots into tools/api_checker/history/ (v3.1.0 through v3.12.0) so diff_api.py can compare releases without live extraction. POLICY.md documents what counts as public API and what stability is guaranteed. Running this tooling against the current tree found and fixed a real documentation backlog: ~25 new API doc pages (ordered_map's methods, json_sax's ctor/dtor/ operator=, byte_container_with_subtype's comparison operators, several orphaned type aliases), each with a compiled and output-verified example, plus missing @sa comments and stale/incorrect Version History entries on several existing pages (found by diffing consecutive release pairs and checking whether the resulting change was actually reflected in the target page's history section). Also adds docs/home/api_changes.md, a per-release, per-function reference of public API changes (v3.1.0 through v3.12.0) generated from the history/ snapshots, complementing (not replacing) the existing release notes. Along the way, found and fixed several extractor bugs by testing against real release tags rather than trusting the algorithm in isolation -- most notably an identity-key scheme based on libclang's USR that encoded the enclosing class template's own arity, and a since-renamed ABI inline-namespace pattern (json_v3_11_0 vs. today's json_abi_v3_11_2) that neither of two earlier regex attempts stripped correctly. Both are documented in extract_api.py's docstrings and tools/api_checker/history/README.md so the failure mode doesn't recur silently. .github/workflows/check_api_docs.yml runs extract_api.py + check_docs.py in CI, advisory-only for now (documented backlog may not be at zero for entities this PR didn't touch), plus a blocking drift check on the committed tools/api_checker/api_surface.json. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Signed-off-by: Niels Lohmann <mail@nlohmann.me>
This commit is contained in:
@@ -0,0 +1,21 @@
|
||||
#include <iostream>
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
using json = nlohmann::json;
|
||||
|
||||
int main()
|
||||
{
|
||||
// an empty binary value is encoded differently by the two drafts:
|
||||
// draft2 omits the optimized type marker for an empty byte array,
|
||||
// while draft3 always writes it
|
||||
json j = json::binary({});
|
||||
|
||||
// encode using BJData draft2 (the default)
|
||||
auto v_draft2 = json::to_bjdata(j, true, true, json::bjdata_version_t::draft2);
|
||||
|
||||
// encode using BJData draft3
|
||||
auto v_draft3 = json::to_bjdata(j, true, true, json::bjdata_version_t::draft3);
|
||||
|
||||
std::cout << "draft2 size: " << v_draft2.size() << '\n'
|
||||
<< "draft3 size: " << v_draft3.size() << std::endl;
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
draft2 size: 4
|
||||
draft3 size: 6
|
||||
@@ -0,0 +1,11 @@
|
||||
#include <iostream>
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
using byte_container_with_subtype = nlohmann::byte_container_with_subtype<std::vector<std::uint8_t>>;
|
||||
|
||||
int main()
|
||||
{
|
||||
std::cout << std::boolalpha
|
||||
<< std::is_same<byte_container_with_subtype::container_type, std::vector<std::uint8_t>>::value
|
||||
<< std::endl;
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
true
|
||||
@@ -0,0 +1,15 @@
|
||||
#include <iostream>
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
using byte_container_with_subtype = nlohmann::byte_container_with_subtype<std::vector<std::uint8_t>>;
|
||||
|
||||
int main()
|
||||
{
|
||||
byte_container_with_subtype c1({0xca, 0xfe});
|
||||
byte_container_with_subtype c2({0xca, 0xfe});
|
||||
byte_container_with_subtype c3({0xca, 0xfe}, 42);
|
||||
|
||||
std::cout << std::boolalpha
|
||||
<< "c1 == c2: " << (c1 == c2) << '\n'
|
||||
<< "c1 == c3: " << (c1 == c3) << std::endl;
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
c1 == c2: true
|
||||
c1 == c3: false
|
||||
@@ -0,0 +1,15 @@
|
||||
#include <iostream>
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
using byte_container_with_subtype = nlohmann::byte_container_with_subtype<std::vector<std::uint8_t>>;
|
||||
|
||||
int main()
|
||||
{
|
||||
byte_container_with_subtype c1({0xca, 0xfe});
|
||||
byte_container_with_subtype c2({0xca, 0xfe});
|
||||
byte_container_with_subtype c3({0xca, 0xfe}, 42);
|
||||
|
||||
std::cout << std::boolalpha
|
||||
<< "c1 != c2: " << (c1 != c2) << '\n'
|
||||
<< "c1 != c3: " << (c1 != c3) << std::endl;
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
c1 != c2: false
|
||||
c1 != c3: true
|
||||
@@ -0,0 +1,10 @@
|
||||
#include <iostream>
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
using byte_container_with_subtype = nlohmann::byte_container_with_subtype<std::vector<std::uint8_t>>;
|
||||
|
||||
int main()
|
||||
{
|
||||
std::cout << std::boolalpha
|
||||
<< std::is_same<byte_container_with_subtype::subtype_type, std::uint64_t>::value << std::endl;
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
true
|
||||
@@ -0,0 +1,13 @@
|
||||
#include <iostream>
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
using json = nlohmann::json;
|
||||
|
||||
int main()
|
||||
{
|
||||
// an initializer_list_t is what a braced-init-list of JSON values is deduced as
|
||||
json::initializer_list_t init = {"a", 1, 2.0, false};
|
||||
|
||||
json j(init);
|
||||
std::cout << j.dump() << std::endl;
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
["a",1,2.0,false]
|
||||
@@ -0,0 +1,10 @@
|
||||
#include <iostream>
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
using json = nlohmann::json;
|
||||
|
||||
int main()
|
||||
{
|
||||
std::cout << std::boolalpha
|
||||
<< std::is_same<json::json_sax_t::binary_t, json::binary_t>::value << std::endl;
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
true
|
||||
@@ -0,0 +1,10 @@
|
||||
#include <iostream>
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
using json = nlohmann::json;
|
||||
|
||||
int main()
|
||||
{
|
||||
std::cout << std::boolalpha
|
||||
<< std::is_same<json::json_sax_t::number_float_t, json::number_float_t>::value << std::endl;
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
true
|
||||
@@ -0,0 +1,10 @@
|
||||
#include <iostream>
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
using json = nlohmann::json;
|
||||
|
||||
int main()
|
||||
{
|
||||
std::cout << std::boolalpha
|
||||
<< std::is_same<json::json_sax_t::number_integer_t, json::number_integer_t>::value << std::endl;
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
true
|
||||
@@ -0,0 +1,10 @@
|
||||
#include <iostream>
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
using json = nlohmann::json;
|
||||
|
||||
int main()
|
||||
{
|
||||
std::cout << std::boolalpha
|
||||
<< std::is_same<json::json_sax_t::number_unsigned_t, json::number_unsigned_t>::value << std::endl;
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
true
|
||||
@@ -0,0 +1,10 @@
|
||||
#include <iostream>
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
using json = nlohmann::json;
|
||||
|
||||
int main()
|
||||
{
|
||||
std::cout << std::boolalpha
|
||||
<< std::is_same<json::json_sax_t::string_t, json::string_t>::value << std::endl;
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
true
|
||||
@@ -0,0 +1,10 @@
|
||||
#include <iostream>
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
using json = nlohmann::json;
|
||||
|
||||
int main()
|
||||
{
|
||||
std::cout << std::boolalpha
|
||||
<< std::is_same<json::json_sax_t, nlohmann::json_sax<json>>::value << std::endl;
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
true
|
||||
@@ -0,0 +1,12 @@
|
||||
#include <iostream>
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
int main()
|
||||
{
|
||||
using Map = nlohmann::ordered_map<std::string, int>;
|
||||
|
||||
std::cout << std::boolalpha
|
||||
<< "Container is std::vector<std::pair<const Key, T>>: "
|
||||
<< std::is_same<Map::Container, std::vector<std::pair<const std::string, int>>>::value
|
||||
<< std::endl;
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
Container is std::vector<std::pair<const Key, T>>: true
|
||||
@@ -0,0 +1,26 @@
|
||||
#include <iostream>
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
int main()
|
||||
{
|
||||
nlohmann::ordered_map<std::string, int> m;
|
||||
m["one"] = 1;
|
||||
m["two"] = 2;
|
||||
|
||||
// access an existing element
|
||||
std::cout << "m.at(\"one\") = " << m.at("one") << std::endl;
|
||||
|
||||
// modify through the reference returned by at()
|
||||
m.at("two") = 22;
|
||||
std::cout << "m.at(\"two\") = " << m.at("two") << std::endl;
|
||||
|
||||
// accessing a missing key throws
|
||||
try
|
||||
{
|
||||
m.at("three");
|
||||
}
|
||||
catch (const std::out_of_range& e)
|
||||
{
|
||||
std::cout << "exception: " << e.what() << std::endl;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
m.at("one") = 1
|
||||
m.at("two") = 22
|
||||
exception: key not found
|
||||
@@ -0,0 +1,12 @@
|
||||
#include <iostream>
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
int main()
|
||||
{
|
||||
nlohmann::ordered_map<std::string, int> m;
|
||||
m["one"] = 1;
|
||||
|
||||
std::cout << std::boolalpha
|
||||
<< "m.count(\"one\") = " << m.count("one") << '\n'
|
||||
<< "m.count(\"two\") = " << m.count("two") << std::endl;
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
m.count("one") = 1
|
||||
m.count("two") = 0
|
||||
@@ -0,0 +1,15 @@
|
||||
#include <iostream>
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
int main()
|
||||
{
|
||||
nlohmann::ordered_map<std::string, std::string> m;
|
||||
|
||||
// emplace a new element
|
||||
auto res1 = m.emplace("one", "eins");
|
||||
std::cout << std::boolalpha << "inserted: " << res1.second << ", value: " << res1.first->second << std::endl;
|
||||
|
||||
// emplace with an already-existing key: no-op, returns the existing element
|
||||
auto res2 = m.emplace("one", "uno");
|
||||
std::cout << std::boolalpha << "inserted: " << res2.second << ", value: " << res2.first->second << std::endl;
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
inserted: true, value: eins
|
||||
inserted: false, value: eins
|
||||
@@ -0,0 +1,24 @@
|
||||
#include <iostream>
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
int main()
|
||||
{
|
||||
nlohmann::ordered_map<std::string, int> m;
|
||||
m["one"] = 1;
|
||||
m["two"] = 2;
|
||||
m["three"] = 3;
|
||||
|
||||
// erase by key
|
||||
std::size_t removed = m.erase("two");
|
||||
std::cout << "removed by key: " << removed << std::endl;
|
||||
|
||||
// erase by iterator
|
||||
m.erase(m.begin());
|
||||
|
||||
std::cout << "remaining: ";
|
||||
for (const auto& element : m)
|
||||
{
|
||||
std::cout << element.first << ' ';
|
||||
}
|
||||
std::cout << std::endl;
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
removed by key: 1
|
||||
remaining: three
|
||||
@@ -0,0 +1,19 @@
|
||||
#include <iostream>
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
int main()
|
||||
{
|
||||
nlohmann::ordered_map<std::string, int> m;
|
||||
m["one"] = 1;
|
||||
|
||||
auto it = m.find("one");
|
||||
if (it != m.end())
|
||||
{
|
||||
std::cout << "found: " << it->first << " = " << it->second << std::endl;
|
||||
}
|
||||
|
||||
if (m.find("two") == m.end())
|
||||
{
|
||||
std::cout << "\"two\" not found" << std::endl;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
found: one = 1
|
||||
"two" not found
|
||||
@@ -0,0 +1,21 @@
|
||||
#include <iostream>
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
int main()
|
||||
{
|
||||
nlohmann::ordered_map<std::string, int> m;
|
||||
|
||||
// insert a single value
|
||||
auto res = m.insert({"one", 1});
|
||||
std::cout << std::boolalpha << "inserted: " << res.second << std::endl;
|
||||
|
||||
// insert a range from another container
|
||||
std::vector<std::pair<const std::string, int>> more = {{"two", 2}, {"three", 3}};
|
||||
m.insert(more.begin(), more.end());
|
||||
|
||||
for (const auto& element : m)
|
||||
{
|
||||
std::cout << element.first << ':' << element.second << ' ';
|
||||
}
|
||||
std::cout << std::endl;
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
inserted: true
|
||||
one:1 two:2 three:3
|
||||
@@ -0,0 +1,12 @@
|
||||
#include <iostream>
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
int main()
|
||||
{
|
||||
using Map = nlohmann::ordered_map<std::string, int>;
|
||||
Map::key_compare compare{};
|
||||
|
||||
std::cout << std::boolalpha
|
||||
<< "compare(\"a\", \"a\") = " << compare("a", "a") << '\n'
|
||||
<< "compare(\"a\", \"b\") = " << compare("a", "b") << std::endl;
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
compare("a", "a") = true
|
||||
compare("a", "b") = false
|
||||
@@ -0,0 +1,14 @@
|
||||
#include <iostream>
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
int main()
|
||||
{
|
||||
nlohmann::ordered_map<std::string, int> m;
|
||||
|
||||
// operator[] inserts a default-constructed value if the key doesn't exist yet
|
||||
m["one"] = 1;
|
||||
std::cout << "m[\"one\"] = " << m["one"] << std::endl;
|
||||
|
||||
// accessing again just returns the existing value
|
||||
std::cout << "m[\"one\"] = " << m["one"] << std::endl;
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
m["one"] = 1
|
||||
m["one"] = 1
|
||||
Reference in New Issue
Block a user