Add basic_json_view::dump() and the comparison operators, and read floats from the parser's digit layout instead of rescanning the token. dump(indent, indent_char, ensure_ascii, number_format) writes a value the way ordered_json::parse(text).dump() writes it for the same arguments: members in document order, all of them should a key occur more than once; strings escaped by the same rules, using the library's scanning kernels; floats written with the library's to_chars conversion, so the output equals basic_json's byte for byte; integers copied from the source, where they are already canonical, except -0, which parse() reads as 0. There is no error_handler argument, because the view only holds valid UTF-8. number_format::source copies numbers exactly as they appear in the source (e.g. "1.50", "1E2", "-0"), which basic_json cannot provide. operator<< takes the indentation from the stream width, as for basic_json. The writer walks iteratively, so nesting depth is limited by memory only. operator== and operator!= compare two views, or a view and a basic_json value in either order, by the rules basic_json's operator== uses: numbers compare by value across their types, objects compare by their members with duplicate keys resolved as parse() resolves them, member order matters only where the object type keeps one, and discarded views compare as discarded basic_json values do, including under JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON. Nothing is materialized except single scalars. While parsing, the view now records where the integer digits, the fraction digits, and the exponent of a float token are, so floats and doubles with at most 19 digits are read from that layout with the library's decimal_to_float() instead of rescanning the token. Both round correctly, so the values are those of parse(). get<double>(), materialize(), dump(), and the comparisons all use it. Signed-off-by: Niels Lohmann <mail@nlohmann.me>
5.9 KiB
nlohmann::basic_json_view
Defined in header <nlohmann/json_view.hpp>
template<typename BasicJsonType>
class basic_json_view;
A read-only handle to one value of a basic_json_document: two pointers (a pointer
to the document and a pointer into its index), trivially copyable. A view is valid as long as
- the document is alive,
- the document has not been re-parsed with
read()(orparse()into it) or shrunk withshrink_to_fit()since the view was taken, and - if the document borrows its source text, that text is still alive.
Moving the document itself does not invalidate its views: the index is heap-allocated independently of the
basic_json_document object.
basic_json_view provides the read-only part of the BasicJsonType interface: the type-inspection functions, element
access, lookup, iteration, conversion, and comparison -- get<T>(), get_string(),
number_token(), and materialize() to build the BasicJsonType value of a
subtree on demand. operator[], at, contains, and
value also accept a json_pointer. operator== and
operator!= compare two views, or a view and a BasicJsonType value, without ever building a
BasicJsonType value for a view; no ordering comparison (#!cpp operator<) is provided.
Template parameters
BasicJsonType- a specialization of
basic_json, matching thebasic_json_documentthe view was taken from.
Specializations
- json_view - views of a
json_document - ordered_json_view - views of an
ordered_json_document
Member types
- value_t - the JSON type enumeration, see
basic_json::value_t - string_t, number_integer_t, number_unsigned_t, number_float_t, json_pointer - the corresponding
member types of
BasicJsonType - size_type -
#!cpp std::size_t - string_view_t -
#!cpp std::string_viewon C++17 and newer, a minimal internal substitute otherwise - iterator, const_iterator - a forward iterator over the elements of an array or the member values of an object, in document order; both names refer to the same type, since a view is always read-only
- item - a (key, value) pair produced by
items() - number_format - how
dump()writes numbers
Member functions
Object inspection
- type - return the type of the value
- type_name - return the type as string
- is_null - return whether the value is null
- is_boolean - return whether the value is a boolean
- is_number - return whether the value is a number
- is_number_integer - return whether the value is an integer number
- is_number_unsigned - return whether the value is an unsigned integer number
- is_number_float - return whether the value is a floating-point number
- is_string - return whether the value is a string
- is_array - return whether the value is an array
- is_object - return whether the value is an object
- is_binary - return whether the value is a binary array (always
#!cpp false) - is_primitive - return whether the type is primitive
- is_structured - return whether the type is structured
- is_discarded - return whether the view is invalid
- operator bool - return whether the view refers to a value
Element access
- at - access specified element with bounds checking
- operator[] - access specified element
- value - access specified element with default value
- front - access the first element
- back - access the last element
Lookup
- find - find an element in an object
- count - returns the number of occurrences of a key in an object
- contains - check the existence of an element in an object
Iterators
- begin - returns an iterator to the first element
- cbegin - returns a const iterator to the first element
- end - returns an iterator to one past the last element
- cend - returns a const iterator to one past the last element
- items - wrapper to access iterator member functions in range-based for
Capacity
Conversion
- get - get a value
- get_to - get a value and write it to a destination
- get_string - get a string value without a copy
- number_token - get a number's token text without a copy
- materialize - build the
BasicJsonTypevalue of this subtree
Comparison
- operator== - comparison: equal
- operator!= - comparison: not equal
Serialization
- dump - serialize to a JSON-formatted string
- operator<< - serialize to stream
Source access
- source_offset - byte offset of this value in the document's source text
Version history
- Added in version 3.13.0.