|
◆ sax_parse() [1/2]
template<typename SAX >
static bool nlohmann::basic_json::sax_parse |
( |
detail::input_adapter && |
i, |
|
|
SAX * |
sax, |
|
|
input_format_t |
format = input_format_t::json , |
|
|
const bool |
strict = true |
|
) |
| |
|
inlinestatic |
The SAX event lister must follow the interface of json_sax.
This function reads from a compatible input. Examples are:
- an array of 1-byte values
- strings with character/literal type with size of 1 byte
- input streams
- container with contiguous storage of 1-byte values. Compatible container types include
std::vector , std::string , std::array , std::valarray , and std::initializer_list . Furthermore, C-style arrays can be used with std::begin() /std::end() . User-defined containers can be used as long as they implement random-access iterators and a contiguous storage.
- Precondition
- Each element of the container has a size of 1 byte. Violating this precondition yields undefined behavior. This precondition is enforced with a static assertion.
-
The container storage is contiguous. Violating this precondition yields undefined behavior. This precondition is enforced with an assertion.
- Warning
- There is no way to enforce all preconditions at compile-time. If the function is called with a noncompliant container and with assertions switched off, the behavior is undefined and will most likely yield segmentation violation.
- 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 |
- 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( "error: " + std::string(ex.what()));
97 "Title": "View from 15th Floor",
99 "Url": "http://www.example.com/image/481989943",
104 "IDs": [116, 943, 234, 38793],
105 "Distance": 12.723374634
111 sax_event_consumer sec;
117 for ( auto& event : sec.events)
119 std::cout << "(" << event << ") ";
123 std::cout << "\nresult: " << std::boolalpha << result << std::endl;
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 20824 of file json.hpp.
|