* docs: new design for the documentation site Apply the brand to the Material for MkDocs site: Inter and JetBrains Mono served by the site (upright and real italic), brand colours, blue header band with the syntax pattern, wordmark in the header, new home page (hero, cards, animated feature tour), 404 page, polish for tabs, search, footer, focus rings, tables and code blocks, and theme features (code copy, previous/next footer, TOC follow, search suggestions). Draw the number railroad diagram, the parser callback events and the iterator illustrations as inline SVGs that follow the light and dark scheme. The generator scripts are in docs/mkdocs/scripts/figures; the feature slideshow (json-light.svg, json-dark.svg, replacing json.gif on the home page) is built by docs/mkdocs/scripts/slideshow. Decision nodes in five flowcharts become hexagons, which need far less room. Signed-off-by: Niels Lohmann <mail@nlohmann.me> * docs: add figures, table captions and a macro chooser - Figures with numbered captions: SAX stepper, json_view layout and edit stepper, exception map, JSON pointer and patch walkers, QA pipeline, scaling and binary size charts, roadmap lanes, release timeline, integration decision tree and namespace builder - Table captions (nj-table-caption) on 9 pages - Compile times: modules and PCH charts, filesystem section, re-measured Figure 1, links between features/modules.md and compile_times.md - Interactive macro chooser for features/arbitrary_types.md - Link check: exclude URLs that answer the plugin with 400/401/419 or are dead (customers.md) Signed-off-by: Niels Lohmann <mail@nlohmann.me> * docs: macro chooser generates adl_serializer for non-default-constructible types Signed-off-by: Niels Lohmann <mail@nlohmann.me> * docs: cross-link concept pages and API pages in See also sections Add See also links in both directions between the concept pages (features, home, integration) and the API pages, write API links in code font, give every entry a comment after a dash, and show the callback event figure on the parser callbacks page. Signed-off-by: Niels Lohmann <mail@nlohmann.me> * docs: address review comments on the new design - Home: add a "Latest release" button - Sponsors: update the list and group it by tier - Features: list all feature pages on the index page - Releases: add highlights per release, link the 4.0 roadmap, add the first commit to the timeline - Exceptions: prefix all example messages with the exception name - API: no code font for format names in the See also links of the to_/from_ pages - Merge Patch: show the document before, the patch, and the result - Replace "--" with typographic dashes, mark external links with an icon, center Mermaid diagrams Signed-off-by: Niels Lohmann <mail@nlohmann.me> --------- Signed-off-by: Niels Lohmann <mail@nlohmann.me>
6.1 KiB
nlohmann::basic_json_document::erase
// (1)
std::size_t erase(view_type object, string_view_t key);
// (2)
template<typename I>
void erase(view_type array, I idx);
// (3)
std::size_t erase(const json_pointer& ptr);
Only an editable document (#!cpp view_mode::editable, e.g. json_editable_document)
has erase; calling it on a read-only basic_json_document fails to compile (#!cpp static_assert).
- Removes every member of
objectwhose key iskey(see Notes on duplicate keys) and returns how many were removed;#!cpp 0ifobjecthas no member with this key. - Removes the element at index
idxofarray, which must already exist (#!cpp idx < array.size()). - Removes the value the JSON pointer
ptrrefers to, relative toroot().ptrmust not be empty (root()itself cannot be erased), and the parent of the target must already exist. The target is removed as in 1. if the parent is an object (returns the number of removed members,#!cpp 0or more) and as in 2. if it is an array (always returns#!cpp 1).
Template parameters
I- an integral type other than
#!cpp bool, deduced (overloads taking a#!cpp boolor a non-integral type foridxdo not participate in overload resolution).
Parameters
object(in)- the object to remove a member of
array(in)- the array to remove an element of
key(in)- the key of the member(s) to remove
idx(in)- the index of the element to remove; a negative value throws (see Exceptions)
ptr(in)- a JSON pointer to the value to remove, relative to
root()
Return value
- the number of removed members (
#!cpp 0ifobjecthad none with thiskey) - (nothing)
- the number of removed values (
#!cpp 0or more for an object member, always#!cpp 1for an array element)
Exceptions
- Throws
type_error.307ifobjectis not an object — the same messageBasicJsonType::erasethrows for the same type. - Throws
type_error.307ifarrayis not an array. Throwsout_of_range.401ifidxis negative, or if#!cpp idx >= array.size(). - Throws
out_of_range.405("JSON pointer has no parent") ifptris empty. Throws whatatthrows (overload 3) for resolvingptr's parent. For the last reference token itself: if the parent is an array, throws what 2. throws for an index that is out of range, or, for a token that is not a valid array index,parse_error.106(a leading#!cpp '0'),parse_error.109(not a number),out_of_range.410(too large forsize_type), orout_of_range.404(an empty token); otherwise (an object, or a primitive value the pointer's parent resolves to) throws what 1. throws.
Overloads 1. and 2. also throw invalid_iterator.202
("view does not belong to this document") if object/array is a discarded
view or a view of a different document. Overload 3. never throws invalid_iterator.202: it takes no view, and it
always starts from this document's own root().
Complexity
- Linear in the number of members of
object. - Linear in the number of elements of
arrayat or afteridx(they move one slot over). - Linear in the number of reference tokens of
ptrand, for each token, in the number of members of the object at that level or the index into the array (asat), plus the complexity of 1. or 2. for the last token.
Notes
!!! info "Duplicate keys"
Overload 1. removes *every* member with `key`, not just the first — unlike [`set`](set.md), which assigns the
first occurrence and drops the rest. This is why it returns a count rather than a single view: there may be
more than one member removed, or none.
Like set and push_back, erase never moves an element's value: a view still
referring to a removed member or element keeps showing what it last held (see Edits) — it just no
longer appears when array/object is read, dumped, or iterated. What does change is the container's sequence of
links to its elements: removing an element of array (2.) shifts the links to the elements after it by one, as
insert does in the other direction. Every iterator already taken over array/object is invalidated
by an erase, not only those at or after the removed element, since the container may now use a different sequence.
Examples
??? example
The example below drops a deprecated field and a decommissioned entry from a configuration document — using all
three overloads — and shows what stays intact that would not with a plain `json`/`ordered_json` value: the order
of the fields around the ones removed, and the exact spelling of a number that was never touched.
```cpp
--8<-- "examples/basic_json_document__erase.cpp"
```
Output:
```json
--8<-- "examples/basic_json_document__erase.output"
```
See also
insert- insert an element into an arrayset- replace a value, or set an object member, an array element, or the value a JSON pointer refers topush_back- append to an arrayroot- the view of the root value, the starting point of overload 3BasicJsonType::erase- the corresponding function ofbasic_json- Edits - what an edit guarantees, for every overload
- Zero-copy JSON views - what an edit guarantees and costs
Version history
- Added in version 3.13.0.