From b5967d0ff9e816631824edf7d49f4e7096be651c Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Wed, 11 Nov 2026 22:32:29 +0100 Subject: [PATCH] Export the editing API of json_view from the C++20 module (#6090) Signed-off-by: Niels Lohmann --- docs/mkdocs/docs/features/modules.md | 7 +++++++ src/modules/json.cppm | 6 ++++++ tests/module_cpp20/main.cpp | 19 ++++++++++++++++++- 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/docs/mkdocs/docs/features/modules.md b/docs/mkdocs/docs/features/modules.md index 780809f08..9b3b7acd0 100644 --- a/docs/mkdocs/docs/features/modules.md +++ b/docs/mkdocs/docs/features/modules.md @@ -36,15 +36,21 @@ Only the following symbols are exported from `nlohmann.json`: - `nlohmann::basic_json` - `nlohmann::basic_json_document` - `nlohmann::basic_json_view` +- `nlohmann::is_json_view` - `nlohmann::json` - `nlohmann::json_document` +- `nlohmann::json_editable_document` +- `nlohmann::json_editable_view` - `nlohmann::json_pointer` - `nlohmann::json_view` - `nlohmann::ordered_json` - `nlohmann::ordered_json_document` +- `nlohmann::ordered_json_editable_document` +- `nlohmann::ordered_json_editable_view` - `nlohmann::ordered_json_view` - `nlohmann::ordered_map` - `nlohmann::to_string` +- `nlohmann::view_mode` - `nlohmann::literals::json_literals::operator""_json` - `nlohmann::literals::json_literals::operator""_json_pointer` @@ -56,6 +62,7 @@ Additionally, the following `nlohmann::detail` symbols are exported, solely to w and should not be used directly: - `nlohmann::detail::json_sax_dom_callback_parser` +- `nlohmann::detail::json_sax_dom_parser` - `nlohmann::detail::unknown_size` ## Known issues diff --git a/src/modules/json.cppm b/src/modules/json.cppm index 3917e0722..1108113bf 100644 --- a/src/modules/json.cppm +++ b/src/modules/json.cppm @@ -36,15 +36,21 @@ using NLOHMANN_JSON_NAMESPACE::adl_serializer; using NLOHMANN_JSON_NAMESPACE::basic_json; using NLOHMANN_JSON_NAMESPACE::basic_json_document; using NLOHMANN_JSON_NAMESPACE::basic_json_view; +using NLOHMANN_JSON_NAMESPACE::is_json_view; using NLOHMANN_JSON_NAMESPACE::json; using NLOHMANN_JSON_NAMESPACE::json_document; +using NLOHMANN_JSON_NAMESPACE::json_editable_document; +using NLOHMANN_JSON_NAMESPACE::json_editable_view; using NLOHMANN_JSON_NAMESPACE::json_pointer; using NLOHMANN_JSON_NAMESPACE::json_view; using NLOHMANN_JSON_NAMESPACE::ordered_json; using NLOHMANN_JSON_NAMESPACE::ordered_json_document; +using NLOHMANN_JSON_NAMESPACE::ordered_json_editable_document; +using NLOHMANN_JSON_NAMESPACE::ordered_json_editable_view; using NLOHMANN_JSON_NAMESPACE::ordered_json_view; using NLOHMANN_JSON_NAMESPACE::ordered_map; using NLOHMANN_JSON_NAMESPACE::to_string; +using NLOHMANN_JSON_NAMESPACE::view_mode; inline namespace literals { diff --git a/tests/module_cpp20/main.cpp b/tests/module_cpp20/main.cpp index 4e492cef9..a475c8bff 100644 --- a/tests/module_cpp20/main.cpp +++ b/tests/module_cpp20/main.cpp @@ -56,9 +56,26 @@ int main() const nlohmann::json_document doc = nlohmann::json_document::parse(R"({"a": 1, "list": [1, 2, 3]})"); const std::size_t doc_size = doc.root().size(); + // editable documents and views, view_mode, is_json_view and the ordered variants + nlohmann::json_editable_document edoc = nlohmann::json_editable_document::parse(R"({"a": 1, "list": [1, 2, 3]})"); + edoc.set(edoc.root(), "b", 2); + edoc.push_back(edoc.root()["list"], 4); + const nlohmann::json_editable_view eroot = edoc.root(); + const std::size_t edit_size = eroot.size(); + const std::size_t list_size = eroot["list"].size(); + nlohmann::ordered_json_editable_document oedoc = nlohmann::ordered_json_editable_document::parse(R"({"z": 1, "y": 2})"); + oedoc.erase(oedoc.root(), "z"); + const nlohmann::ordered_json_editable_view oeroot = oedoc.root(); + nlohmann::basic_json_document mdoc = nlohmann::basic_json_document::parse("[]"); + mdoc.push_back(mdoc.root(), 1); + static_assert(nlohmann::is_json_view::value, "json_view is a view"); + static_assert(nlohmann::is_json_view::value, "json_editable_view is a view"); + static_assert(!nlohmann::is_json_view::value, "json is not a view"); + // use every result so the references cannot be optimized away return (a == 1 && last == 3 && b == 2 && lit.size() == 3 && m.size() == 1 && !dumped.empty() && !os.str().empty() - && doc_size == 2) + && doc_size == 2 + && edit_size == 3 && list_size == 4 && oeroot.size() == 1 && mdoc.root().size() == 1) ? 0 : 1; }