|
◆ 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 >
static basic_json nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer, BinaryType >::parse |
( |
InputType && |
i, |
|
|
const parser_callback_t |
cb = nullptr , |
|
|
const bool |
allow_exceptions = true , |
|
|
const bool |
ignore_comments = false |
|
) |
| |
|
inlinestatic |
- Template Parameters
-
InputType | A compatible input, for instance
- 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] | cb | a parser callback function of type parser_callback_t which is used to control the deserialization by filtering unwanted values (optional) |
[in] | allow_exceptions | whether to throw exceptions in case of a parse error (optional, true by default) |
[in] | ignore_comments | whether comments should be ignored and treated like whitespace (true) or yield a parse error (true); (optional, false by default) |
- Returns
- deserialized JSON value; in case of a parse error and allow_exceptions set to
false , the return value will be value_t::discarded.
- 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 parser callback function cb or reading from the input i has a super-linear complexity.
- Note
- A UTF-8 byte order mark is silently ignored.
- Example
- The example below demonstrates the
parse() function reading from an array.
3 #include <nlohmann/json.hpp>
15 "Title": "View from 15th Floor",
17 "Url": "http://www.example.com/image/481989943",
22 "IDs": [116, 943, 234, 38793]
29 std::cout << std::setw(4) << j_complete << "\n\n";
static basic_json parse(InputType &&i, const parser_callback_t cb=nullptr, const bool allow_exceptions=true, const bool ignore_comments=false) deserialize from a compatible input
basic_json<> json default JSON class
Output (play with this example online): {
"Image": {
"Animated": false,
"Height": 600,
"IDs": [
116,
943,
234,
38793
],
"Thumbnail": {
"Height": 125,
"Url": "http://www.example.com/image/481989943",
"Width": 100
},
"Title": "View from 15th Floor",
"Width": 800
}
}
The example code above can be translated withg++ -std=c++11 -Isingle_include doc/examples/parse__array__parser_callback_t.cpp -o parse__array__parser_callback_t
- Example
- The example below demonstrates the
parse() function with and without callback function.
3 #include <nlohmann/json.hpp>
15 "Title": "View from 15th Floor",
17 "Url": "http://www.example.com/image/481989943",
22 "IDs": [116, 943, 234, 38793]
29 std::cout << std::setw(4) << j_complete << "\n\n";
36 if (event == json::parse_event_t::key and parsed == json( "Thumbnail"))
48 std::cout << std::setw(4) << j_filtered << '\n';
detail::parser_callback_t< basic_json > parser_callback_t per-element parser callback type
detail::parse_event_t parse_event_t parser event types
Output (play with this example online): {
"Image": {
"Animated": false,
"Height": 600,
"IDs": [
116,
943,
234,
38793
],
"Thumbnail": {
"Height": 125,
"Url": "http://www.example.com/image/481989943",
"Width": 100
},
"Title": "View from 15th Floor",
"Width": 800
}
}
{
"Image": {
"Animated": false,
"Height": 600,
"IDs": [
116,
943,
234,
38793
],
"Title": "View from 15th Floor",
"Width": 800
}
}
The example code above can be translated withg++ -std=c++11 -Isingle_include doc/examples/parse__string__parser_callback_t.cpp -o parse__string__parser_callback_t
- Example
- The example below demonstrates the
parse() function with and without callback function.
4 #include <nlohmann/json.hpp>
16 "Title": "View from 15th Floor",
18 "Url": "http://www.example.com/image/481989943",
23 "IDs": [116, 943, 234, 38793]
34 std::cout << std::setw(4) << j_complete << "\n\n";
41 if (event == json::parse_event_t::key and parsed == json( "Thumbnail"))
57 std::cout << std::setw(4) << j_filtered << '\n';
Output (play with this example online): {
"Image": {
"Animated": false,
"Height": 600,
"IDs": [
116,
943,
234,
38793
],
"Thumbnail": {
"Height": 125,
"Url": "http://www.example.com/image/481989943",
"Width": 100
},
"Title": "View from 15th Floor",
"Width": 800
}
}
{
"Image": {
"Animated": false,
"Height": 600,
"IDs": [
116,
943,
234,
38793
],
"Title": "View from 15th Floor",
"Width": 800
}
}
The example code above can be translated withg++ -std=c++11 -Isingle_include doc/examples/parse__istream__parser_callback_t.cpp -o parse__istream__parser_callback_t
- Example
- The example below demonstrates the
parse() function reading from a contiguous container.
3 #include <nlohmann/json.hpp>
10 std::vector<uint8_t> text = { '[', '1', ',', '2', ',', '3', ']', '\0'};
14 std::cout << std::setw(4) << j_complete << "\n\n";
Output (play with this example online): [
1,
2,
3
]
The example code above can be translated withg++ -std=c++11 -Isingle_include doc/examples/parse__contiguouscontainer__parser_callback_t.cpp -o parse__contiguouscontainer__parser_callback_t
- Since
- version 2.0.3 (contiguous containers); version 3.9.0 allowed to ignore comments.
Definition at line 24259 of file json.hpp.
|