Compare commits

..
Author SHA1 Message Date
Niels Lohmann 2638f276d0 docs: format tables, use the Oxford comma
Reformat the tables changed by this PR (BJData serialization and
deserialization mappings, completeness and binary-value tables) so all
rows are aligned, and add the Oxford comma to lists of three or more
items in the added text.

Signed-off-by: Niels Lohmann <mail@nlohmann.me>
2026-10-26 06:53:55 +01:00
Niels Lohmann 66d4f3602b Address review of the BJData documentation
The $ type restriction exists since Draft 2 (Draft 3 added B), $ requires a count, ND char above 127 is rejected on read, and the new API page is listed in alphabetical order in the nav.

Signed-off-by: Niels Lohmann <mail@nlohmann.me>
2026-10-26 06:53:54 +01:00
Niels Lohmann 612e9ade2c Regenerate single_include and apply astyle
Result of make amalgamate (including make pretty) for the preceding changes.

Signed-off-by: Niels Lohmann <mail@nlohmann.me>
2026-10-26 06:53:53 +01:00
Niels Lohmann b3ab23e11c docs: correct and complete the BJData documentation
Verify the BJData chapter against the library and the Draft 2/3/4
specifications and fix what did not match:

- bjdata.md: correct the list of unused markers (N, not Z), the ND-array
  example (3x2, not 2x3), the conditions under which an object is packed
  as an ND-array (exactly three keys, row vectors, float-only elements for
  single/double, char 0..127, byte only in Draft 3), the binary table row
  and notes (which flag/draft combinations write [$B), the "$" type
  restriction (Draft 3, Z is excluded too), the H marker in the
  deserialization table, and the claim of a complete deserialization
  mapping. Document the supported drafts, that Draft 4 (SoA containers,
  extension type E) and column-major ND-arrays are rejected, ND-arrays of
  half floats, duplicate object keys, and link the Draft 3 specifications
  instead of master.
- Add an API page for basic_json::bjdata_version_t with an example, and
  link it from the nav, the API index and to_bjdata.
- Update binary_formats/index.md and binary_values.md, which still claimed
  that BJData has no binary type, and fix a wrong byte comment there.
- Document parse_error.115 and the out_of_range.408 cases for from_bjdata,
  the precise other_error.502 condition for to_bjdata, and reword the note
  on truncated unsigned integers in exceptions.md (only ND-array elements
  were truncated before 3.13.0).

Signed-off-by: Niels Lohmann <mail@nlohmann.me>
2026-10-26 06:53:52 +01:00
Niels Lohmann 31538a91ed Read ND-arrays of half floats and enforce the char limit in ND-arrays
BJData N-dimensional arrays with element type 'h' (float16) failed to
read with a misleading parse_error.112, because the reader's dtype name
table lacked 'h'. It now maps to "half", the JData name. The writer still
does not pack "half"; such an object is written as a plain object and
reads back equal.

BJData limits 'C' (char) to 0..127 in Draft 2 and Draft 3, and the 1-D
reader enforces this, but ND-arrays with _ArrayType_ "char" wrote values
up to 255 as 'C' elements and read them back without a check. The writer
now only packs values in 0..127 (others fall back to a plain object that
round-trips), and the reader rejects 'C' elements above 127 with
parse_error.113 like the 1-D reader.

Signed-off-by: Niels Lohmann <mail@nlohmann.me>
2026-10-26 06:53:51 +01:00
Niels Lohmann bf8dca5c16 Fix -Weffc++ in the BSON recorder test and a clang-tidy use-auto hit
Signed-off-by: Niels Lohmann <mail@nlohmann.me>
2026-10-26 06:53:50 +01:00
Niels Lohmann d19e030bf1 docs: use the Oxford comma, drop discarded-value remarks
Add the serial comma in a list added by this PR and remove the BSON
example of type_error.321 (with the type_error.317 remark) so the docs do
not mention discarded values for BSON; restore the original singular
heading of that section. No tables were changed by this PR.

Signed-off-by: Niels Lohmann <mail@nlohmann.me>
2026-10-26 06:53:09 +01:00
Niels Lohmann e359852b76 Address review of the BSON documentation
Describe exactly when a too small document size is detected and note that subtype 2 data written by earlier versions is not readable.

Signed-off-by: Niels Lohmann <mail@nlohmann.me>
2026-10-26 06:53:08 +01:00
Niels Lohmann 734912bed6 Regenerate single_include and apply astyle
Result of make amalgamate (including make pretty) for the preceding changes.

Signed-off-by: Niels Lohmann <mail@nlohmann.me>
2026-10-26 06:53:07 +01:00
Niels Lohmann 91cc42b302 Document BSON subtype 0x02, document size checks, and lenient reads
Describe the inner length of binary subtype 0x02 that to_bson now writes and
from_bson checks and removes. Add the document size checks (parse_error.112,
not affected by strict=false) to bson.md and from_bson.md, and extend the
list of lenient reads with the UUID and MD5 payload lengths (not checked),
duplicate keys (last value wins), embedded NUL bytes in strings, and the
ignored trailing bytes with strict=false. State that the members of an object
are written in iteration order.

Correct the statement that element names are never validated on read: they
are checked and sanitized like string values by the error handler. Add the
BSON message to the examples of out_of_range.415 and type_error.321, the
subtype limit of 255 to the binary values page, CBOR to the formats that use
byte_container_with_subtype, and fix a typo in the incomplete mapping box.

Signed-off-by: Niels Lohmann <mail@nlohmann.me>
2026-10-26 06:53:06 +01:00
Niels Lohmann 95412a36d0 Check the declared BSON document size before reading its content
The BSON reader compared a document's declared size with the number of bytes
read only when it reached the terminator. The declared size never bounded
the reads, so a SAX handler saw the keys and values of a document whose size
was too small before parse error 112 was reported.

The container frame now stores the position where the document ends
according to its declared size. open_bson_document rejects a size below 5
(size prefix plus terminator) and an embedded document that does not end
before the terminator of its enclosing document; the element loop rejects an
element whose type byte leaves no room for the name terminator and the
document terminator. The terminator check stays and still catches a declared
size that is too large. No input that was accepted before is rejected now;
documents with a wrong size are reported earlier, with fewer SAX events, a
different byte position and, in three cases, a different message. The error
id (parse_error.112) is unchanged and strict=false still does not skip the
check.

The tests that asserted the old messages and positions are updated, with the
outer sizes of two inputs corrected so that they still exercise the embedded
document. New tests cover the SAX events, a size below 5, and an embedded
document that fits exactly.

Signed-off-by: Niels Lohmann <mail@nlohmann.me>
2026-10-26 06:53:05 +01:00
Niels Lohmann d2309300e9 Write and read the inner length of BSON binary subtype 0x02
The BSON specification defines the payload of the "binary (old)" subtype
0x02 as an int32 followed by that many bytes. to_bson wrote the plain
payload instead, so decoders such as pymongo and libbson-based readers
rejected the bytes, and from_bson returned the inner length as part of the
payload (documented as lenient behavior in gh-5333).

The writer now emits the outer length (payload + 4), the subtype, the inner
int32 holding the payload size, and the payload. The reader requires an
outer length of at least 4 and an inner length equal to the outer length
minus 4 (the cases the bson-corpus lists as decode errors), reports parse
error 112 otherwise, and returns only the payload. Both directions change
together so that the round trip keeps working.

The single-reservation fast path for scalar entries declines subtype 0x02
because it has no room for the inner length.

This is a deliberate exception to the opt-in rule for behavior changes:
the previous output was invalid BSON.

Signed-off-by: Niels Lohmann <mail@nlohmann.me>
2026-10-26 06:53:04 +01:00
Niels Lohmann d3d39a9c10 Fix narrowing of the optimized-container count in BJData and UBJSON tests
Signed-off-by: Niels Lohmann <mail@nlohmann.me>
2026-10-26 06:52:50 +01:00
Niels Lohmann 24ff6eaa43 docs: format tables, use the Oxford comma, drop discarded-value remarks
Reformat the two UBJSON mapping tables that this PR changed (uniform column
widths, padded cells), use the serial comma in lists of three or more in the
added text, and restore the original type_error.321 bullet in to_ubjson.md.

Signed-off-by: Niels Lohmann <mail@nlohmann.me>
2026-10-25 20:05:29 +01:00
Niels Lohmann 9d9bf927ff Address review of the UBJSON documentation
Clarify that empty containers carry no $ annotation of their own and note that a counted N object is not supported.

Signed-off-by: Niels Lohmann <mail@nlohmann.me>
2026-10-25 20:05:29 +01:00
Niels Lohmann cc39e1f8da Regenerate single_include and apply astyle
Result of make amalgamate (including make pretty) for the preceding changes.

Signed-off-by: Niels Lohmann <mail@nlohmann.me>
2026-10-25 20:05:28 +01:00
Niels Lohmann 5ac95db5a9 Correct and complete the UBJSON documentation
Fix statements that disagree with the behavior (checked with probes):
- `Z` is null; the unused marker is `N`.
- `$` requires `#`; use_type with use_size=false throws other_error.502 only
  for non-empty arrays, objects and binary values.
- NaN and infinity are written as IEEE 754 float64 and read back unchanged;
  the UBJSON specification encodes infinity as null.
- `H` is documented (semantics, parse_error.115, out_of_range.406); the
  "complete mapping" claim is qualified.
- use_type "same type" means same marker.
- Long Z/T/F arrays lose only the `$` annotation; the `#` count and the
  element markers are kept.
- from_ubjson lists the 112, 113, 115, 406 and 408 causes that occur for
  UBJSON, without the BJData-only ND-array wording.
- Duplicate object keys keep the last value; the `$` type marker is
  validated even for count 0.
- Smaller fixes: float32 row, "map" -> "object", https link, exception 110
  strict case, to_ubjson history of discarded values.

Signed-off-by: Niels Lohmann <mail@nlohmann.me>
2026-10-25 20:05:27 +01:00
Niels Lohmann d1d142633a Validate the optimized-container type marker for count 0 in UBJSON and BJData
The '$' type marker of an optimized container was only checked when an
element was read, so containers with count 0 accepted any marker:
`[$x#i0`, `{$x#i0` and `[$E#i0` parsed as empty containers although a
non-empty container with the same marker is rejected and the UBJSON
specification requires a legal marker after '$'.

get_ubjson_size_type() now checks the marker right after '$' against the
allowed set (UBJSON: Z N T F i U I l L d D H C S [ {; BJData: B U i u I m
l M L h d D C, with the existing exclusion of T F Z N S H [ {) and reports
parse_error.112 "marker ... is not a permitted optimized array type".

Input that was accepted and is now rejected: an optimized container with
count 0 and an invalid '$' type marker. Valid input is unaffected. Two
existing tests that used an invalid marker incidentally ("[$#" and a
negative-count case) were adapted.

Signed-off-by: Niels Lohmann <mail@nlohmann.me>
2026-10-25 20:05:26 +01:00
Niels Lohmann 49e9eaac4f Address review of the MessagePack documentation
Name parse_error.113 for a 0xC1 map key, move the duplicate-key sentence out of the ext section, and keep the 0xC1 row in marker order.

Signed-off-by: Niels Lohmann <mail@nlohmann.me>
2026-10-25 20:05:25 +01:00
Niels Lohmann 4b35877ae5 Document MessagePack ext types and fix related doc errors
Add an "Extension types (ext, fixext)" section to the MessagePack page:
ext/fixext are read as binary, the signed ext type is exposed as an
unsigned subtype 0..255 (-1 reads as 255), Timestamp is not interpreted,
subtypes are always written as ext/fixext, and subtypes above 255 throw
out_of_range.415. Add the 0xC1 row to the deserialization table, note
that duplicate keys keep the last value, and correct the NaN/Infinity
explanation, the "31 bytes / 15 elements" wording, the ill-formed UTF-8
sentence, and the generic 0xC1-key error message.

Add fixext16 and the unsigned subtype mapping to binary_t.md and
binary_values.md, reword "maximum of the MessagePack ext type" in
to_msgpack.md and exceptions.md, and mark MessagePack deserialization
as incomplete (non-string keys) in the binary formats overview.

Signed-off-by: Niels Lohmann <mail@nlohmann.me>
2026-10-25 20:05:24 +01:00
Niels Lohmann 34f9be2aa2 docs: drop discarded-value remarks
Remove the mention that a discarded value cannot be serialized to CBOR
(type_error.321) from the exception-safety and mapping sections. The
integer table and all added prose already follow the table-format and
Oxford-comma rules.

Signed-off-by: Niels Lohmann <mail@nlohmann.me>
2026-10-25 20:05:23 +01:00
Niels Lohmann 8c4818d5f7 Address review of the CBOR changes
Cite RFC 8949 Section 5.3.1 for duplicate keys, describe the out_of_range.408 condition precisely, rewrap a long line, and update the stale get_string comment (per-chunk UTF-8 check). single_include regenerated.

Signed-off-by: Niels Lohmann <mail@nlohmann.me>
2026-10-25 17:56:22 +01:00
Niels Lohmann df1bca6dae Regenerate single_include and apply astyle
Result of make amalgamate (including make pretty) for the preceding changes.

Signed-off-by: Niels Lohmann <mail@nlohmann.me>
2026-10-25 17:46:08 +01:00
Niels Lohmann b22cad367c docs: correct and extend the CBOR documentation
Fix the negative-integer rows (they used MessagePack boundaries), the
number_integer range of the 0x1B row, and the half-precision entry in the
list of unused types; correct the NaN history (0xF9 since 3.8.0) and the
tagged-item wording (only the tag directly in front of a byte string is
stored, and only in the 0xD8..0xDB form). Document that to_cbor is not
RFC 8949 deterministic, that duplicate keys are accepted (last wins),
tags in front of keys, per-chunk UTF-8 validation, the exceptions
parse_error.113, out_of_range.406/408 of from_cbor, the pointer and
C array input traps, the partially written output on exceptions, and the
store/ignore results in the binary values example. Drop the claim that
the CBOR reader needs StringType::append(const StringType&) and
BinaryType::insert(pos, first, last).

Signed-off-by: Niels Lohmann <mail@nlohmann.me>
2026-10-25 17:46:05 +01:00
Niels Lohmann 95169a63fa Skip CBOR tags in front of map keys and validate indefinite text chunks
A tag in front of a text string map key was rejected with parse_error.113
even with cbor_tag_handler_t::ignore or store, although tags in front of
values are handled. The tags are now skipped iteratively before the key is
read; the handler error still rejects them.

The chunks of an indefinite-length text string were concatenated before
the single UTF-8 check, so a code point split over two chunks, or an
invalid chunk, was accepted. RFC 8949, Section 3.2.3 requires every chunk
to be valid on its own. With error_handler_t::strict, replace and ignore
each chunk is now checked; keep is unchanged. The chunks are appended with
append(ptr, len), so StringType needs nothing new.

Signed-off-by: Niels Lohmann <mail@nlohmann.me>
2026-10-25 17:46:05 +01:00
Niels Lohmann 2a56afc714 Fix VS 2015 build of the DOM parser (#5882) (#5916)
- Name std::hash only once the object type has emplace_new_key: VS 2015
  static-asserts on std::hash of a type without one (alt_string) instead of
  reporting a substitution failure (C2338).
- Replace the std::copy calls on frames by loops: VS 2015 reports raw
  pointers handed to std::copy as C4996, an error under /WX.

Signed-off-by: Niels Lohmann <mail@nlohmann.me>
2026-10-25 12:08:58 +01:00
Niels Lohmann d176d8d61e Exclude single_include from flawfinder scan (#5912)
Signed-off-by: Niels Lohmann <mail@nlohmann.me>
2026-10-25 12:08:36 +01:00
Niels Lohmann 1cf5492bcd docs: byte layout sections, glossary entries, and QA page update (#5910)
* docs: byte layout sections, glossary, and QA page updates

- Move "Byte layout" of each binary format to its own section before
  "Serialization" and describe the idea of the format.
- Glossary: add LLDB and other terms used throughout the documentation.
- quality_assurance: correct Clang Static Analyzer and CMake entries; add
  CodeQL, Semgrep, Flawfinder, CIFuzz, OpenSSF Scorecard, REUSE, Meson/SwiftPM,
  and reproducibility checks.

Signed-off-by: Niels Lohmann <mail@nlohmann.me>

* docs: drop unused length colour from CBOR and MessagePack byte layout legends

Signed-off-by: Niels Lohmann <mail@nlohmann.me>

---------

Signed-off-by: Niels Lohmann <mail@nlohmann.me>
2026-10-25 12:08:14 +01:00
Niels Lohmann 330fbead59 Document clang-tidy exclusions, drop unneeded ones, and clean up doc examples (#5909)
* Document clang-tidy exclusions and drop unneeded ones

Re-enable every excluded check in a run of clang-tidy 22 over all tests
and remove the seven that never fire (cert-err58-cpp,
clang-analyzer-webkit.NoUncountedMemberChecker,
cppcoreguidelines-virtual-class-destructor, google-readability-function-size,
hicpp-function-size, hicpp-no-assembler, readability-function-size).

Document the reason for every remaining exclusion at the top of the file.

Exclude in advance the checks that only exist in clang-tidy 23 and are
noise here (bugprone-signed-bitwise, *-explicit-constructor,
readability-trailing-comma, modernize-use-string-view), plus
modernize-use-integer-sign-comparison, which needs C++20 std::cmp_less
and flags deliberate casts.

Signed-off-by: Niels Lohmann <mail@nlohmann.me>

* Fix clang-tidy findings in documentation examples and replace redundant aliases

Apply clang-tidy fixes to the documentation examples:
- performance-avoid-endl: std::endl -> '\n'
- readability-qualified-auto: auto -> auto* / const auto*
- performance-for-range-copy: copy -> const reference
- google-readability-casting: C-style cast -> static_cast

Replace redundant aliases (readability-redundant-qualified-alias) such as
"using json = nlohmann::json;" with "using nlohmann::json;" in the
documentation, README, examples, tests, and benchmarks. Aliases at class
scope and aliases of other types (with_base_class_t etc.) are kept.

No changes to the library or its public API. All changed examples were
compiled and run; their output matches the stored .output files (except
max_size and meta, which depend on the platform and compiler).

Signed-off-by: Niels Lohmann <mail@nlohmann.me>

* Format doc examples with the pinned astyle

Signed-off-by: Niels Lohmann <mail@nlohmann.me>

---------

Signed-off-by: Niels Lohmann <mail@nlohmann.me>
2026-10-25 12:07:51 +01:00
Niels Lohmann fe231dc692 docs: write std::chrono durations as unit/value objects; reword a title with escaped quotes (#5911)
Signed-off-by: Niels Lohmann <mail@nlohmann.me>
2026-10-25 12:07:32 +01:00
Niels Lohmann ffd4242dc2 Docs: fill documentation gaps found in user questions (#5907)
* Docs: fill documentation gaps found in user questions

Add pages on class-hierarchy/smart-pointer/variant/chrono recipes, working
without exceptions, and large, streamed and partial input. Extend the FAQ
(23 entries, Known bugs first), parse error documentation (empty-input
checklist, line/column, diagnostic positions), element access, conversions,
creating values, ADL troubleshooting, memory, and build/distribution notes.
Add 14 examples with generated output. Correct the statement that parse errors
cannot be recovered from with -fno-exceptions.

The topics come from recurring questions on Stack Overflow, Reddit, Hacker
News, YouTube, and the project's GitHub discussions and issues.

Signed-off-by: Niels Lohmann <mail@nlohmann.me>

* docs: declare range-for lists in variables to keep astyle quiet

astyle reformats a braced init list placed directly in a range-for
header; declare the list first so check_amalgamation passes.

Signed-off-by: Niels Lohmann <mail@nlohmann.me>

---------

Signed-off-by: Niels Lohmann <mail@nlohmann.me>
2026-10-25 12:07:13 +01:00
479 changed files with 4596 additions and 1768 deletions

No files matched your search

+36 -7
View File
@@ -1,3 +1,33 @@
# Style or vendor rule sets that do not fit a header-only library:
# altera-*, fuchsia-*, llvmlibc-*, android-cloexec-fopen, boost-use-ranges, llvm-header-guard,
# llvm-include-order, llvm-prefer-static-over-anonymous-namespace, llvm-use-ranges,
# misc-use-anonymous-namespace, google-runtime-float, google-runtime-int, hicpp-signed-bitwise,
# bugprone-signed-bitwise, hicpp-explicit-conversions, google-explicit-constructor,
# cppcoreguidelines-explicit-constructor, misc-explicit-constructor (implicit conversions are the
# API), readability-trailing-comma,
# hicpp-avoid-goto / cppcoreguidelines-avoid-goto, hicpp-no-array-decay,
# hicpp-uppercase-literal-suffix, readability-uppercase-literal-suffix, modernize-use-trailing-return-type.
# Constructs the library needs on purpose (the library supports C++11 and legacy compilers):
# cppcoreguidelines-macro-usage, cppcoreguidelines-avoid-do-while (macros),
# cppcoreguidelines-pro-bounds-*, cppcoreguidelines-pro-type-reinterpret-cast,
# cppcoreguidelines-pro-type-union-access (parsers, serializers, basic_json's value union),
# cppcoreguidelines-avoid-const-or-ref-data-members, cppcoreguidelines-rvalue-reference-param-not-moved,
# cppcoreguidelines-avoid-non-const-global-variables, misc-non-private-member-variables-in-classes,
# misc-no-recursion (value walks are recursive, bounded by detail::recursion_depth_limit()),
# modernize-type-traits, modernize-use-constraints, modernize-use-ranges, modernize-use-std-numbers,
# modernize-use-designated-initializers, modernize-use-nodiscard, modernize-concat-nested-namespaces,
# readability-redundant-typename, readability-redundant-parentheses,
# readability-redundant-access-specifiers, readability-simplify-boolean-expr,
# readability-use-concise-preprocessor-directives, modernize-use-string-view,
# modernize-use-integer-sign-comparison (needs std::cmp_less; the flagged
# static_cast<std::size_t>(end - pos) < n comparisons are deliberate)
# (all need a newer standard or compiler than C++11, or would change macro-generated or
# deliberately explicit code).
# Too noisy to be useful (numeric parsing/formatting code, doctest, tests):
# bugprone-easily-swappable-parameters, cppcoreguidelines-avoid-magic-numbers, readability-magic-numbers,
# readability-identifier-length, readability-function-cognitive-complexity, performance-enum-size,
# concurrency-mt-unsafe, misc-confusable-identifiers, misc-include-cleaner (run on its own for
# the consumer headers in cmake/ci.cmake).
# portability-avoid-pragma-once: kept disabled on purpose. #pragma once is accepted # portability-avoid-pragma-once: kept disabled on purpose. #pragma once is accepted
# by every supported compiler, and tools/amalgamate/amalgamate.py strips it from # by every supported compiler, and tools/amalgamate/amalgamate.py strips it from
# single_include, so there is nothing left to fix here. # single_include, so there is nothing left to fix here.
@@ -10,14 +40,14 @@ Checks: '*,
-android-cloexec-fopen, -android-cloexec-fopen,
-boost-use-ranges, -boost-use-ranges,
-bugprone-easily-swappable-parameters, -bugprone-easily-swappable-parameters,
-cert-err58-cpp, -bugprone-signed-bitwise,
-clang-analyzer-webkit.NoUncountedMemberChecker,
-concurrency-mt-unsafe, -concurrency-mt-unsafe,
-cppcoreguidelines-avoid-const-or-ref-data-members, -cppcoreguidelines-avoid-const-or-ref-data-members,
-cppcoreguidelines-avoid-do-while, -cppcoreguidelines-avoid-do-while,
-cppcoreguidelines-avoid-goto, -cppcoreguidelines-avoid-goto,
-cppcoreguidelines-avoid-magic-numbers, -cppcoreguidelines-avoid-magic-numbers,
-cppcoreguidelines-avoid-non-const-global-variables, -cppcoreguidelines-avoid-non-const-global-variables,
-cppcoreguidelines-explicit-constructor,
-cppcoreguidelines-macro-usage, -cppcoreguidelines-macro-usage,
-cppcoreguidelines-pro-bounds-avoid-unchecked-container-access, -cppcoreguidelines-pro-bounds-avoid-unchecked-container-access,
-cppcoreguidelines-pro-bounds-array-to-pointer-decay, -cppcoreguidelines-pro-bounds-array-to-pointer-decay,
@@ -26,19 +56,15 @@ Checks: '*,
-cppcoreguidelines-pro-type-reinterpret-cast, -cppcoreguidelines-pro-type-reinterpret-cast,
-cppcoreguidelines-pro-type-union-access, -cppcoreguidelines-pro-type-union-access,
-cppcoreguidelines-rvalue-reference-param-not-moved, -cppcoreguidelines-rvalue-reference-param-not-moved,
-cppcoreguidelines-virtual-class-destructor,
-fuchsia-default-arguments-calls, -fuchsia-default-arguments-calls,
-fuchsia-default-arguments-declarations, -fuchsia-default-arguments-declarations,
-fuchsia-overloaded-operator, -fuchsia-overloaded-operator,
-google-explicit-constructor, -google-explicit-constructor,
-google-readability-function-size,
-google-runtime-float, -google-runtime-float,
-google-runtime-int, -google-runtime-int,
-hicpp-avoid-goto, -hicpp-avoid-goto,
-hicpp-explicit-conversions, -hicpp-explicit-conversions,
-hicpp-function-size,
-hicpp-no-array-decay, -hicpp-no-array-decay,
-hicpp-no-assembler,
-hicpp-signed-bitwise, -hicpp-signed-bitwise,
-hicpp-uppercase-literal-suffix, -hicpp-uppercase-literal-suffix,
-llvm-header-guard, -llvm-header-guard,
@@ -48,6 +74,7 @@ Checks: '*,
-llvmlibc-*, -llvmlibc-*,
-misc-use-anonymous-namespace, -misc-use-anonymous-namespace,
-misc-confusable-identifiers, -misc-confusable-identifiers,
-misc-explicit-constructor,
-misc-include-cleaner, -misc-include-cleaner,
-misc-no-recursion, -misc-no-recursion,
-misc-non-private-member-variables-in-classes, -misc-non-private-member-variables-in-classes,
@@ -55,20 +82,22 @@ Checks: '*,
-modernize-type-traits, -modernize-type-traits,
-modernize-use-constraints, -modernize-use-constraints,
-modernize-use-designated-initializers, -modernize-use-designated-initializers,
-modernize-use-integer-sign-comparison,
-modernize-use-nodiscard, -modernize-use-nodiscard,
-modernize-use-ranges, -modernize-use-ranges,
-modernize-use-string-view,
-modernize-use-std-numbers, -modernize-use-std-numbers,
-modernize-use-trailing-return-type, -modernize-use-trailing-return-type,
-performance-enum-size, -performance-enum-size,
-portability-avoid-pragma-once, -portability-avoid-pragma-once,
-readability-function-cognitive-complexity, -readability-function-cognitive-complexity,
-readability-function-size,
-readability-identifier-length, -readability-identifier-length,
-readability-magic-numbers, -readability-magic-numbers,
-readability-redundant-access-specifiers, -readability-redundant-access-specifiers,
-readability-redundant-parentheses, -readability-redundant-parentheses,
-readability-redundant-typename, -readability-redundant-typename,
-readability-simplify-boolean-expr, -readability-simplify-boolean-expr,
-readability-trailing-comma,
-readability-uppercase-literal-suffix, -readability-uppercase-literal-suffix,
-readability-use-concise-preprocessor-directives' -readability-use-concise-preprocessor-directives'
+2 -1
View File
@@ -43,7 +43,8 @@ jobs:
- name: flawfinder_scan - name: flawfinder_scan
uses: david-a-wheeler/flawfinder@c4216b74cf2639ffa98503768bd6e4299b5440c9 # v2.0.20 uses: david-a-wheeler/flawfinder@c4216b74cf2639ffa98503768bd6e4299b5440c9 # v2.0.20
with: with:
arguments: '--sarif ./' # single_include/ is omitted: it is generated from include/ and would report every finding twice
arguments: '--sarif ./cmake ./docs ./include ./src ./tests ./tools'
output: 'flawfinder_results.sarif' output: 'flawfinder_results.sarif'
- name: Upload analysis results to GitHub Security tab - name: Upload analysis results to GitHub Security tab
-1
View File
@@ -90,7 +90,6 @@ cc_library(
"include/nlohmann/detail/view/simd.hpp", "include/nlohmann/detail/view/simd.hpp",
"include/nlohmann/detail/view/string_ref.hpp", "include/nlohmann/detail/view/string_ref.hpp",
"include/nlohmann/detail/view/value.hpp", "include/nlohmann/detail/view/value.hpp",
"include/nlohmann/detail/view/view_mode.hpp",
"include/nlohmann/json.hpp", "include/nlohmann/json.hpp",
"include/nlohmann/json_fwd.hpp", "include/nlohmann/json_fwd.hpp",
"include/nlohmann/json_literals.hpp", "include/nlohmann/json_literals.hpp",
+4 -4
View File
@@ -151,7 +151,7 @@ The `json` class provides an API for manipulating a JSON value. To create a `jso
```cpp ```cpp
#include <fstream> #include <fstream>
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
using json = nlohmann::json; using nlohmann::json;
// ... // ...
@@ -164,7 +164,7 @@ If using modules (enabled with `NLOHMANN_JSON_BUILD_MODULES`), this example beco
import std; import std;
import nlohmann.json; import nlohmann.json;
using json = nlohmann::json; using nlohmann::json;
// ... // ...
@@ -842,7 +842,7 @@ assert(p == p2);
To make this work with one of your types, you only need to provide two functions: To make this work with one of your types, you only need to provide two functions:
```cpp ```cpp
using json = nlohmann::json; using nlohmann::json;
namespace ns { namespace ns {
void to_json(json& j, const person& p) { void to_json(json& j, const person& p) {
@@ -1257,7 +1257,7 @@ See the page [quality assurance](https://json.nlohmann.me/community/quality_assu
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
// for convenience // for convenience
using json = nlohmann::json; using nlohmann::json;
``` ```
to the files you want to process JSON and set the necessary switches to enable C++11 (e.g., `-std=c++11` for GCC and Clang). to the files you want to process JSON and set the necessary switches to enable C++11 (e.g., `-std=c++11` for GCC and Clang).
+3 -2
View File
@@ -90,8 +90,9 @@ type `#!cpp binary_t*` must be dereferenced.
- MessagePack - MessagePack
- If a subtype is given and the binary array contains exactly 1, 2, 4, 8, or 16 elements, the fixext family (fixext1, - If a subtype is given and the binary array contains exactly 1, 2, 4, 8, or 16 elements, the fixext family (fixext1,
fixext2, fixext4, fixext8) is used. For other sizes, the ext family (ext8, ext16, ext32) is used. The subtype is fixext2, fixext4, fixext8, fixext16) is used. For other sizes, the ext family (ext8, ext16, ext32) is used. The
then added as a signed 8-bit integer. subtype is then written as the signed 8-bit ext type: subtypes 128 to 255 map to the negative (reserved) ext
types, and on reading the ext type is exposed as an unsigned subtype from 0 to 255.
- If no subtype is given, the bin family (bin8, bin16, bin32) is used. - If no subtype is given, the bin family (bin8, bin16, bin32) is used.
- BSON - BSON
@@ -0,0 +1,48 @@
# <small>nlohmann::basic_json::</small>bjdata_version_t
```cpp
enum class bjdata_version_t
{
draft2,
draft3
};
```
This enumeration is used in [`to_bjdata`](to_bjdata.md) to choose the version of the BJData specification to write:
draft2
: BJData Draft 2 (the default). Binary values are written as arrays of `U` integers, and ND-arrays with
`"_ArrayType_": "byte"` are written as regular objects, since the `B` marker is not part of Draft 2.
draft3
: BJData Draft 3. Binary values are written as `B` elements: as an optimized binary array (`[$B#`) if both `use_size`
and `use_type` are set, and as an array of `B`-prefixed elements otherwise. ND-arrays with
`"_ArrayType_": "byte"` are written as packed arrays (`[$B#[...]`).
Only the writer is affected by the version. [`from_bjdata`](from_bjdata.md) reads the markers of both drafts.
## Examples
??? example
The example below shows how the different values of the `bjdata_version_t` influence the serialization of a binary
value with [`to_bjdata`](to_bjdata.md).
```cpp
--8<-- "examples/bjdata_version_t.cpp"
```
Output:
```json
--8<-- "examples/bjdata_version_t.output"
```
## See also
- [to_bjdata](to_bjdata.md) serializes a JSON value to BJData
- [BJData](../../features/binary_formats/bjdata.md) - the article on the BJData format
## Version history
- Added in version 3.12.0.
@@ -15,11 +15,13 @@ error
: report a parse error in case of a tag (the `from_cbor` overloads throw a `parse_error` exception by default) : report a parse error in case of a tag (the `from_cbor` overloads throw a `parse_error` exception by default)
ignore ignore
: ignore tags : ignore tags (also in front of a text string map key)
store store
: store tagged byte strings (for bytes 0xd8..0xdb) as binary values with the tag as subtype; other tagged values are : store tagged byte strings (for bytes 0xd8..0xdb) as binary values with the tag as subtype; other tagged values are
read as if the tag were ignored. If several tags precede a byte string, only the innermost one is stored. read as if the tag were ignored. If several tags precede a byte string, only the tag directly in front of it is
stored, and only when it has the form 0xd8..0xdb (a tag 0..23 written as 0xc0..0xd7 is skipped). Tags in front of a
text string map key are skipped.
## Examples ## Examples
@@ -82,8 +82,11 @@ Strong guarantee: if an exception is thrown, there are no changes in the JSON va
- Throws [parse_error.112](../../home/exceptions.md#jsonexceptionparse_error112) if a parse error occurs - Throws [parse_error.112](../../home/exceptions.md#jsonexceptionparse_error112) if a parse error occurs
- Throws [parse_error.113](../../home/exceptions.md#jsonexceptionparse_error113) if a string could not be parsed - Throws [parse_error.113](../../home/exceptions.md#jsonexceptionparse_error113) if a string could not be parsed
successfully, or if a string value or object key is not valid UTF-8 and `error_handler` is `strict` successfully, or if a string value or object key is not valid UTF-8 and `error_handler` is `strict`
- Throws [parse_error.115](../../home/exceptions.md#jsonexceptionparse_error115) if the text of a high-precision
number (`H`) is not a valid number
- Throws [out_of_range.408](../../home/exceptions.md#jsonexceptionout_of_range408) if the size of an optimized container - Throws [out_of_range.408](../../home/exceptions.md#jsonexceptionout_of_range408) if the size of an optimized container
or n-dimensional array cannot be represented by `std::size_t` cannot be represented by `std::size_t` or exceeds the container's `max_size()`, or if the product of the dimensions of
an n-dimensional array overflows
## Complexity ## Complexity
+2 -1
View File
@@ -80,7 +80,8 @@ Strong guarantee: if an exception is thrown, there are no changes in the JSON va
- Throws [`parse_error.110`](../../home/exceptions.md#jsonexceptionparse_error110) if the given input ends prematurely or - Throws [`parse_error.110`](../../home/exceptions.md#jsonexceptionparse_error110) if the given input ends prematurely or
the end of the input was not reached when `strict` was set to true the end of the input was not reached when `strict` was set to true
- Throws [`parse_error.112`](../../home/exceptions.md#jsonexceptionparse_error112) if a parse error occurs (e.g., an - Throws [`parse_error.112`](../../home/exceptions.md#jsonexceptionparse_error112) if a parse error occurs (e.g., an
invalid string or byte array length) invalid string or byte array length, an invalid inner length of a binary value with subtype `0x02`, or a document size
that does not match the content; `strict` does not affect the document size checks)
- Throws [`parse_error.114`](../../home/exceptions.md#jsonexceptionparse_error114) if an unsupported BSON record type is - Throws [`parse_error.114`](../../home/exceptions.md#jsonexceptionparse_error114) if an unsupported BSON record type is
encountered encountered
- Throws [`parse_error.113`](../../home/exceptions.md#jsonexceptionparse_error113) if a string value or object key is - Throws [`parse_error.113`](../../home/exceptions.md#jsonexceptionparse_error113) if a string value or object key is
+12 -4
View File
@@ -32,8 +32,10 @@ The exact mapping and its limitations are described on a [dedicated page](../../
- an `std::istream` object - an `std::istream` object
- a `FILE` pointer - a `FILE` pointer
- a C-style array of characters - a C-style array of characters (its terminating NUL is read as trailing data: `parse_error.110` unless `strict` is
- a pointer to a null-terminated string of single byte characters `false`)
- a pointer to a null-terminated string of single byte characters (read up to the first 0x00 byte, which is a valid
CBOR byte)
- a container `obj` for which `begin(obj)` and `end(obj)` produce a valid pair of iterators - a container `obj` for which `begin(obj)` and `end(obj)` produce a valid pair of iterators
(as found via ADL or member functions, with semantics compatible to `std::begin` and `std::end`) (as found via ADL or member functions, with semantics compatible to `std::begin` and `std::end`)
@@ -89,8 +91,14 @@ Strong guarantee: if an exception is thrown, there are no changes in the JSON va
- Throws [parse_error.112](../../home/exceptions.md#jsonexceptionparse_error112) if unsupported features from CBOR were - Throws [parse_error.112](../../home/exceptions.md#jsonexceptionparse_error112) if unsupported features from CBOR were
used in the given input or if the input is not valid CBOR used in the given input or if the input is not valid CBOR
- Throws [parse_error.113](../../home/exceptions.md#jsonexceptionparse_error113) if a map key is not a string (keys of - Throws [parse_error.113](../../home/exceptions.md#jsonexceptionparse_error113) if a map key is not a string (keys of
other types are not supported, as JSON object keys are always strings), or if a string value or object key is not other types are not supported, as JSON object keys are always strings), if the head of a string or byte string is
valid UTF-8 and `error_handler` is `strict` malformed (including a chunk of the wrong type or an indefinite-length string nested inside an indefinite-length
string), or if a string value or object key (or one chunk of an indefinite-length string) is not valid UTF-8 and
`error_handler` is `strict`
- Throws [out_of_range.408](../../home/exceptions.md#jsonexceptionout_of_range408) if an array or map announces
a length that `std::size_t` cannot hold, or that equals `std::size_t(-1)` (for example `9B FF FF FF FF FF FF FF FF`)
- Throws [out_of_range.406](../../home/exceptions.md#jsonexceptionout_of_range406) if a floating-point number does not
fit into `number_float_t`
## Complexity ## Complexity
+11 -4
View File
@@ -79,11 +79,18 @@ Strong guarantee: if an exception is thrown, there are no changes in the JSON va
- Throws [parse_error.110](../../home/exceptions.md#jsonexceptionparse_error110) if the given input ends prematurely or - Throws [parse_error.110](../../home/exceptions.md#jsonexceptionparse_error110) if the given input ends prematurely or
the end of the file was not reached when `strict` was set to true the end of the file was not reached when `strict` was set to true
- Throws [parse_error.112](../../home/exceptions.md#jsonexceptionparse_error112) if a parse error occurs - Throws [parse_error.112](../../home/exceptions.md#jsonexceptionparse_error112) if an invalid type marker is read
(including a type marker after `$` of an optimized container, even if its count is 0), or if `$` is not followed by `#`
- Throws [parse_error.113](../../home/exceptions.md#jsonexceptionparse_error113) if a string could not be parsed - Throws [parse_error.113](../../home/exceptions.md#jsonexceptionparse_error113) if a string could not be parsed
successfully, or if a string value or object key is not valid UTF-8 and `error_handler` is `strict` successfully (negative or non-integer length, `C` followed by a byte above 0x7F), if the count of an optimized
- Throws [out_of_range.408](../../home/exceptions.md#jsonexceptionout_of_range408) if the size of an optimized container container is negative, or if a string value or object key is not valid UTF-8 and `error_handler` is `strict`
or n-dimensional array cannot be represented by `std::size_t` - Throws [parse_error.115](../../home/exceptions.md#jsonexceptionparse_error115) if the text of a high-precision number
(`H`) is not a valid JSON number
- Throws [out_of_range.406](../../home/exceptions.md#jsonexceptionout_of_range406) if a high-precision number, or a
float64 when `number_float_t` is `float`, does not fit into `number_float_t`
- Throws [out_of_range.408](../../home/exceptions.md#jsonexceptionout_of_range408) if the count of an optimized
container exceeds the capacity of the container (or of `std::size_t`), or if an optimized array of `Z`, `T`, or `F` has
more than 1,048,576 elements
## Complexity ## Complexity
+1 -1
View File
@@ -45,7 +45,7 @@ Constant.
#include <iostream> #include <iostream>
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
using json = nlohmann::json; using nlohmann::json;
int main() int main()
{ {
+1
View File
@@ -121,6 +121,7 @@ The class satisfies the following concept requirements:
- [**json_pointer**](../json_pointer/index.md) - JSON Pointer implementation - [**json_pointer**](../json_pointer/index.md) - JSON Pointer implementation
- [**json_serializer**](json_serializer.md) - type of the serializer to for conversions from/to JSON - [**json_serializer**](json_serializer.md) - type of the serializer to for conversions from/to JSON
- [**error_handler_t**](error_handler_t.md) - type to choose behavior on decoding errors - [**error_handler_t**](error_handler_t.md) - type to choose behavior on decoding errors
- [**bjdata_version_t**](bjdata_version_t.md) - type to choose the BJData version to write
- [**cbor_tag_handler_t**](cbor_tag_handler_t.md) - type to choose how to handle CBOR tags - [**cbor_tag_handler_t**](cbor_tag_handler_t.md) - type to choose how to handle CBOR tags
- **initializer_list_t** - type for initializer lists of `basic_json` values - **initializer_list_t** - type for initializer lists of `basic_json` values
- [**input_format_t**](input_format_t.md) - type to choose the format to parse - [**input_format_t**](input_format_t.md) - type to choose the format to parse
+5 -2
View File
@@ -43,7 +43,8 @@ The exact mapping and its limitations are described on a [dedicated page](../../
`#!cpp false` by default. `#!cpp false` by default.
`version` (in) `version` (in)
: which version of BJData to use (see note on "Binary values" on [BJData](../../features/binary_formats/bjdata.md)); : which version of BJData to use, see [`bjdata_version_t`](bjdata_version_t.md) and the note on "Binary values" on
[BJData](../../features/binary_formats/bjdata.md);
optional, `#!cpp bjdata_version_t::draft2` by default. optional, `#!cpp bjdata_version_t::draft2` by default.
`error_handler` (in) `error_handler` (in)
@@ -64,7 +65,9 @@ Strong guarantee: if an exception is thrown, there are no changes in the JSON va
## Exceptions ## Exceptions
- Throws [`other_error.502`](../../home/exceptions.md#jsonexceptionother_error502) if `use_type` is true and `use_size` - Throws [`other_error.502`](../../home/exceptions.md#jsonexceptionother_error502) if `use_type` is true and `use_size`
is false, and `j` contains a non-empty array, object, or binary value. is false, and `j` contains a non-empty array or object, or a non-empty binary value (with
`version = bjdata_version_t::draft3`, any binary value, including an empty one).
Scalars, empty arrays, and empty objects do not throw.
- Throws [type_error.316](../../home/exceptions.md#jsonexceptiontype_error316) if a string or object key in `j` is - Throws [type_error.316](../../home/exceptions.md#jsonexceptiontype_error316) if a string or object key in `j` is
not valid UTF-8 and `error_handler` is `strict` (the default only if not valid UTF-8 and `error_handler` is `strict` (the default only if
[`JSON_STRICT_BINARY_UTF8`](../macros/json_strict_binary_utf8.md) is enabled) [`JSON_STRICT_BINARY_UTF8`](../macros/json_strict_binary_utf8.md) is enabled)
+1 -1
View File
@@ -13,7 +13,7 @@ static void to_bson(const basic_json& j, detail::output_adapter<char> o,
``` ```
BSON (Binary JSON) is a binary format in which zero or more ordered key/value pairs are stored as a single entity (a BSON (Binary JSON) is a binary format in which zero or more ordered key/value pairs are stored as a single entity (a
so-called document). so-called document). The members of an object are written in the order in which iteration visits them.
1. Returns a byte vector containing the BSON serialization. 1. Returns a byte vector containing the BSON serialization.
2. Writes the BSON serialization to an output adapter. 2. Writes the BSON serialization to an output adapter.
+2 -1
View File
@@ -42,7 +42,8 @@ The exact mapping and its limitations are described on a [dedicated page](../../
## Exception safety ## Exception safety
Strong guarantee: if an exception is thrown, there are no changes in the JSON value. Strong guarantee: if an exception is thrown, there are no changes in the JSON value. Bytes that were already written to
the output adapter are not taken back.
## Exceptions ## Exceptions
@@ -53,7 +53,7 @@ Strong guarantee: if an exception is thrown, there are no changes in the JSON va
value, array, or object exceeds 4294967295, the maximum MessagePack can store; example: value, array, or object exceeds 4294967295, the maximum MessagePack can store; example:
`"MessagePack length 4294967296 exceeds maximum of 4294967295"` `"MessagePack length 4294967296 exceeds maximum of 4294967295"`
- Throws [`out_of_range.415`](../../home/exceptions.md#jsonexceptionout_of_range415) if the subtype of a binary value - Throws [`out_of_range.415`](../../home/exceptions.md#jsonexceptionout_of_range415) if the subtype of a binary value
exceeds 255, the maximum of the MessagePack ext type; example: exceeds 255, the largest value the library can store in the one-byte MessagePack ext type; example:
`"subtype 70000 is too large for the MessagePack ext type (max 255)"` `"subtype 70000 is too large for the MessagePack ext type (max 255)"`
- Throws [type_error.316](../../home/exceptions.md#jsonexceptiontype_error316) if a string or object key in `j` is - Throws [type_error.316](../../home/exceptions.md#jsonexceptiontype_error316) if a string or object key in `j` is
not valid UTF-8 and `error_handler` is `strict` not valid UTF-8 and `error_handler` is `strict`
@@ -12,7 +12,7 @@ void erase(view_type array, I idx);
std::size_t erase(const json_pointer& ptr); std::size_t erase(const json_pointer& ptr);
``` ```
Only an **editable** document (`#!cpp view_mode::editable`, e.g. [`json_editable_document`](../json_editable_document.md)) Only an **editable** document (`#!cpp Editable == true`, e.g. [`json_editable_document`](../json_editable_document.md))
has `erase`; calling it on a read-only `basic_json_document` fails to compile (`#!cpp static_assert`). has `erase`; calling it on a read-only `basic_json_document` fails to compile (`#!cpp static_assert`).
1. Removes every member of `object` whose key is `key` (see [Notes](#notes) on duplicate keys) and returns how many 1. Removes every member of `object` whose key is `key` (see [Notes](#notes) on duplicate keys) and returns how many
@@ -3,7 +3,7 @@
<small>Defined in header `<nlohmann/json_view.hpp>`</small> <small>Defined in header `<nlohmann/json_view.hpp>`</small>
```cpp ```cpp
template<typename BasicJsonType, view_mode Mode = view_mode::read_only> template<typename BasicJsonType, bool Editable = false>
class basic_json_document; class basic_json_document;
``` ```
@@ -20,9 +20,9 @@ is move-only: a copy would duplicate the whole index, the decoded-string buffer,
enough in every use we know of. The index, the decoded strings, and the owned text live in one heap block that the enough in every use we know of. The index, the decoded strings, and the owned text live in one heap block that the
document holds through a `#!cpp std::unique_ptr`; this is also why views survive a move of the document. document holds through a `#!cpp std::unique_ptr`; this is also why views survive a move of the document.
With `#!cpp Mode == view_mode::editable`, the document also offers [`set`](set.md), [`push_back`](push_back.md), With `#!cpp Editable == true`, the document also offers [`set`](set.md), [`push_back`](push_back.md),
[`insert`](insert.md), and [`erase`](erase.md) to change values in place, see [Edits](#edits) below. The source text [`insert`](insert.md), and [`erase`](erase.md) to change values in place, see [Edits](#edits) below. The source text
itself is never written; a read-only document (`#!cpp view_mode::read_only`, the default) does not carry any of the itself is never written; a read-only document (`#!cpp Editable == false`, the default) does not carry any of the
bookkeeping edits need, and calling any of them on one fails to compile (`#!cpp static_assert`). bookkeeping edits need, and calling any of them on one fails to compile (`#!cpp static_assert`).
## Template parameters ## Template parameters
@@ -32,10 +32,9 @@ bookkeeping edits need, and calling any of them on one fails to compile (`#!cpp
[`ordered_json`](../ordered_json.md). Only 64-bit `number_integer_t`/`number_unsigned_t` types are supported; this [`ordered_json`](../ordered_json.md). Only 64-bit `number_integer_t`/`number_unsigned_t` types are supported; this
is checked with a `static_assert`. is checked with a `static_assert`.
`Mode` `Editable`
: a [`view_mode`](../view_mode.md): `#!cpp view_mode::editable` if the document supports [`set`](set.md), : whether the document supports [`set`](set.md), [`push_back`](push_back.md), [`insert`](insert.md), and
[`push_back`](push_back.md), [`insert`](insert.md), and [`erase`](erase.md) (optional, `#!cpp view_mode::read_only` [`erase`](erase.md) (optional, `#!cpp false` by default). See [Edits](#edits) below.
by default). See [Edits](#edits) below.
## Specializations ## Specializations
@@ -48,7 +47,7 @@ bookkeeping edits need, and calling any of them on one fails to compile (`#!cpp
## Member types ## Member types
- **view_type** - the type of view returned by [`root()`](root.md) (`#!cpp basic_json_view<BasicJsonType, Mode>`) - **view_type** - the type of view returned by [`root()`](root.md) (`#!cpp basic_json_view<BasicJsonType, Editable>`)
- **value_t** - the JSON type enumeration, see [`basic_json::value_t`](../basic_json/value_t.md) - **value_t** - the JSON type enumeration, see [`basic_json::value_t`](../basic_json/value_t.md)
- **image_check** - how [`load()`](load.md) validates an image (`full`, `bounds`, `none`), see [`image_check`](load.md#image_check) - **image_check** - how [`load()`](load.md) validates an image (`full`, `bounds`, `none`), see [`image_check`](load.md#image_check)
@@ -81,15 +80,15 @@ bookkeeping edits need, and calling any of them on one fails to compile (`#!cpp
### Edits ### Edits
- [**set**](set.md) - replace a value, or set an object member, an array element, or the value a JSON pointer refers - [**set**](set.md) - replace a value, or set an object member, an array element, or the value a JSON pointer refers
to (editable documents only) to (`#!cpp Editable` documents only)
- [**push_back**](push_back.md) - append to an array (editable documents only) - [**push_back**](push_back.md) - append to an array (`#!cpp Editable` documents only)
- [**insert**](insert.md) - insert an element into an array before a given position (editable documents only) - [**insert**](insert.md) - insert an element into an array before a given position (`#!cpp Editable` documents only)
- [**erase**](erase.md) - remove an object member, an array element, or the value a JSON pointer refers to - [**erase**](erase.md) - remove an object member, an array element, or the value a JSON pointer refers to
(editable documents only) (`#!cpp Editable` documents only)
## Edits ## Edits
An editable document (`#!cpp view_mode::editable`) can be changed after parsing, with [`set`](set.md), An editable document (`#!cpp Editable == true`) can be changed after parsing, with [`set`](set.md),
[`push_back`](push_back.md), [`insert`](insert.md), and [`erase`](erase.md); [`push_back`](push_back.md), [`insert`](insert.md), and [`erase`](erase.md);
[`json_editable_document`](../json_editable_document.md) and [`json_editable_document`](../json_editable_document.md) and
[`ordered_json_editable_document`](../ordered_json_editable_document.md) are the corresponding specializations. A few [`ordered_json_editable_document`](../ordered_json_editable_document.md) are the corresponding specializations. A few
@@ -5,7 +5,7 @@ template<typename I, typename V>
view_type insert(view_type array, I idx, V&& value); view_type insert(view_type array, I idx, V&& value);
``` ```
Only an **editable** document (`#!cpp view_mode::editable`, e.g. [`json_editable_document`](../json_editable_document.md)) Only an **editable** document (`#!cpp Editable == true`, e.g. [`json_editable_document`](../json_editable_document.md))
has `insert`; calling it on a read-only `basic_json_document` fails to compile (`#!cpp static_assert`). has `insert`; calling it on a read-only `basic_json_document` fails to compile (`#!cpp static_assert`).
Inserts `value` into `array` as a new element before position `idx`, which must not be past the end Inserts `value` into `array` as a new element before position `idx`, which must not be past the end
@@ -55,7 +55,10 @@ validate with overload 2 first, if the original vector must survive a failed loa
## Exceptions ## Exceptions
Throws [`parse_error.116`](../../home/exceptions.md#jsonexceptionparse_error116) if `image` is not one On a big-endian target, throws [`type_error.320`](../../home/exceptions.md#jsonexceptiontype_error320) -- the same
exception [`save()`](save.md#exceptions) throws there, since the image format is little-endian only.
Otherwise throws [`parse_error.116`](../../home/exceptions.md#jsonexceptionparse_error116) if `image` is not one
`save()` could have written, or fails the requested `check`: `save()` could have written, or fails the requested `check`:
| message | when | | message | when |
@@ -92,9 +95,6 @@ are checked once, however many nodes refer to it.
## Notes ## Notes
**Little-endian targets only.** On a big-endian target, `load()` does not exist, just like
[`save()`](save.md#notes): the image format is little-endian, so a call fails to compile.
### `image_check` { #image_check } ### `image_check` { #image_check }
```cpp ```cpp
@@ -13,7 +13,7 @@ editable, and it does not have to be `array`'s own document -- which is copied s
source document afterward; a `BasicJsonType` value; or anything `BasicJsonType` can be constructed from (numbers, source document afterward; a `BasicJsonType` value; or anything `BasicJsonType` can be constructed from (numbers,
strings, `#!cpp bool`, `#!cpp nullptr`, containers, ...). strings, `#!cpp bool`, `#!cpp nullptr`, containers, ...).
Only an **editable** document (`#!cpp view_mode::editable`, e.g. [`json_editable_document`](../json_editable_document.md)) Only an **editable** document (`#!cpp Editable == true`, e.g. [`json_editable_document`](../json_editable_document.md))
has `push_back`; calling it on a read-only `basic_json_document` fails to compile (`#!cpp static_assert`). has `push_back`; calling it on a read-only `basic_json_document` fails to compile (`#!cpp static_assert`).
## Template parameters ## Template parameters
@@ -12,7 +12,7 @@ An edited document is written in its *current* state, with its values in documen
parser would have produced them for that JSON text: a member [`set`](set.md) added goes at the end, an parser would have produced them for that JSON text: a member [`set`](set.md) added goes at the end, an
[`erase`](erase.md)d member leaves no trace, and a float that is not finite (NaN or positive/negative infinity) [`erase`](erase.md)d member leaves no trace, and a float that is not finite (NaN or positive/negative infinity)
becomes null, the same substitution [`dump()`](../basic_json_view/dump.md) makes. The same document always saves to becomes null, the same substitution [`dump()`](../basic_json_view/dump.md) makes. The same document always saves to
the same bytes -- also across `BasicJsonType` and the [`view_mode`](../view_mode.md), since the image reflects document order and the same bytes -- also across `BasicJsonType` and `#!cpp Editable`, since the image reflects document order and
values only, not which specialization produced them. values only, not which specialization produced them.
## Return value ## Return value
@@ -31,6 +31,9 @@ Throws [`type_error.320`](../../home/exceptions.md#jsonexceptiontype_error320) i
[discarded](is_discarded.md) -- a default-constructed document, or one a failed [`parse()`](parse.md)/ [discarded](is_discarded.md) -- a default-constructed document, or one a failed [`parse()`](parse.md)/
[`read()`](read.md) with `allow_exceptions == false` left discarded. [`read()`](read.md) with `allow_exceptions == false` left discarded.
On a big-endian target, throws `type_error.320` with a different message instead: the image format is little-endian
only (see [Notes](#notes)).
Throws [`out_of_range.416`](../../home/exceptions.md#jsonexceptionout_of_range416) if the node count, the text, or Throws [`out_of_range.416`](../../home/exceptions.md#jsonexceptionout_of_range416) if the node count, the text, or
the decoded strings of the image would individually reach 4 GiB -- the same 32-bit offsets the decoded strings of the image would individually reach 4 GiB -- the same 32-bit offsets
[`parse()`](parse.md#exceptions) and, for edits, [`set`](set.md)/[`push_back`](push_back.md) are already limited to. [`parse()`](parse.md#exceptions) and, for edits, [`set`](set.md)/[`push_back`](push_back.md) are already limited to.
@@ -41,6 +44,9 @@ the decoded strings of the image would individually reach 4 GiB -- the same 32-b
[json.exception.type_error.320] cannot save a discarded json_document [json.exception.type_error.320] cannot save a discarded json_document
``` ```
``` ```
[json.exception.type_error.320] json_document images need a little-endian target
```
```
[json.exception.out_of_range.416] images of 4 GiB or more are not supported by json_document [json.exception.out_of_range.416] images of 4 GiB or more are not supported by json_document
``` ```
@@ -64,9 +70,9 @@ else -- see [`load`'s Exceptions](load.md#exceptions).
the *same* build on the *same* (little-endian) machine -- not as a long-term storage format. Keep the original the *same* build on the *same* (little-endian) machine -- not as a long-term storage format. Keep the original
JSON text if you need to read a saved document back with a future library version. JSON text if you need to read a saved document back with a future library version.
**Little-endian targets only.** The image is written as raw little-endian bytes, with no byte-swapping. On a **Little-endian only.** The image is written as raw little-endian bytes, with no byte-swapping. `save()` (and
big-endian target, `save()` and [`load`](load.md) do not exist: a call fails to compile rather than produce bytes a [`load`](load.md)) throw `type_error.320` on a big-endian target rather than silently produce bytes a big-endian
big-endian reader could not interpret correctly. reader could not interpret correctly.
## Examples ## Examples
@@ -18,7 +18,7 @@ template<typename V>
view_type set(const json_pointer& ptr, V&& value); view_type set(const json_pointer& ptr, V&& value);
``` ```
Only an **editable** document (`#!cpp view_mode::editable`, e.g. [`json_editable_document`](../json_editable_document.md)) Only an **editable** document (`#!cpp Editable == true`, e.g. [`json_editable_document`](../json_editable_document.md))
has `set`; calling it on a read-only `basic_json_document` fails to compile (`#!cpp static_assert`). has `set`; calling it on a read-only `basic_json_document` fails to compile (`#!cpp static_assert`).
1. Replaces the value `target` refers to with `value`. 1. Replaces the value `target` refers to with `value`.
@@ -3,7 +3,7 @@
<small>Defined in header `<nlohmann/json_view.hpp>`</small> <small>Defined in header `<nlohmann/json_view.hpp>`</small>
```cpp ```cpp
template<typename BasicJsonType, view_mode Mode = view_mode::read_only> template<typename BasicJsonType, bool Editable = false>
class basic_json_view; class basic_json_view;
``` ```
@@ -37,7 +37,7 @@ Moving the document itself does not invalidate its views: the index is heap-allo
`#!cpp operator ValueType()`), and no `#!cpp get_ptr()` or `#!cpp get_ref()`; see [`get`](get.md). `#!cpp operator ValueType()`), and no `#!cpp get_ptr()` or `#!cpp get_ref()`; see [`get`](get.md).
`basic_json_view` itself is always read-only -- it never has a `set` or `push_back` of its own. A view of an `basic_json_view` itself is always read-only -- it never has a `set` or `push_back` of its own. A view of an
**editable** document (`#!cpp view_mode::editable`) sees every edit made through **editable** document (`#!cpp Editable == true`) sees every edit made through
[`basic_json_document::set`](../basic_json_document/set.md) and [`basic_json_document::set`](../basic_json_document/set.md) and
[`basic_json_document::push_back`](../basic_json_document/push_back.md): once a value is changed, every view that [`basic_json_document::push_back`](../basic_json_document/push_back.md): once a value is changed, every view that
still refers to it -- including ones taken before the change -- reads the new value. See still refers to it -- including ones taken before the change -- reads the new value. See
@@ -49,10 +49,9 @@ still refers to it -- including ones taken before the change -- reads the new va
: a specialization of [`basic_json`](../basic_json/index.md), matching the : a specialization of [`basic_json`](../basic_json/index.md), matching the
[`basic_json_document`](../basic_json_document/index.md) the view was taken from. [`basic_json_document`](../basic_json_document/index.md) the view was taken from.
`Mode` `Editable`
: a [`view_mode`](../view_mode.md): whether the view is of an editable document, matching the : whether the view is of an editable document, matching the [`basic_json_document`](../basic_json_document/index.md)
[`basic_json_document`](../basic_json_document/index.md) it was taken from (optional, `#!cpp view_mode::read_only` by it was taken from (optional, `#!cpp false` by default). See [Edits](../basic_json_document/index.md#edits).
default). See [Edits](../basic_json_document/index.md#edits).
## Specializations ## Specializations
@@ -6,7 +6,7 @@ class byte_container_with_subtype : public BinaryType;
``` ```
This type extends the template parameter `BinaryType` provided to [`basic_json`](../basic_json/index.md) with a subtype This type extends the template parameter `BinaryType` provided to [`basic_json`](../basic_json/index.md) with a subtype
used by BSON and MessagePack. This type exists so that the user does not have to specify a type themselves with a used by BSON, CBOR, and MessagePack. This type exists so that the user does not have to specify a type themselves with a
specific naming scheme in order to override the binary type. specific naming scheme in order to override the binary type.
## Template parameters ## Template parameters
+1 -1
View File
@@ -3,7 +3,7 @@
<small>Defined in header `<nlohmann/json_view.hpp>`</small> <small>Defined in header `<nlohmann/json_view.hpp>`</small>
```cpp ```cpp
using json_document = basic_json_document<json, view_mode::read_only>; using json_document = basic_json_document<json>;
``` ```
This type is a [`basic_json_document`](basic_json_document/index.md) of the default [`json`](json.md) This type is a [`basic_json_document`](basic_json_document/index.md) of the default [`json`](json.md)
@@ -3,7 +3,7 @@
<small>Defined in header `<nlohmann/json_view.hpp>`</small> <small>Defined in header `<nlohmann/json_view.hpp>`</small>
```cpp ```cpp
using json_editable_document = basic_json_document<json, view_mode::editable>; using json_editable_document = basic_json_document<json, true>;
``` ```
This type is an **editable** [`basic_json_document`](basic_json_document/index.md) of the default This type is an **editable** [`basic_json_document`](basic_json_document/index.md) of the default
+1 -1
View File
@@ -3,7 +3,7 @@
<small>Defined in header `<nlohmann/json_view.hpp>`</small> <small>Defined in header `<nlohmann/json_view.hpp>`</small>
```cpp ```cpp
using json_editable_view = basic_json_view<json, view_mode::editable>; using json_editable_view = basic_json_view<json, true>;
``` ```
This type is a [`basic_json_view`](basic_json_view/index.md) of a value of a This type is a [`basic_json_view`](basic_json_view/index.md) of a value of a
+1 -1
View File
@@ -3,7 +3,7 @@
<small>Defined in header `<nlohmann/json_view.hpp>`</small> <small>Defined in header `<nlohmann/json_view.hpp>`</small>
```cpp ```cpp
using json_view = basic_json_view<json, view_mode::read_only>; using json_view = basic_json_view<json>;
``` ```
This type is a [`basic_json_view`](basic_json_view/index.md) of a value of a [`json_document`](json_document.md). This type is a [`basic_json_view`](basic_json_view/index.md) of a value of a [`json_document`](json_document.md).
+2 -2
View File
@@ -38,7 +38,7 @@ Therefore, assertions can be switched off by defining `NDEBUG`.
```cpp ```cpp
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
using json = nlohmann::json; using nlohmann::json;
int main() int main()
{ {
@@ -64,7 +64,7 @@ Therefore, assertions can be switched off by defining `NDEBUG`.
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
using json = nlohmann::json; using nlohmann::json;
int main() int main()
{ {
@@ -79,7 +79,7 @@ The default value is `0` (disabled — existing behavior is preserved).
```cpp ```cpp
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
using json = nlohmann::json; using nlohmann::json;
int main() int main()
{ {
@@ -98,7 +98,7 @@ The default value is `0` (disabled — existing behavior is preserved).
#define JSON_BRACE_INIT_COPY_SEMANTICS 1 #define JSON_BRACE_INIT_COPY_SEMANTICS 1
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
using json = nlohmann::json; using nlohmann::json;
int main() int main()
{ {
@@ -54,7 +54,7 @@ The default value is `0` (disabled, the deprecated functions can still be called
```cpp ```cpp
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
using json = nlohmann::json; using nlohmann::json;
int main() int main()
{ {
@@ -72,7 +72,7 @@ The default value is `0` (disabled, the deprecated functions can still be called
#define JSON_DELETE_DEPRECATED_FUNCTIONS 1 #define JSON_DELETE_DEPRECATED_FUNCTIONS 1
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
using json = nlohmann::json; using nlohmann::json;
int main() int main()
{ {
@@ -39,7 +39,7 @@ The default value is `0`.
#define JSON_DISABLE_ENUM_SERIALIZATION 1 #define JSON_DISABLE_ENUM_SERIALIZATION 1
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
using json = nlohmann::json; using nlohmann::json;
enum class Choice enum class Choice
{ {
@@ -66,7 +66,7 @@ The default value is `0`.
#define JSON_DISABLE_ENUM_SERIALIZATION 1 #define JSON_DISABLE_ENUM_SERIALIZATION 1
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
using json = nlohmann::json; using nlohmann::json;
enum class Choice enum class Choice
{ {
@@ -99,7 +99,7 @@ The default value is `0`.
#define JSON_DISABLE_ENUM_SERIALIZATION 1 #define JSON_DISABLE_ENUM_SERIALIZATION 1
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
using json = nlohmann::json; using nlohmann::json;
enum class Choice enum class Choice
{ {
@@ -92,7 +92,7 @@ The default value is `0`: streams are scanned in bulk.
#include <sstream> #include <sstream>
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
using json = nlohmann::json; using nlohmann::json;
int main() int main()
{ {
@@ -115,7 +115,7 @@ The default value is `0`: streams are scanned in bulk.
#include <sstream> #include <sstream>
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
using json = nlohmann::json; using nlohmann::json;
int main() int main()
{ {
@@ -72,7 +72,7 @@ The default value is `0` (disabled — existing behavior is preserved).
```cpp ```cpp
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
using json = nlohmann::json; using nlohmann::json;
int main() int main()
{ {
@@ -89,7 +89,7 @@ The default value is `0` (disabled — existing behavior is preserved).
#define JSON_DISABLE_TUPLE_REFERENCE_CONVERSION 1 #define JSON_DISABLE_TUPLE_REFERENCE_CONVERSION 1
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
using json = nlohmann::json; using nlohmann::json;
int main() int main()
{ {
@@ -73,7 +73,7 @@ The default value is `0`: the file is locked once and read with the unlocked std
#include <cstdio> #include <cstdio>
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
using json = nlohmann::json; using nlohmann::json;
int main() int main()
{ {
@@ -92,7 +92,7 @@ The default value is `0`: the file is locked once and read with the unlocked std
#include <cstdio> #include <cstdio>
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
using json = nlohmann::json; using nlohmann::json;
int main() int main()
{ {
+1 -1
View File
@@ -61,7 +61,7 @@ By default, `#!cpp JSON_NO_SIMD` is not defined, and vector instructions are use
#include <iostream> #include <iostream>
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
using json = nlohmann::json; using nlohmann::json;
int main() int main()
{ {
@@ -94,7 +94,7 @@ The default value is `0` (disabled — existing behavior is preserved).
#include <sstream> #include <sstream>
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
using json = nlohmann::json; using nlohmann::json;
int main() int main()
{ {
@@ -115,7 +115,7 @@ The default value is `0` (disabled — existing behavior is preserved).
#include <sstream> #include <sstream>
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
using json = nlohmann::json; using nlohmann::json;
int main() int main()
{ {
@@ -64,7 +64,7 @@ The default value is `0` (disabled, the behavior of version 3.12.0 and earlier i
```cpp ```cpp
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
using json = nlohmann::json; using nlohmann::json;
int main() int main()
{ {
@@ -81,7 +81,7 @@ The default value is `0` (disabled, the behavior of version 3.12.0 and earlier i
#define JSON_STRICT_BINARY_UTF8 1 #define JSON_STRICT_BINARY_UTF8 1
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
using json = nlohmann::json; using nlohmann::json;
int main() int main()
{ {
@@ -92,7 +92,7 @@ The default value is `0` (disabled — existing behavior is preserved).
```cpp ```cpp
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
using json = nlohmann::json; using nlohmann::json;
int main() int main()
{ {
@@ -109,7 +109,7 @@ The default value is `0` (disabled — existing behavior is preserved).
#define JSON_STRICT_NUL_HANDLING 1 #define JSON_STRICT_NUL_HANDLING 1
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
using json = nlohmann::json; using nlohmann::json;
int main() int main()
{ {
@@ -77,7 +77,7 @@ The default value is `0` (disabled — existing behavior is preserved).
#include <map> #include <map>
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
using json = nlohmann::json; using nlohmann::json;
enum TaskState { TS_STOPPED, TS_RUNNING, TS_COMPLETED }; enum TaskState { TS_STOPPED, TS_RUNNING, TS_COMPLETED };
@@ -105,7 +105,7 @@ The default value is `0` (disabled — existing behavior is preserved).
#include <map> #include <map>
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
using json = nlohmann::json; using nlohmann::json;
enum TaskState { TS_STOPPED, TS_RUNNING, TS_COMPLETED }; enum TaskState { TS_STOPPED, TS_RUNNING, TS_COMPLETED };
@@ -3,7 +3,7 @@
<small>Defined in header `<nlohmann/json_view.hpp>`</small> <small>Defined in header `<nlohmann/json_view.hpp>`</small>
```cpp ```cpp
using ordered_json_document = basic_json_document<ordered_json, view_mode::read_only>; using ordered_json_document = basic_json_document<ordered_json>;
``` ```
This type is a [`basic_json_document`](basic_json_document/index.md) of the [`ordered_json`](ordered_json.md) This type is a [`basic_json_document`](basic_json_document/index.md) of the [`ordered_json`](ordered_json.md)
@@ -3,7 +3,7 @@
<small>Defined in header `<nlohmann/json_view.hpp>`</small> <small>Defined in header `<nlohmann/json_view.hpp>`</small>
```cpp ```cpp
using ordered_json_editable_document = basic_json_document<ordered_json, view_mode::editable>; using ordered_json_editable_document = basic_json_document<ordered_json, true>;
``` ```
This type is an **editable** [`basic_json_document`](basic_json_document/index.md) of the This type is an **editable** [`basic_json_document`](basic_json_document/index.md) of the
@@ -3,7 +3,7 @@
<small>Defined in header `<nlohmann/json_view.hpp>`</small> <small>Defined in header `<nlohmann/json_view.hpp>`</small>
```cpp ```cpp
using ordered_json_editable_view = basic_json_view<ordered_json, view_mode::editable>; using ordered_json_editable_view = basic_json_view<ordered_json, true>;
``` ```
This type is a [`basic_json_view`](basic_json_view/index.md) of a value of an This type is a [`basic_json_view`](basic_json_view/index.md) of a value of an
+1 -1
View File
@@ -3,7 +3,7 @@
<small>Defined in header `<nlohmann/json_view.hpp>`</small> <small>Defined in header `<nlohmann/json_view.hpp>`</small>
```cpp ```cpp
using ordered_json_view = basic_json_view<ordered_json, view_mode::read_only>; using ordered_json_view = basic_json_view<ordered_json>;
``` ```
This type is a [`basic_json_view`](basic_json_view/index.md) of a value of an This type is a [`basic_json_view`](basic_json_view/index.md) of a value of an
-80
View File
@@ -1,80 +0,0 @@
# <small>nlohmann::</small>view_mode
<small>Defined in header `<nlohmann/json_view.hpp>`</small>
```cpp
enum class view_mode : std::uint8_t
{
read_only,
editable
};
```
The second template parameter of [`basic_json_document`](basic_json_document/index.md) and
[`basic_json_view`](basic_json_view/index.md): whether a document can be changed after parsing, and whether a view is
of such a document.
`read_only`
: the default: the document is parsed once and then only read. It carries none of the bookkeeping edits need, and
calling [`set`](basic_json_document/set.md), [`push_back`](basic_json_document/push_back.md),
[`insert`](basic_json_document/insert.md), or [`erase`](basic_json_document/erase.md) on it fails to compile.
`editable`
: the document also offers [`set`](basic_json_document/set.md), [`push_back`](basic_json_document/push_back.md),
[`insert`](basic_json_document/insert.md), and [`erase`](basic_json_document/erase.md); its views see every edit,
see [Edits](basic_json_document/index.md#edits).
The shortcuts name both modes: [`json_document`](json_document.md) and [`json_view`](json_view.md) are read-only,
[`json_editable_document`](json_editable_document.md) and [`json_editable_view`](json_editable_view.md) are editable
(and likewise for `ordered_json`).
## Notes
The mode is a compile-time property of the document's and the view's type, not a setting of a call, and there is no conversion between the two modes: a read-only view reads a
container in place, while a view of an editable document follows the links that edits leave behind. A function that
accepts views therefore names the modes it accepts in its signature:
```cpp
// views of editable documents only
template<typename BasicJsonType>
void f(basic_json_view<BasicJsonType, view_mode::editable> v);
// views of read-only documents only
template<typename BasicJsonType>
void f(basic_json_view<BasicJsonType, view_mode::read_only> v);
// either
template<typename BasicJsonType, view_mode Mode>
void f(basic_json_view<BasicJsonType, Mode> v);
```
A function that takes a [`json_view`](json_view.md) accepts neither an [`ordered_json_view`](ordered_json_view.md)
nor a [`json_editable_view`](json_editable_view.md). Views of the two modes still compare with
[`operator==`](basic_json_view/operator_eq.md).
## Examples
??? example
The example below shows a function template that accepts the views of both modes, and one that accepts only
views of editable documents.
```cpp
--8<-- "examples/view_mode.cpp"
```
Output:
```
--8<-- "examples/view_mode.output"
```
## See also
- [basic_json_document](basic_json_document/index.md) - a parsed JSON text
- [basic_json_view](basic_json_view/index.md) - a handle to one value of a document
- [json_editable_document](json_editable_document.md) - an editable document of `json`
## Version history
Added in version 3.13.0.
@@ -188,10 +188,13 @@ Note: Some modern features (like C++20 ranges or filesystem support) may be disa
``` ```
- [x] The code is checked with the latest [Cppcheck](https://cppcheck.sourceforge.io) with all warnings enabled. - [x] The code is checked with the latest [Cppcheck](https://cppcheck.sourceforge.io) with all warnings enabled.
- [x] The code is checked with the latest [Clang Static Analyzer](https://clang-analyzer.llvm.org) with 89 enabled - [x] The code is checked with the latest [Clang Static Analyzer](https://clang-analyzer.llvm.org) with 52 enabled
rules. checkers.
- [x] The code is checked with [Infer](https://fbinfer.com). - [x] The code is checked with [Infer](https://fbinfer.com).
- [x] The code is checked with [Codacy](https://app.codacy.com/gh/nlohmann/json/dashboard). - [x] The code is checked with [Codacy](https://app.codacy.com/gh/nlohmann/json/dashboard).
- [x] The code is scanned with [CodeQL](https://codeql.github.com), [Semgrep](https://semgrep.dev), and
[Flawfinder](https://dwheeler.com/flawfinder/) on every push and pull request and on a schedule; the findings are
reported to GitHub's code scanning.
## Dynamic analysis ## Dynamic analysis
@@ -204,6 +207,8 @@ Note: Some modern features (like C++20 ranges or filesystem support) may be disa
- [x] The test suite is executed with [Valgrind](https://valgrind.org) (Memcheck) to detect memory leaks. - [x] The test suite is executed with [Valgrind](https://valgrind.org) (Memcheck) to detect memory leaks.
- [x] The test suite is executed with [Sanitizers](https://github.com/google/sanitizers) (address sanitizer, undefined - [x] The test suite is executed with [Sanitizers](https://github.com/google/sanitizers) (address sanitizer, undefined
behavior sanitizer, integer overflow detection, nullability violations). behavior sanitizer, integer overflow detection, nullability violations).
- [x] The fuzz targets are run with [CIFuzz](https://google.github.io/oss-fuzz/getting-started/continuous-integration/)
on every pull request to catch regressions before they are merged.
## Dependencies ## Dependencies
@@ -223,6 +228,8 @@ Note: Some modern features (like C++20 ranges or filesystem support) may be disa
No release is made while such an alert is open. No release is made while such an alert is open.
- [x] Third-party code included in the repository for testing, such as [doctest](https://github.com/doctest/doctest), - [x] Third-party code included in the repository for testing, such as [doctest](https://github.com/doctest/doctest),
is updated manually. is updated manually.
- [x] The repository is evaluated with the [OpenSSF Scorecard](https://securityscorecards.dev) on a schedule and on
every push to the default branch, and the results are reported to GitHub's code scanning.
## Style check ## Style check
@@ -258,4 +265,21 @@ Note: Some modern features (like C++20 ranges or filesystem support) may be disa
- [x] The library is tested against relevant CMake versions: - [x] The library is tested against relevant CMake versions:
- CMake 3.5 (the earliest supported) - CMake 3.5 (the earliest supported)
- CMake 3.31.6 (the latest 3.x release) - CMake 3.31.6 (the latest 3.x release)
- CMake 4.0.0 (a very recent release) - CMake 4.0.0 (the first 4.x release)
!!! success "Requirement: Other build systems"
The library can be used with build systems and package managers other than CMake.
- [x] The library can be consumed with [Meson](https://json.nlohmann.me/integration/package_managers/) and the Swift
Package Manager; the installation with Meson and the resolution of `Package.swift` are tested in the CI.
!!! success "Requirement: Reproducible and verifiable builds"
The library can be built and tested without network access or a Git checkout, and its licensing is machine-readable.
- [x] The test suite is executed from an exported source tree without the `.git` directory and with the test data
being available offline.
- [x] The test suite is executed without the tests that change installed files to check that builds are reproducible.
- [x] The repository is checked for compliance with the [REUSE specification](https://reuse.software) so that the
license and copyright of every file is machine-readable.
- [x] The documentation is built with warnings treated as errors, and its links are checked regularly.
+1 -1
View File
@@ -2,7 +2,7 @@
#include <iomanip> #include <iomanip>
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
using json = nlohmann::json; using nlohmann::json;
int main() int main()
{ {
@@ -2,7 +2,7 @@
#include <vector> #include <vector>
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
using json = nlohmann::json; using nlohmann::json;
int main() int main()
{ {
+3 -3
View File
@@ -2,19 +2,19 @@
#include <iomanip> #include <iomanip>
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
using json = nlohmann::json; using nlohmann::json;
int main() int main()
{ {
// a valid JSON text // a valid JSON text
auto valid_text = R"( const auto* valid_text = R"(
{ {
"numbers": [1, 2, 3] "numbers": [1, 2, 3]
} }
)"; )";
// an invalid JSON text // an invalid JSON text
auto invalid_text = R"( const auto* invalid_text = R"(
{ {
"strings": ["extra", "comma", ] "strings": ["extra", "comma", ]
} }
@@ -2,7 +2,7 @@
#include <string> #include <string>
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
using json = nlohmann::json; using nlohmann::json;
int main() int main()
{ {
+1 -1
View File
@@ -1,7 +1,7 @@
#include <iostream> #include <iostream>
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
using json = nlohmann::json; using nlohmann::json;
int main() int main()
{ {
+2 -2
View File
@@ -2,9 +2,9 @@
#include <iomanip> #include <iomanip>
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
using json = nlohmann::json; using nlohmann::json;
int main() int main()
{ {
std::cout << std::boolalpha << std::is_same<std::vector<json>, json::array_t>::value << std::endl; std::cout << std::boolalpha << std::is_same<std::vector<json>, json::array_t>::value << '\n';
} }
@@ -1,7 +1,7 @@
#include <iostream> #include <iostream>
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
using json = nlohmann::json; using nlohmann::json;
using namespace nlohmann::literals; using namespace nlohmann::literals;
int main() int main()
@@ -1,7 +1,7 @@
#include <iostream> #include <iostream>
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
using json = nlohmann::json; using nlohmann::json;
using namespace nlohmann::literals; using namespace nlohmann::literals;
int main() int main()
@@ -3,7 +3,7 @@
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
using namespace std::string_view_literals; using namespace std::string_view_literals;
using json = nlohmann::json; using nlohmann::json;
int main() int main()
{ {
@@ -3,7 +3,7 @@
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
using namespace std::string_view_literals; using namespace std::string_view_literals;
using json = nlohmann::json; using nlohmann::json;
int main() int main()
{ {
@@ -1,7 +1,7 @@
#include <iostream> #include <iostream>
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
using json = nlohmann::json; using nlohmann::json;
int main() int main()
{ {
@@ -1,7 +1,7 @@
#include <iostream> #include <iostream>
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
using json = nlohmann::json; using nlohmann::json;
int main() int main()
{ {
+1 -1
View File
@@ -1,7 +1,7 @@
#include <iostream> #include <iostream>
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
using json = nlohmann::json; using nlohmann::json;
int main() int main()
{ {
@@ -1,7 +1,7 @@
#include <iostream> #include <iostream>
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
using json = nlohmann::json; using nlohmann::json;
int main() int main()
{ {
+1 -1
View File
@@ -1,7 +1,7 @@
#include <iostream> #include <iostream>
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
using json = nlohmann::json; using nlohmann::json;
int main() int main()
{ {
@@ -1,8 +1,8 @@
#include <iostream> #include <iostream>
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
using json = nlohmann::json; using nlohmann::json;
using ordered_json = nlohmann::ordered_json; using nlohmann::ordered_json;
int main() int main()
{ {
@@ -8,7 +8,7 @@
#include <valarray> #include <valarray>
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
using json = nlohmann::json; using nlohmann::json;
int main() int main()
{ {
@@ -1,7 +1,7 @@
#include <iostream> #include <iostream>
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
using json = nlohmann::json; using nlohmann::json;
int main() int main()
{ {
@@ -1,7 +1,7 @@
#include <iostream> #include <iostream>
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
using json = nlohmann::json; using nlohmann::json;
int main() int main()
{ {
@@ -1,7 +1,7 @@
#include <iostream> #include <iostream>
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
using json = nlohmann::json; using nlohmann::json;
int main() int main()
{ {
@@ -1,7 +1,7 @@
#include <iostream> #include <iostream>
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
using json = nlohmann::json; using nlohmann::json;
int main() int main()
{ {
@@ -1,7 +1,7 @@
#include <iostream> #include <iostream>
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
using json = nlohmann::json; using nlohmann::json;
int main() int main()
{ {
@@ -1,7 +1,7 @@
#include <iostream> #include <iostream>
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
using json = nlohmann::json; using nlohmann::json;
int main() int main()
{ {
@@ -1,7 +1,7 @@
#include <iostream> #include <iostream>
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
using json = nlohmann::json; using nlohmann::json;
int main() int main()
{ {
@@ -1,7 +1,7 @@
#include <iostream> #include <iostream>
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
using json = nlohmann::json; using nlohmann::json;
int main() int main()
{ {
@@ -2,7 +2,7 @@
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
#include <nlohmann/json_view.hpp> #include <nlohmann/json_view.hpp>
using json_document = nlohmann::json_document; using nlohmann::json_document;
int main() int main()
{ {
@@ -2,7 +2,7 @@
#include <utility> #include <utility>
#include <nlohmann/json_view.hpp> #include <nlohmann/json_view.hpp>
using json_document = nlohmann::json_document; using nlohmann::json_document;
int main() int main()
{ {
@@ -1,9 +1,9 @@
#include <iostream> #include <iostream>
#include <nlohmann/json_view.hpp> #include <nlohmann/json_view.hpp>
using json = nlohmann::json; using nlohmann::json;
using json_editable_document = nlohmann::json_editable_document; using nlohmann::json_editable_document;
using json_editable_view = nlohmann::json_editable_view; using nlohmann::json_editable_view;
int main() int main()
{ {
@@ -1,9 +1,9 @@
#include <iostream> #include <iostream>
#include <nlohmann/json_view.hpp> #include <nlohmann/json_view.hpp>
using json = nlohmann::json; using nlohmann::json;
using json_editable_document = nlohmann::json_editable_document; using nlohmann::json_editable_document;
using json_editable_view = nlohmann::json_editable_view; using nlohmann::json_editable_view;
int main() int main()
{ {
@@ -1,7 +1,7 @@
#include <iostream> #include <iostream>
#include <nlohmann/json_view.hpp> #include <nlohmann/json_view.hpp>
using json_document = nlohmann::json_document; using nlohmann::json_document;
int main() int main()
{ {
@@ -4,8 +4,8 @@
#include <vector> #include <vector>
#include <nlohmann/json_view.hpp> #include <nlohmann/json_view.hpp>
using json = nlohmann::json; using nlohmann::json;
using json_document = nlohmann::json_document; using nlohmann::json_document;
using image_check = json_document::image_check; using image_check = json_document::image_check;
int main() int main()
@@ -2,7 +2,7 @@
#include <string> #include <string>
#include <nlohmann/json_view.hpp> #include <nlohmann/json_view.hpp>
using json_document = nlohmann::json_document; using nlohmann::json_document;
int main() int main()
{ {
@@ -1,7 +1,7 @@
#include <iostream> #include <iostream>
#include <nlohmann/json_view.hpp> #include <nlohmann/json_view.hpp>
using json_document = nlohmann::json_document; using nlohmann::json_document;
int main() int main()
{ {
@@ -2,7 +2,7 @@
#include <string> #include <string>
#include <nlohmann/json_view.hpp> #include <nlohmann/json_view.hpp>
using json_document = nlohmann::json_document; using nlohmann::json_document;
int main() int main()
{ {
@@ -3,7 +3,7 @@
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
#include <nlohmann/json_view.hpp> #include <nlohmann/json_view.hpp>
using json_document = nlohmann::json_document; using nlohmann::json_document;
int main() int main()
{ {
@@ -1,7 +1,7 @@
#include <iostream> #include <iostream>
#include <nlohmann/json_view.hpp> #include <nlohmann/json_view.hpp>
using json_document = nlohmann::json_document; using nlohmann::json_document;
json_document parse_from_temporary_buffer() json_document parse_from_temporary_buffer()
{ {
@@ -2,7 +2,7 @@
#include <vector> #include <vector>
#include <nlohmann/json_view.hpp> #include <nlohmann/json_view.hpp>
using json_document = nlohmann::json_document; using nlohmann::json_document;
int main() int main()
{ {
@@ -1,8 +1,8 @@
#include <iostream> #include <iostream>
#include <nlohmann/json_view.hpp> #include <nlohmann/json_view.hpp>
using json = nlohmann::json; using nlohmann::json;
using json_editable_document = nlohmann::json_editable_document; using nlohmann::json_editable_document;
int main() int main()
{ {
@@ -3,7 +3,7 @@
#include <vector> #include <vector>
#include <nlohmann/json_view.hpp> #include <nlohmann/json_view.hpp>
using json_document = nlohmann::json_document; using nlohmann::json_document;
int main() int main()
{ {
@@ -1,7 +1,7 @@
#include <iostream> #include <iostream>
#include <nlohmann/json_view.hpp> #include <nlohmann/json_view.hpp>
using json_document = nlohmann::json_document; using nlohmann::json_document;
int main() int main()
{ {
@@ -4,7 +4,7 @@
#include <vector> #include <vector>
#include <nlohmann/json_view.hpp> #include <nlohmann/json_view.hpp>
using json_document = nlohmann::json_document; using nlohmann::json_document;
int main() int main()
{ {
@@ -1,9 +1,9 @@
#include <iostream> #include <iostream>
#include <nlohmann/json_view.hpp> #include <nlohmann/json_view.hpp>
using json = nlohmann::json; using nlohmann::json;
using json_editable_document = nlohmann::json_editable_document; using nlohmann::json_editable_document;
using json_editable_view = nlohmann::json_editable_view; using nlohmann::json_editable_view;
int main() int main()
{ {
@@ -3,7 +3,7 @@
#include <string> #include <string>
#include <nlohmann/json_view.hpp> #include <nlohmann/json_view.hpp>
using json_document = nlohmann::json_document; using nlohmann::json_document;
int main() int main()
{ {
@@ -2,7 +2,7 @@
#include <string> #include <string>
#include <nlohmann/json_view.hpp> #include <nlohmann/json_view.hpp>
using json_document = nlohmann::json_document; using nlohmann::json_document;
int main() int main()
{ {
@@ -1,8 +1,8 @@
#include <iostream> #include <iostream>
#include <nlohmann/json_view.hpp> #include <nlohmann/json_view.hpp>
using json_document = nlohmann::json_document; using nlohmann::json_document;
using json_view = nlohmann::json_view; using nlohmann::json_view;
int main() int main()
{ {
@@ -1,7 +1,7 @@
#include <iostream> #include <iostream>
#include <nlohmann/json_view.hpp> #include <nlohmann/json_view.hpp>
using json_document = nlohmann::json_document; using nlohmann::json_document;
using json_pointer = nlohmann::json::json_pointer; using json_pointer = nlohmann::json::json_pointer;
int main() int main()
@@ -1,7 +1,7 @@
#include <iostream> #include <iostream>
#include <nlohmann/json_view.hpp> #include <nlohmann/json_view.hpp>
using json_document = nlohmann::json_document; using nlohmann::json_document;
int main() int main()
{ {
@@ -1,7 +1,7 @@
#include <iostream> #include <iostream>
#include <nlohmann/json_view.hpp> #include <nlohmann/json_view.hpp>
using json_document = nlohmann::json_document; using nlohmann::json_document;
int main() int main()
{ {
@@ -2,8 +2,8 @@
#include <numeric> #include <numeric>
#include <nlohmann/json_view.hpp> #include <nlohmann/json_view.hpp>
using json_document = nlohmann::json_document; using nlohmann::json_document;
using json_view = nlohmann::json_view; using nlohmann::json_view;
int main() int main()
{ {
@@ -2,8 +2,8 @@
#include <iostream> #include <iostream>
#include <nlohmann/json_view.hpp> #include <nlohmann/json_view.hpp>
using json_document = nlohmann::json_document; using nlohmann::json_document;
using json_view = nlohmann::json_view; using nlohmann::json_view;
int main() int main()
{ {
@@ -1,7 +1,7 @@
#include <iostream> #include <iostream>
#include <nlohmann/json_view.hpp> #include <nlohmann/json_view.hpp>
using json_document = nlohmann::json_document; using nlohmann::json_document;
int main() int main()
{ {
@@ -1,7 +1,7 @@
#include <iostream> #include <iostream>
#include <nlohmann/json_view.hpp> #include <nlohmann/json_view.hpp>
using json_document = nlohmann::json_document; using nlohmann::json_document;
using json_pointer = nlohmann::json::json_pointer; using json_pointer = nlohmann::json::json_pointer;
int main() int main()
Loaded 100 of 479 files, more files were not shown because too many files have changed in this diff. Show more