refactored binary readers to use a SAX parser

parent 149d2fd0
...@@ -124,6 +124,8 @@ struct json_sax ...@@ -124,6 +124,8 @@ struct json_sax
}; };
namespace detail
{
template<typename BasicJsonType> template<typename BasicJsonType>
class json_sax_dom_parser : public json_sax<BasicJsonType> class json_sax_dom_parser : public json_sax<BasicJsonType>
{ {
...@@ -172,9 +174,16 @@ class json_sax_dom_parser : public json_sax<BasicJsonType> ...@@ -172,9 +174,16 @@ class json_sax_dom_parser : public json_sax<BasicJsonType>
return true; return true;
} }
bool start_object(std::size_t) override bool start_object(std::size_t len) override
{ {
ref_stack.push_back(handle_value(BasicJsonType::value_t::object)); ref_stack.push_back(handle_value(BasicJsonType::value_t::object));
if (JSON_UNLIKELY(len != json_sax<BasicJsonType>::no_limit and len > ref_stack.back()->max_size()))
{
JSON_THROW(out_of_range::create(408,
"excessive object size: " + std::to_string(len)));
}
return true; return true;
} }
...@@ -191,9 +200,16 @@ class json_sax_dom_parser : public json_sax<BasicJsonType> ...@@ -191,9 +200,16 @@ class json_sax_dom_parser : public json_sax<BasicJsonType>
return true; return true;
} }
bool start_array(std::size_t) override bool start_array(std::size_t len) override
{ {
ref_stack.push_back(handle_value(BasicJsonType::value_t::array)); ref_stack.push_back(handle_value(BasicJsonType::value_t::array));
if (JSON_UNLIKELY(len != json_sax<BasicJsonType>::no_limit and len > ref_stack.back()->max_size()))
{
JSON_THROW(out_of_range::create(408,
"excessive array size: " + std::to_string(len)));
}
return true; return true;
} }
...@@ -348,6 +364,7 @@ class json_sax_acceptor : public json_sax<BasicJsonType> ...@@ -348,6 +364,7 @@ class json_sax_acceptor : public json_sax<BasicJsonType>
return false; return false;
} }
}; };
}
} }
...@@ -172,7 +172,7 @@ class basic_json ...@@ -172,7 +172,7 @@ class basic_json
template<typename BasicJsonType> template<typename BasicJsonType>
friend class ::nlohmann::detail::binary_reader; friend class ::nlohmann::detail::binary_reader;
template<typename BasicJsonType> template<typename BasicJsonType>
friend class ::nlohmann::json_sax_dom_parser; friend class ::nlohmann::detail::json_sax_dom_parser;
/// workaround type for MSVC /// workaround type for MSVC
using basic_json_t = NLOHMANN_BASIC_JSON_TPL; using basic_json_t = NLOHMANN_BASIC_JSON_TPL;
......
...@@ -149,7 +149,7 @@ json parser_helper(const std::string& s) ...@@ -149,7 +149,7 @@ json parser_helper(const std::string& s)
CHECK(j_nothrow == j); CHECK(j_nothrow == j);
json j_sax; json j_sax;
nlohmann::json_sax_dom_parser<json> sdp(j_sax); nlohmann::detail::json_sax_dom_parser<json> sdp(j_sax);
json::sax_parse(s, &sdp); json::sax_parse(s, &sdp);
CHECK(j_sax == j); CHECK(j_sax == j);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment