This commit is contained in:
nlohmann
2026-10-03 05:43:12 +00:00
parent 2aaa1d24ef
commit a410b4f40f
741 changed files with 8115 additions and 2252 deletions
+35 -2
View File
@@ -46,7 +46,7 @@ Strong guarantee: if an exception is thrown, there are no changes in the JSON va
## Exceptions
- Throws [`other_error.502`](https://json.nlohmann.me/home/exceptions/#jsonexceptionother_error502) if `use_type` is true and `use_size` is false.
- Throws [`other_error.502`](https://json.nlohmann.me/home/exceptions/#jsonexceptionother_error502) if `use_type` is true and `use_size` is false, and `j` contains a non-empty array, object, or binary value.
## Complexity
@@ -54,7 +54,7 @@ Linear in the size of the JSON value `j`.
## Examples
Example
Example: serialize a JSON value to BJData
The example shows the serialization of a JSON value to a byte vector in BJData format.
@@ -134,6 +134,39 @@ Output:
[$i#i812345678
```
Example: other_error.502 exception
The example shows how requesting type annotations (`use_type`) without size annotations (`use_size`) throws an exception, because type-optimized containers can only be read back with a preceding size.
```
#include <iostream>
#include <nlohmann/json.hpp>
using json = nlohmann::json;
int main()
{
// create a non-empty JSON array
json j = {1, 2, 3};
// exception other_error.502
try
{
json::to_bjdata(j, false, true);
}
catch (const json::other_error& e)
{
std::cout << e.what() << '\n';
}
}
```
Output:
```
[json.exception.other_error.502] use_type requires use_size = true
```
## See also
- [from_bjdata](https://json.nlohmann.me/api/basic_json/from_bjdata/index.md) create a JSON value from an input in BJData format