another experiment
This commit is contained in:
parent
8b833c452a
commit
04c6c886eb
5 changed files with 163 additions and 38 deletions
28
doc/examples/parse__array__parser_callback_t.cpp
Normal file
28
doc/examples/parse__array__parser_callback_t.cpp
Normal file
|
@ -0,0 +1,28 @@
|
||||||
|
#include <json.hpp>
|
||||||
|
|
||||||
|
using json = nlohmann::json;
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
// a JSON text
|
||||||
|
char text[] = R"(
|
||||||
|
{
|
||||||
|
"Image": {
|
||||||
|
"Width": 800,
|
||||||
|
"Height": 600,
|
||||||
|
"Title": "View from 15th Floor",
|
||||||
|
"Thumbnail": {
|
||||||
|
"Url": "http://www.example.com/image/481989943",
|
||||||
|
"Height": 125,
|
||||||
|
"Width": 100
|
||||||
|
},
|
||||||
|
"Animated" : false,
|
||||||
|
"IDs": [116, 943, 234, 38793]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)";
|
||||||
|
|
||||||
|
// parse and serialize JSON
|
||||||
|
json j_complete = json::parse(text);
|
||||||
|
std::cout << std::setw(4) << j_complete << "\n\n";
|
||||||
|
}
|
1
doc/examples/parse__array__parser_callback_t.link
Normal file
1
doc/examples/parse__array__parser_callback_t.link
Normal file
|
@ -0,0 +1 @@
|
||||||
|
<a target="_blank" href="http://melpon.org/wandbox/permlink/CwZnqGqte14SYJ5s"><b>online</b></a>
|
20
doc/examples/parse__array__parser_callback_t.output
Normal file
20
doc/examples/parse__array__parser_callback_t.output
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
{
|
||||||
|
"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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
108
src/json.hpp
108
src/json.hpp
|
@ -5876,6 +5876,78 @@ class basic_json
|
||||||
/// @name deserialization
|
/// @name deserialization
|
||||||
/// @{
|
/// @{
|
||||||
|
|
||||||
|
/*!
|
||||||
|
@brief deserialize from an array
|
||||||
|
|
||||||
|
This function reads from an array of 1-byte values.
|
||||||
|
|
||||||
|
@pre 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.**
|
||||||
|
|
||||||
|
@param[in] array array to read from
|
||||||
|
@param[in] cb a parser callback function of type @ref parser_callback_t
|
||||||
|
which is used to control the deserialization by filtering unwanted values
|
||||||
|
(optional)
|
||||||
|
|
||||||
|
@return result of the deserialization
|
||||||
|
|
||||||
|
@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
|
||||||
|
@a cb has a super-linear complexity.
|
||||||
|
|
||||||
|
@note A UTF-8 byte order mark is silently ignored.
|
||||||
|
|
||||||
|
@liveexample{The example below demonstrates the `parse()` function reading
|
||||||
|
from an array.,parse__array__parser_callback_t}
|
||||||
|
|
||||||
|
@since version 2.0.3
|
||||||
|
*/
|
||||||
|
template<class T, std::size_t N>
|
||||||
|
static basic_json parse(T (&array)[N],
|
||||||
|
const parser_callback_t cb = nullptr)
|
||||||
|
{
|
||||||
|
// delegate the call to the iterator-range parse overload
|
||||||
|
return parse(std::begin(array), std::end(array), cb);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*!
|
||||||
|
@brief deserialize from string literal
|
||||||
|
|
||||||
|
@tparam CharT character/literal type with size of 1 byte
|
||||||
|
@param[in] s string literal to read a serialized JSON value from
|
||||||
|
@param[in] cb a parser callback function of type @ref parser_callback_t
|
||||||
|
which is used to control the deserialization by filtering unwanted values
|
||||||
|
(optional)
|
||||||
|
|
||||||
|
@return result of the deserialization
|
||||||
|
|
||||||
|
@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
|
||||||
|
@a cb has a super-linear complexity.
|
||||||
|
|
||||||
|
@note A UTF-8 byte order mark is silently ignored.
|
||||||
|
@note String containers like `std::string` or @ref string_t can be parsed
|
||||||
|
with @ref parse(const ContiguousContainer&, const parser_callback_t)
|
||||||
|
|
||||||
|
@liveexample{The example below demonstrates the `parse()` function with
|
||||||
|
and without callback function.,parse__string__parser_callback_t}
|
||||||
|
|
||||||
|
@sa @ref parse(std::istream&, const parser_callback_t) for a version that
|
||||||
|
reads from an input stream
|
||||||
|
|
||||||
|
@since version 1.0.0 (originally for @ref string_t)
|
||||||
|
*/
|
||||||
|
template<typename CharPT, typename std::enable_if<
|
||||||
|
std::is_pointer<CharPT>::value and
|
||||||
|
std::is_integral<typename std::remove_pointer<CharPT>::type>::value and
|
||||||
|
sizeof(std::remove_pointer<CharPT>) == 1, int>::type = 0>
|
||||||
|
static basic_json parse(const CharPT s,
|
||||||
|
const parser_callback_t cb = nullptr)
|
||||||
|
{
|
||||||
|
return parser(reinterpret_cast<const char*>(s), cb).parse();
|
||||||
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
@brief deserialize from stream
|
@brief deserialize from stream
|
||||||
|
|
||||||
|
@ -6027,6 +6099,7 @@ class basic_json
|
||||||
@since version 2.0.3
|
@since version 2.0.3
|
||||||
*/
|
*/
|
||||||
template<class ContiguousContainer, typename std::enable_if<
|
template<class ContiguousContainer, typename std::enable_if<
|
||||||
|
not std::is_pointer<ContiguousContainer>::value and
|
||||||
std::is_base_of<
|
std::is_base_of<
|
||||||
std::random_access_iterator_tag,
|
std::random_access_iterator_tag,
|
||||||
typename std::iterator_traits<decltype(std::begin(std::declval<ContiguousContainer const>()))>::iterator_category>::value
|
typename std::iterator_traits<decltype(std::begin(std::declval<ContiguousContainer const>()))>::iterator_category>::value
|
||||||
|
@ -6038,41 +6111,6 @@ class basic_json
|
||||||
return parse(std::begin(c), std::end(c), cb);
|
return parse(std::begin(c), std::end(c), cb);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
|
||||||
@brief deserialize from string literal
|
|
||||||
|
|
||||||
@tparam CharT character/literal type with size of 1 byte
|
|
||||||
@param[in] s string literal to read a serialized JSON value from
|
|
||||||
@param[in] cb a parser callback function of type @ref parser_callback_t
|
|
||||||
which is used to control the deserialization by filtering unwanted values
|
|
||||||
(optional)
|
|
||||||
|
|
||||||
@return result of the deserialization
|
|
||||||
|
|
||||||
@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
|
|
||||||
@a cb has a super-linear complexity.
|
|
||||||
|
|
||||||
@note A UTF-8 byte order mark is silently ignored.
|
|
||||||
@note String containers like `std::string` or @ref string_t can be parsed
|
|
||||||
with @ref parse(const ContiguousContainer&, const parser_callback_t)
|
|
||||||
|
|
||||||
@liveexample{The example below demonstrates the `parse()` function with
|
|
||||||
and without callback function.,parse__string__parser_callback_t}
|
|
||||||
|
|
||||||
@sa @ref parse(std::istream&, const parser_callback_t) for a version that
|
|
||||||
reads from an input stream
|
|
||||||
|
|
||||||
@since version 1.0.0 (originally for @ref string_t)
|
|
||||||
*/
|
|
||||||
template<typename CharT, typename std::enable_if<
|
|
||||||
std::is_integral<CharT>::value and sizeof(CharT) == 1, int>::type = 0>
|
|
||||||
static basic_json parse(const CharT* s,
|
|
||||||
const parser_callback_t cb = nullptr)
|
|
||||||
{
|
|
||||||
return parser(reinterpret_cast<const char*>(s), cb).parse();
|
|
||||||
}
|
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
@brief deserialize from stream
|
@brief deserialize from stream
|
||||||
|
|
||||||
|
|
|
@ -5876,6 +5876,41 @@ class basic_json
|
||||||
/// @name deserialization
|
/// @name deserialization
|
||||||
/// @{
|
/// @{
|
||||||
|
|
||||||
|
/*!
|
||||||
|
@brief deserialize from an array
|
||||||
|
|
||||||
|
This function reads from an array of 1-byte values.
|
||||||
|
|
||||||
|
@pre 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.**
|
||||||
|
|
||||||
|
@param[in] array array to read from
|
||||||
|
@param[in] cb a parser callback function of type @ref parser_callback_t
|
||||||
|
which is used to control the deserialization by filtering unwanted values
|
||||||
|
(optional)
|
||||||
|
|
||||||
|
@return result of the deserialization
|
||||||
|
|
||||||
|
@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
|
||||||
|
@a cb has a super-linear complexity.
|
||||||
|
|
||||||
|
@note A UTF-8 byte order mark is silently ignored.
|
||||||
|
|
||||||
|
@liveexample{The example below demonstrates the `parse()` function reading
|
||||||
|
from an array.,parse__array__parser_callback_t}
|
||||||
|
|
||||||
|
@since version 2.0.3
|
||||||
|
*/
|
||||||
|
template<class T, std::size_t N>
|
||||||
|
static basic_json parse(T (&array)[N],
|
||||||
|
const parser_callback_t cb = nullptr)
|
||||||
|
{
|
||||||
|
// delegate the call to the iterator-range parse overload
|
||||||
|
return parse(std::begin(array), std::end(array), cb);
|
||||||
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
@brief deserialize from string literal
|
@brief deserialize from string literal
|
||||||
|
|
||||||
|
@ -5903,9 +5938,11 @@ class basic_json
|
||||||
|
|
||||||
@since version 1.0.0 (originally for @ref string_t)
|
@since version 1.0.0 (originally for @ref string_t)
|
||||||
*/
|
*/
|
||||||
template<typename CharT, typename std::enable_if<
|
template<typename CharPT, typename std::enable_if<
|
||||||
std::is_integral<CharT>::value and sizeof(CharT) == 1, int>::type = 0>
|
std::is_pointer<CharPT>::value and
|
||||||
static basic_json parse(const CharT* s,
|
std::is_integral<typename std::remove_pointer<CharPT>::type>::value and
|
||||||
|
sizeof(std::remove_pointer<CharPT>) == 1, int>::type = 0>
|
||||||
|
static basic_json parse(const CharPT s,
|
||||||
const parser_callback_t cb = nullptr)
|
const parser_callback_t cb = nullptr)
|
||||||
{
|
{
|
||||||
return parser(reinterpret_cast<const char*>(s), cb).parse();
|
return parser(reinterpret_cast<const char*>(s), cb).parse();
|
||||||
|
@ -6062,6 +6099,7 @@ class basic_json
|
||||||
@since version 2.0.3
|
@since version 2.0.3
|
||||||
*/
|
*/
|
||||||
template<class ContiguousContainer, typename std::enable_if<
|
template<class ContiguousContainer, typename std::enable_if<
|
||||||
|
not std::is_pointer<ContiguousContainer>::value and
|
||||||
std::is_base_of<
|
std::is_base_of<
|
||||||
std::random_access_iterator_tag,
|
std::random_access_iterator_tag,
|
||||||
typename std::iterator_traits<decltype(std::begin(std::declval<ContiguousContainer const>()))>::iterator_category>::value
|
typename std::iterator_traits<decltype(std::begin(std::declval<ContiguousContainer const>()))>::iterator_category>::value
|
||||||
|
|
Loading…
Reference in a new issue