✨ implemented non-throwing binary reader
This commit is contained in:
parent
a271ee5f16
commit
9e07e9b4ec
4 changed files with 252 additions and 170 deletions
|
|
@ -41,6 +41,9 @@ class binary_reader
|
|||
using json_sax_t = json_sax<BasicJsonType>;
|
||||
|
||||
public:
|
||||
/// the supported binary input formats
|
||||
enum class binary_format_t { cbor, msgpack, ubjson };
|
||||
|
||||
/*!
|
||||
@brief create a binary reader
|
||||
|
||||
|
|
@ -52,77 +55,50 @@ class binary_reader
|
|||
}
|
||||
|
||||
/*!
|
||||
@brief create a JSON value from CBOR input
|
||||
|
||||
@param[in] format the binary format to parse
|
||||
@param[in] sax_ a SAX event processor
|
||||
@param[in] strict whether to expect the input to be consumed completed
|
||||
@return JSON value created from CBOR input
|
||||
|
||||
@throw parse_error.110 if input ended unexpectedly or the end of file was
|
||||
not reached when @a strict was set to true
|
||||
@throw parse_error.112 if unsupported byte was read
|
||||
@return
|
||||
*/
|
||||
BasicJsonType parse_cbor(const bool strict)
|
||||
bool sax_parse(const binary_format_t format, json_sax_t* sax_, const bool strict)
|
||||
{
|
||||
BasicJsonType result;
|
||||
json_sax_dom_parser<BasicJsonType> sdp(result);
|
||||
sax = &sdp;
|
||||
parse_cbor_internal();
|
||||
result.assert_invariant();
|
||||
if (strict)
|
||||
sax = sax_;
|
||||
bool result;
|
||||
|
||||
switch (format)
|
||||
{
|
||||
get();
|
||||
expect_eof();
|
||||
case binary_format_t::cbor:
|
||||
result = parse_cbor_internal();
|
||||
break;
|
||||
|
||||
case binary_format_t::msgpack:
|
||||
result = parse_msgpack_internal();
|
||||
break;
|
||||
|
||||
case binary_format_t::ubjson:
|
||||
result = parse_ubjson_internal();
|
||||
break;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/*!
|
||||
@brief create a JSON value from MessagePack input
|
||||
|
||||
@param[in] strict whether to expect the input to be consumed completed
|
||||
@return JSON value created from MessagePack input
|
||||
|
||||
@throw parse_error.110 if input ended unexpectedly or the end of file was
|
||||
not reached when @a strict was set to true
|
||||
@throw parse_error.112 if unsupported byte was read
|
||||
*/
|
||||
BasicJsonType parse_msgpack(const bool strict)
|
||||
{
|
||||
BasicJsonType result;
|
||||
json_sax_dom_parser<BasicJsonType> sdp(result);
|
||||
sax = &sdp;
|
||||
parse_msgpack_internal();
|
||||
result.assert_invariant();
|
||||
if (strict)
|
||||
// strict mode: next byte must be EOF
|
||||
if (result and strict)
|
||||
{
|
||||
get();
|
||||
expect_eof();
|
||||
if (format == binary_format_t::ubjson)
|
||||
{
|
||||
get_ignore_noop();
|
||||
}
|
||||
else
|
||||
{
|
||||
get();
|
||||
}
|
||||
|
||||
if (JSON_UNLIKELY(current != std::char_traits<char>::eof()))
|
||||
{
|
||||
return sax->parse_error(chars_read, get_token_string(), parse_error::create(110, chars_read, "expected end of input"));
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/*!
|
||||
@brief create a JSON value from UBJSON input
|
||||
|
||||
@param[in] strict whether to expect the input to be consumed completed
|
||||
@return JSON value created from UBJSON input
|
||||
|
||||
@throw parse_error.110 if input ended unexpectedly or the end of file was
|
||||
not reached when @a strict was set to true
|
||||
@throw parse_error.112 if unsupported byte was read
|
||||
*/
|
||||
BasicJsonType parse_ubjson(const bool strict)
|
||||
{
|
||||
BasicJsonType result;
|
||||
json_sax_dom_parser<BasicJsonType> sdp(result);
|
||||
sax = &sdp;
|
||||
parse_ubjson_internal();
|
||||
result.assert_invariant();
|
||||
if (strict)
|
||||
{
|
||||
get_ignore_noop();
|
||||
expect_eof();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
|
@ -1662,18 +1638,6 @@ class binary_reader
|
|||
return sax->end_object();
|
||||
}
|
||||
|
||||
/*!
|
||||
@return whether input was completely read
|
||||
*/
|
||||
bool expect_eof() const
|
||||
{
|
||||
if (JSON_UNLIKELY(current != std::char_traits<char>::eof()))
|
||||
{
|
||||
return sax->parse_error(chars_read, get_token_string(), parse_error::create(110, chars_read, "expected end of input"));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/*!
|
||||
@return whether the last read character is not EOF
|
||||
*/
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue