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.
j1 (in, out) : value to be replaced by j2
j2 (in, out) : value to be replaced by j1
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.
Constant.
void swap(nlohmann::basic_json& j1, nlohmann::basic_json& j2) { j1.swap(j2); }
??? example
The following code shows how two values are swapped with `std::swap`.
```cpp
#include <iostream>
#include <iomanip>
#include <nlohmann/json.hpp>
using nlohmann::json;
int main()
{
// create JSON values
json j1 = {{"one", 1}, {"two", 2}};
json j2 = {1, 2, 4, 8, 16};
std::cout << "j1 = " << j1 << " | j2 = " << j2 << '\n';
// swap values
std::swap(j1, j2);
std::cout << "j1 = " << j1 << " | j2 = " << j2 << '\n';
}
```
Output:
```json
j1 = {"one":1,"two":2} | j2 = [1,2,4,8,16]
j1 = [1,2,4,8,16] | j2 = {"one":1,"two":2}
```
swap - exchange the contents of two JSON values