|
◆ sax_parse() [2/3]
template<template< typename U, typename V, typename... Args > class ObjectType = std::map, template< typename U, typename... Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer, class BinaryType = std::vector<std::uint8_t>>
template<typename InputType , typename SAX >
static bool nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer, BinaryType >::sax_parse |
( |
InputType && |
i, |
|
|
SAX * |
sax, |
|
|
input_format_t |
format = input_format_t::json , |
|
|
const bool |
strict = true , |
|
|
const bool |
ignore_comments = false |
|
) |
| |
|
inlinestatic |
The SAX event lister must follow the interface of json_sax.
This function reads from a compatible input. Examples are:
- an std::istream object
- a FILE pointer
- a C-style array of characters
- a pointer to a null-terminated string of single byte characters
- an object obj for which begin(obj) and end(obj) produces a valid pair of iterators.
- Parameters
-
[in] | i | input to read from |
[in,out] | sax | SAX event listener |
[in] | format | the format to parse (JSON, CBOR, MessagePack, or UBJSON) |
[in] | strict | whether the input has to be consumed completely |
[in] | ignore_comments | whether comments should be ignored and treated like whitespace (true) or yield a parse error (true); (optional, false by default); only applies to the JSON file format. |
- Returns
- return value of the last processed SAX event
- Exceptions
-
parse_error.101 | if a parse error occurs; example: ""unexpected end of input; expected string literal"" |
parse_error.102 | if to_unicode fails or surrogate error |
parse_error.103 | if to_unicode fails |
- Complexity
- Linear in the length of the input. The parser is a predictive LL(1) parser. The complexity can be higher if the SAX consumer sax has a super-linear complexity.
- Note
- A UTF-8 byte order mark is silently ignored.
- Example
- The example below demonstrates the
sax_parse() function reading from string and processing the events with a user-defined SAX event consumer.
4 #include <nlohmann/json.hpp>
14 std::vector<std::string> events;
18 events.push_back( "value: null");
22 bool boolean( bool val) override
24 events.push_back( "value: " + std::string(val ? "true" : "false"));
30 events.push_back( "value: " + std::to_string(val));
36 events.push_back( "value: " + std::to_string(val));
42 events.push_back( "value: " + s);
48 events.push_back( "value: " + val);
52 bool start_object(std::size_t elements) override
54 events.push_back( "start: object");
58 bool end_object() override
60 events.push_back( "end: object");
64 bool start_array(std::size_t elements) override
66 events.push_back( "start: array");
70 bool end_array() override
72 events.push_back( "end: array");
78 events.push_back( "key: " + val);
84 events.push_back( "binary");
90 events.push_back( "error: " + std::string(ex.what()));
103 "Title": "View from 15th Floor",
105 "Url": "http://www.example.com/image/481989943",
110 "IDs": [116, 943, 234, 38793],
111 "Distance": 12.723374634
117 sax_event_consumer sec;
123 for ( auto& event : sec.events)
125 std::cout << "(" << event << ") ";
129 std::cout << "\nresult: " << std::boolalpha << result << std::endl;
NumberIntegerType number_integer_t a type for a number (integer)
static bool sax_parse(InputType &&i, SAX *sax, input_format_t format=input_format_t::json, const bool strict=true, const bool ignore_comments=false) generate SAX events
detail::exception exception general exception of the basic_json class
json_sax< basic_json > json_sax_t SAX interface type, see nlohmann::json_sax.
StringType string_t a type for a string
detail::parse_error parse_error exception indicating a parse error
NumberFloatType number_float_t a type for a number (floating-point)
nlohmann::byte_container_with_subtype< BinaryType > binary_t a type for a packed binary type
NumberUnsignedType number_unsigned_t a type for a number (unsigned)
basic_json<> json default JSON class
Output (play with this example online): (start: object) (key: Image) (start: object) (key: Width) (value: 800) (key: Height) (value: 600) (key: Title) (value: View from 15th Floor) (key: Thumbnail) (start: object) (key: Url) (value: http://www.example.com/image/481989943) (key: Height) (value: 125) (key: Width) (value: 100) (end: object) (key: Animated) (value: false) (key: IDs) (start: array) (value: 116) (value: 943) (value: 234) (value: 38793) (end: array) (key: Distance) (value: 12.723374634) (end: object) (end: object)
result: true
The example code above can be translated withg++ -std=c++11 -Isingle_include doc/examples/sax_parse.cpp -o sax_parse
- Since
- version 3.2.0
Definition at line 24451 of file json.hpp.
|