mirror of
https://github.com/nlohmann/json.git
synced 2026-09-12 11:17:58 +00:00
Complete documentation for 3.11.0 (#3464)
* 👥 update contributor and sponsor list * 🚧 document BJData format * 🚧 document BJData format * 📝 clarified documentation of [json.exception.parse_error.112] * ✏️ adjust titles * 📝 add more examples * 🚨 adjust warnings for index.md files * 📝 add more examples * 🔥 remove example for deprecated code * 📝 add missing enum entry * 📝 overwork table for binary formats * ✅ add test to create table for binary formats * 📝 fix wording in example * 📝 add more examples * Update iterators.md (#3481) * ✨ add check for overloads to linter #3455 * 👥 update contributor list * 📝 add more examples * 📝 fix documentation * 📝 add more examples * 🎨 fix indentation * 🔥 remove example for destructor * 📝 overwork documentation * Updated BJData documentation, #3464 (#3493) * update bjdata.md for #3464 * Minor edit * Fix URL typo * Add info on demoting ND array to a 1-D optimized array when singleton dimension Co-authored-by: Chaoqi Zhang <prncoprs@163.com> Co-authored-by: Qianqian Fang <fangqq@gmail.com>
This commit is contained in:
co-authored by
Chaoqi Zhang
Qianqian Fang
parent
a8a547d7a2
commit
6a7392058e
@@ -2,9 +2,11 @@
|
||||
|
||||
## Overview
|
||||
|
||||
The `#!cpp at()` member function performs checked access; that is, it returns a reference to the desired value if it exists and throws a [`basic_json::out_of_range` exception](../../home/exceptions.md#out-of-range) otherwise.
|
||||
The [`at`](../../api/basic_json/at.md) member function performs checked access; that is, it returns a reference to the
|
||||
desired value if it exists and throws a [`basic_json::out_of_range` exception](../../home/exceptions.md#out-of-range)
|
||||
otherwise.
|
||||
|
||||
??? example
|
||||
??? example "Read access"
|
||||
|
||||
Consider the following JSON value:
|
||||
|
||||
@@ -18,18 +20,18 @@ The `#!cpp at()` member function performs checked access; that is, it returns a
|
||||
|
||||
Assume the value is parsed to a `json` variable `j`.
|
||||
|
||||
| expression | value |
|
||||
| ---------- | ----- |
|
||||
| `#!cpp j` | `#!json {"name": "Mary Smith", "age": 42, "hobbies": ["hiking", "reading"]}` |
|
||||
| `#!cpp j.at("name")` | `#!json "Mary Smith"` |
|
||||
| `#!cpp j.at("age")` | `#!json 42` |
|
||||
| `#!cpp j.at("hobbies")` | `#!json ["hiking", "reading"]` |
|
||||
| `#!cpp j.at("hobbies").at(0)` | `#!json "hiking"` |
|
||||
| `#!cpp j.at("hobbies").at(1)` | `#!json "reading"` |
|
||||
| expression | value |
|
||||
|-------------------------------|------------------------------------------------------------------------------|
|
||||
| `#!cpp j` | `#!json {"name": "Mary Smith", "age": 42, "hobbies": ["hiking", "reading"]}` |
|
||||
| `#!cpp j.at("name")` | `#!json "Mary Smith"` |
|
||||
| `#!cpp j.at("age")` | `#!json 42` |
|
||||
| `#!cpp j.at("hobbies")` | `#!json ["hiking", "reading"]` |
|
||||
| `#!cpp j.at("hobbies").at(0)` | `#!json "hiking"` |
|
||||
| `#!cpp j.at("hobbies").at(1)` | `#!json "reading"` |
|
||||
|
||||
The return value is a reference, so it can be modified by the original value.
|
||||
|
||||
??? example
|
||||
??? example "Write access"
|
||||
|
||||
```cpp
|
||||
j.at("name") = "John Smith";
|
||||
@@ -45,9 +47,10 @@ The return value is a reference, so it can be modified by the original value.
|
||||
}
|
||||
```
|
||||
|
||||
When accessing an invalid index (i.e., an index greater than or equal to the array size) or the passed object key is non-existing, an exception is thrown.
|
||||
When accessing an invalid index (i.e., an index greater than or equal to the array size) or the passed object key is
|
||||
non-existing, an exception is thrown.
|
||||
|
||||
??? example
|
||||
??? example "Accessing via invalid index or missing key"
|
||||
|
||||
```cpp
|
||||
j.at("hobbies").at(3) = "cooking";
|
||||
@@ -59,13 +62,24 @@ When accessing an invalid index (i.e., an index greater than or equal to the arr
|
||||
[json.exception.out_of_range.401] array index 3 is out of range
|
||||
```
|
||||
|
||||
When you [extended diagnostic messages](../../home/exceptions.md#extended-diagnostic-messages) are enabled by
|
||||
defining [`JSON_DIAGNOSTICS`](../../api/macros/json_diagnostics.md), the exception further gives information where
|
||||
the key or index is missing or out of range.
|
||||
|
||||
```
|
||||
[json.exception.out_of_range.401] (/hobbies) array index 3 is out of range
|
||||
```
|
||||
|
||||
## Notes
|
||||
|
||||
|
||||
!!! failure "Exceptions"
|
||||
|
||||
- `at` can only be used with objects (with a string argument) or with arrays (with a numeric argument). For other types, a [`basic_json::type_error`](../../home/exceptions.md#jsonexceptiontype_error304) is thrown.
|
||||
- [`basic_json::out_of_range` exception](../../home/exceptions.md#out-of-range) exceptions are thrown if the provided key is not found in an object or the provided index is invalid.
|
||||
- [`at`](../../api/basic_json/at.md) can only be used with objects (with a string argument) or with arrays (with a
|
||||
numeric argument). For other types, a [`basic_json::type_error`](../../home/exceptions.md#jsonexceptiontype_error304)
|
||||
is thrown.
|
||||
- [`basic_json::out_of_range` exception](../../home/exceptions.md#out-of-range) exceptions are thrown if the
|
||||
provided key is not found in an object or the provided index is invalid.
|
||||
|
||||
## Summary
|
||||
|
||||
|
||||
Reference in New Issue
Block a user