mirror of
https://github.com/nlohmann/json.git
synced 2026-10-04 05:30:31 +00:00
deploy: 63c10a51fc
This commit is contained in:
File diff suppressed because one or more lines are too long
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user