This commit is contained in:
nlohmann committed 2026-10-12 07:35:31 +00:00
1 parent 79119509a9
commit 22fba27145
555 files changed
+17342 -1394

No files matched your search

+34 -2
View File
@@ -44,13 +44,45 @@ the initializer list constructor `basic_json(initializer_list_t, bool, value_t)`
The following code shows an example for the `object` function.
```cpp
--8<-- "examples/object.cpp"
#include <iostream>
#include <nlohmann/json.hpp>
using json = nlohmann::json;
int main()
{
// create JSON objects
json j_no_init_list = json::object();
json j_empty_init_list = json::object({});
json j_list_of_pairs = json::object({ {"one", 1}, {"two", 2} });
// serialize the JSON objects
std::cout << j_no_init_list << '\n';
std::cout << j_empty_init_list << '\n';
std::cout << j_list_of_pairs << '\n';
// example for an exception
try
{
// can only create an object from a list of pairs
json j_invalid_object = json::object({{ "one", 1, 2 }});
}
catch (const json::type_error& e)
{
std::cout << e.what() << '\n';
}
}
```
Output:
```json
--8<-- "examples/object.output"
{}
{}
{"one":1,"two":2}
[json.exception.type_error.301] cannot create object from initializer list
```
## See also