Repository navigation
Conversation
|
CI fixes for this PR (commit f72fe8d, plus the lower fixes merged in). CI had not run yet; these were found locally with
Verified: — posted by Claude Code on behalf of @nlohmann |
711ddc4 to
c675bcd
Compare
05b6ce8 to
d52db33
Compare
🔴 Amalgamation check failed! 🔴The source code has not been amalgamated and/or formatted correctly, or |
d52db33 to
467ccfd
Compare
| const string_t no_token{}; | ||
| // the ends of the open containers, and whether they are objects | ||
| std::vector<std::pair<const node*, bool>> open; | ||
| std::vector<frame> open; |
e0c29a1 to
a0b9832
Compare
basic_json_document gets a second template parameter, Editable (false by default), plus the aliases json_editable_document, json_editable_view, ordered_json_editable_document and ordered_json_editable_view. Editable documents can change values and structure without rewriting the source text: set()/push_back() on values, keys, array indices and JSON pointers; insert() before an array element; erase() of an object key, array index or JSON pointer. New values and element sequences go into edit storage that the document owns and never moves, so views keep referring to their value across edits and a parsed node never moves. Read-only documents walk the plain node array and are unaffected. Strings are checked for UTF-8 on entry, so dump() of an editable document never throws type_error.316. Binary values cannot be stored (type_error.319). A seeded differential test applies random edits to an editable document and to the equivalent ordered_json and compares both after every step. Signed-off-by: Niels Lohmann <mail@nlohmann.me>
a0b9832 to
c6ee5a6
Compare
Signed-off-by: Niels Lohmann <mail@nlohmann.me>
🔴 Amalgamation check failed! 🔴The source code has not been amalgamated and/or formatted correctly, or 📎 A ready-to-apply patch is attached to the failed workflow run as the git apply amalgamation.patchThis does not require installing astyle yourself. |
Value-initialize the const std::less in find_parent (clang 3.4/3.6 do not implement DR 253), and test the Editable template argument through a function to avoid MSVC C4127. Signed-off-by: Niels Lohmann <mail@nlohmann.me>
Signed-off-by: Niels Lohmann <mail@nlohmann.me> # Conflicts: # include/nlohmann/detail/view/document_data.hpp # single_include/nlohmann/json_view.hpp
Signed-off-by: Niels Lohmann <mail@nlohmann.me>
Part of the stack for the zero-copy view (#5295). This PR combines #5630 and #5631 (without the
bench_edit.cppbenchmark, which moves to the comparison-harness PR at the top of the stack).Summary
basic_json_documentgets a second template parameter,Editable(false by default). The new aliasesjson_editable_document,json_editable_view,ordered_json_editable_documentandordered_json_editable_viewcan change the document they hold:set(view, value)replaces a value;set(object, key, value)assigns a member or adds it, and a null becomes an object. With duplicate keys, the first member is assigned and the others are removed;set(array, index, value)assigns an element;set(json_pointer, value)sets the member or element that a pointer names;"-"or the size of the array appends;push_back(array, value)appends, and a null becomes an array;insert(array, index, value)inserts before an element (index <= size()) and returns a view of the new element;erase(object, key)removes all members with the key and returns their number;erase(array, index)removes an element;erase(json_pointer)removes the member or element a pointer names and returns the number of removed values.A value can be a view (of any document; it is copied), a
BasicJsonTypevalue, or anythingBasicJsonTypecan be constructed from. A view of an erased value keeps its last value; views of other values keep referring to them when elements move.How edits are stored
detail/view/edit_storage.hpp). So views stay valid, and a view keeps referring to its value: after an assignment, it sees the new value.detail::view::navigation<false>and compile without any of this. That policy does not change behavior.Errors
basic_jsonwhere the operation corresponds: type_error.305/307/308/309, out_of_range.401/403/405, parse_error.106/109.basic_json::dump()for the same string. So an editable document only ever holds valid UTF-8, anddump()never throws.Views of editable and read-only documents compare with each other.
Performance
json_view
Measured on the regrouped stack
Not re-measured separately: the read-only paths are unchanged by this PR (see the numbers of the next PRs).
Earlier measurements (on the old stack)
Read-only documents, before vs. after this change are within about 1% for parse, traverse and
dump().Parse + edit + serialize (
bench_edit, measured in the harness PR once it is rebuilt on this change): 1.1–5.9x faster than yyjson mutable documents, RapidJSON andnlohmann::json, on twitter and citm_catalog.Core library (json::parse / dump)
Not affected: this PR only adds new code to the view.
Tests
unit-json_view_edit.cppholds a seeded differential test that applies random assignments, member and element changes, inserts, erases (directly and through JSON pointers), copies within and between documents, and pushes, both to anordered_json_editable_documentand to the equivalentordered_jsonvalue, and compares serialized, materialized, compared and read-back results after every edit.Further tests cover the errors; strings that stay valid while the edit arena grows; numbers (NaN, infinities, extremes,
number_format::source); nulls that become containers, and a replaced root; duplicate keys, values of other documents, and large objects (the index is not used for moved objects); views across inserts and erasures; and documents reused withread().Public API
No breaking change:
basic_json_documentandbasic_json_viewget a defaulted template parameter. Existing code that names them with one argument, or uses the existing aliases, is unaffected.json_editable_document,json_editable_view,ordered_json_editable_document,ordered_json_editable_view.basic_jsonerror ids now also thrown by the view'sset/push_back/insert/erase.Written by Claude Code.
🤖 Generated with Claude Code