Files
json/docs/mkdocs/docs/api/basic_json/std_swap.md
T
Niels Lohmann 29f1df793f Fix documentation errors and clean up repository leftovers
Documentation:
- basic_json_view::contains: basic_json::contains(json_pointer) never
  throws either; drop the claim that it does.
- std::format support is dated 3.13.0 in the serialization feature page;
  std::optional is supported since 3.12.0 and std::filesystem::path since
  3.10.5 in type_recipes.md.
- basic_json_view::source_offset: also -1 for values written by an edit
  and arrays/objects whose elements an edit added or removed.
- basic_json_view::items: const auto& works (binds to the temporary);
  by value is recommended, non-const auto& does not compile.
- basic_json_view::operator==/!=: document the overload that compares a
  read-only with an editable view.
- basic_json_document::erase: out_of_range.404 also for a last token "-".
- json_editable_document/json_editable_view: mention insert and erase.
- basic_json::swap/std::swap: describe the conditional noexcept (only a
  throwing move of json_base_class_t can make it throw) and that the
  std::swap specialization is not provided with C++20 (ADL swap is).
- exceptions.md: out_of_range.405 also from front/pop_front and from
  basic_json_document::erase with an empty pointer; other_error.502 also
  for non-empty binary values in UBJSON/BJData Draft 2, as the writer does.
- Macro pages: fix highlighted lines.

Tests:
- unit-unicode.cpp: enable the "incorrect sequences" sections that were
  disabled with #if 0 since 2017 without a reason. They pass; the reason
  was the runtime of the quadratic high/low surrogate sweep (1024 * 64512
  parses, not finished after 3 CPU minutes). That sweep is thinned to all
  high surrogates against boundary code units, plus one high surrogate
  against every code unit with accept(). The sections are merged into the
  existing "incorrect sequences" section of the same name.

Repository:
- labeler.yml: match the moved json_view feature docs folder.
- check_amalgamation.yml: take config_json_view.json from develop's tools.
- FILES.md: list json_view_benchmarks.yml.
- cmake/ci.cmake: remove the unused targets ci_test_amalgamation (a
  duplicate of make check-amalgamation), ci_benchmarks and ci_clean, and
  the .gitignore entry of the first.
- make clean and .gitignore cover all tests/*_fuzzer binaries.
- hedley_undef.hpp: regenerate with make update_hedley_undef (one line
  moves into sorted order); regenerate the amalgamation.

Signed-off-by: Niels Lohmann <mail@nlohmann.me>
2026-11-16 07:30:15 +01:00

1.7 KiB

std::swap<basic_json>

namespace std {
    void swap(nlohmann::basic_json& j1, nlohmann::basic_json& j2) noexcept(
        is_nothrow_move_constructible<nlohmann::basic_json>::value &&
        is_nothrow_move_assignable<nlohmann::basic_json>::value
    );
}

Exchanges the values of two JSON objects.

This specialization is only provided before C++20, which no longer allows specializing function templates in namespace std. With C++20 (and in generic code with any standard), use the idiom #!cpp using std::swap; swap(j1, j2);: it finds the swap friend function (overload 2) by argument-dependent lookup. A qualified #!cpp std::swap(j1, j2) still compiles with C++20, but uses the generic std::swap, which swaps by three moves instead of exchanging the contents.

Parameters

j1 (in, out)
value to be replaced by j2
j2 (in, out)
value to be replaced by j1

Exception safety

No-throw guarantee if the noexcept condition above holds: it holds unless moving the json_base_class_t can throw. With the default base class, this function never throws exceptions.

Complexity

Constant.

Possible implementation

void swap(nlohmann::basic_json& j1, nlohmann::basic_json& j2)
{
    j1.swap(j2);
}

Examples

??? example

The following code shows how two values are swapped with `std::swap`.
 
```cpp
--8<-- "examples/std_swap.cpp"
```

Output:

```json
--8<-- "examples/std_swap.output"
```

See also

  • swap - exchange the contents of two JSON values

Version history

  • Added in version 1.0.0.
  • Extended for arbitrary basic_json types in version 3.10.5.
  • Not provided with C++20 since version 3.9.0.