With JSON_DIAGNOSTICS and JSON_DIAGNOSTIC_POSITIONS, the exceptions of views now carry the same context as those of basic_json: "(/path) " and "(bytes a-b) " in front of the message, in basic_json's format (with JSON_DIAGNOSTICS, no context for the root). Before, views threw without any context, and since get<T>() reads the view directly, conversions had lost even the relative path they used to get through materialize(). The throw helpers of detail/view/errors.hpp take an error_site (document and node; empty without the two macros). Only on the error path, the path is found by descending from the root (pruned by subtree extent before the first edit, a full walk through edited sequences after), and the bytes as materialize() sets them (position_source; a container's end by following the chain of last elements and scanning to the closing brackets). from_json() for views and the strict enum macro put the same context in front through exception_prefix(). Iterators keep their container only under the two macros, for key(). Without the macros, the accessors compile to the same code as before. Tests compare every value's exceptions with those of the parsed or materialized basic_json value, including edited documents; the checks that were disabled under the two macros run again. Signed-off-by: Niels Lohmann <mail@nlohmann.me>
2.3 KiB
nlohmann::basic_json_view::back
basic_json_view back() const;
Returns the last element of an array, the last member value of an object, or the value itself if it is primitive
(as for BasicJsonType::back(), a primitive value is a range of one element).
Return value
The last element or member value. For a primitive value (number, string, boolean), the value itself.
Exception safety
Strong guarantee: there are no changes to the view or the document it refers to.
Exceptions
Throws invalid_iterator.214 if the view is
null or discarded, or if it is an empty array or object.
As for BasicJsonType, with JSON_DIAGNOSTICS the exception names the JSON Pointer
path of the value within the document, and with JSON_DIAGNOSTIC_POSITIONS
its bytes in the source text (where materialize() sets them).
Complexity
Linear in the size of the array or object: unlike front(), which only ever looks at the first
element, back() has to walk every element to find where they end, since elements are not a fixed size in the
index. Constant if no element of the array is an array or object, since the elements are then adjacent
in the index.
Notes
Unlike BasicJsonType::back(), which has undefined behavior for an empty array or object,
back() throws invalid_iterator.214 for an empty array, an empty object, a null value, or a discarded view.
Examples
??? example
The example below reads only the final status of a build log with `back()`. Even though `back()` is linear in
the number of events (unlike [`front()`](front.md), which is constant), it still avoids building a
`BasicJsonType` value for the events that are not needed.
```cpp
--8<-- "examples/basic_json_view__back.cpp"
```
Output:
```json
--8<-- "examples/basic_json_view__back.output"
```
See also
front- access the first elementBasicJsonType::back- the corresponding function ofbasic_json
Version history
- Added in version 3.13.0.