improved coverage

This commit is contained in:
Niels Lohmann 2018-03-20 22:39:08 +01:00
parent 1e38ffc014
commit 9e1abb4842
No known key found for this signature in database
GPG key ID: 7F3CEA63AE251B69
12 changed files with 412 additions and 180 deletions

View file

@ -41,9 +41,6 @@ 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
@ -61,30 +58,35 @@ class binary_reader
@return
*/
bool sax_parse(const binary_format_t format, json_sax_t* sax_, const bool strict)
bool sax_parse(const input_format_t format,
json_sax_t* sax_,
const bool strict = true)
{
sax = sax_;
bool result;
switch (format)
{
case binary_format_t::cbor:
case input_format_t::cbor:
result = parse_cbor_internal();
break;
case binary_format_t::msgpack:
case input_format_t::msgpack:
result = parse_msgpack_internal();
break;
case binary_format_t::ubjson:
case input_format_t::ubjson:
result = parse_ubjson_internal();
break;
default:
assert(false); // LCOV_EXCL_LINE
}
// strict mode: next byte must be EOF
if (result and strict)
{
if (format == binary_format_t::ubjson)
if (format == input_format_t::ubjson)
{
get_ignore_noop();
}

View file

@ -20,6 +20,9 @@ namespace nlohmann
{
namespace detail
{
/// the supported input formats
enum class input_format_t { json, cbor, msgpack, ubjson };
////////////////////
// input adapters //
////////////////////

View file

@ -105,21 +105,12 @@ struct json_sax
*/
virtual bool end_array() = 0;
/*!
@brief a binary value was read
@param[in] val byte vector
@return whether parsing should proceed
@note examples are CBOR type 2 strings, MessagePack bin, and maybe UBJSON
array<uint8t>
*/
virtual bool binary(const std::vector<uint8_t>& val) = 0;
/*!
@brief a parse error occurred
@param[in] position the position in the input where the error occurs
@param[in] last_token the last read token
@param[in] error_msg a detailed error message
@return whether parsing should proceed
@return whether parsing should proceed (must return false)
*/
virtual bool parse_error(std::size_t position,
const std::string& last_token,
@ -225,11 +216,6 @@ class json_sax_dom_parser : public json_sax<BasicJsonType>
return true;
}
bool binary(const std::vector<uint8_t>&) override
{
return true;
}
bool parse_error(std::size_t, const std::string&,
const detail::exception& ex) override
{
@ -430,11 +416,6 @@ class json_sax_dom_callback_parser : public json_sax<BasicJsonType>
return true;
}
bool binary(const std::vector<uint8_t>&) override
{
return true;
}
bool parse_error(std::size_t, const std::string&,
const detail::exception& ex) override
{
@ -580,11 +561,6 @@ class json_sax_acceptor : public json_sax<BasicJsonType>
return true;
}
bool binary(const std::vector<uint8_t>&) override
{
return true;
}
bool parse_error(std::size_t, const std::string&, const detail::exception&) override
{
return false;

View file

@ -99,7 +99,7 @@ class lexer
}
}
explicit lexer(detail::input_adapter_t adapter)
explicit lexer(detail::input_adapter_t&& adapter)
: ia(std::move(adapter)), decimal_point_char(get_decimal_point()) {}
// delete because of pointer members

View file

@ -60,10 +60,10 @@ class parser
std::function<bool(int depth, parse_event_t event, BasicJsonType& parsed)>;
/// a parser reading from an input adapter
explicit parser(detail::input_adapter_t adapter,
explicit parser(detail::input_adapter_t&& adapter,
const parser_callback_t cb = nullptr,
const bool allow_exceptions_ = true)
: callback(cb), m_lexer(adapter), allow_exceptions(allow_exceptions_)
: callback(cb), m_lexer(std::move(adapter)), allow_exceptions(allow_exceptions_)
{
// read first token
get_token();
@ -160,19 +160,22 @@ class parser
bool accept(const bool strict = true)
{
json_sax_acceptor<BasicJsonType> sax_acceptor;
if (not sax_parse_internal(&sax_acceptor))
{
return false;
}
// strict => last token must be EOF
return not strict or (get_token() == token_type::end_of_input);
return sax_parse(&sax_acceptor, strict);
}
bool sax_parse(json_sax_t* sax)
bool sax_parse(json_sax_t* sax, const bool strict = true)
{
return sax_parse_internal(sax);
const bool result = sax_parse_internal(sax);
// strict mode: next byte must be EOF
if (result and strict and (get_token() != token_type::end_of_input))
{
return sax->parse_error(m_lexer.get_position(),
m_lexer.get_token_string(),
parse_error::create(101, m_lexer.get_position(), exception_message(token_type::end_of_input)));
}
return result;
}
private: