1
0
mirror of https://github.com/nlohmann/json.git synced 2025-07-25 13:41:56 +03:00

🔨 added a SAX-DOM-Parser

This commit is contained in:
Niels Lohmann
2018-02-26 23:39:23 +01:00
parent 21352c4d8e
commit 3ff9455332
7 changed files with 177 additions and 44 deletions

View File

@ -34,7 +34,7 @@ using nlohmann::json;
#include <valarray>
class SaxEventLogger : public nlohmann::json::json_sax
class SaxEventLogger : public nlohmann::json::json_sax_t
{
public:
bool null() override
@ -67,7 +67,7 @@ class SaxEventLogger : public nlohmann::json::json_sax
return true;
}
bool string(const std::string& val) override
bool string(std::string&& val) override
{
events.push_back("string(" + val + ")");
return true;
@ -75,7 +75,7 @@ class SaxEventLogger : public nlohmann::json::json_sax
bool start_object(std::size_t elements) override
{
if (elements == std::size_t(-1))
if (elements == no_limit)
{
events.push_back("start_object()");
}
@ -86,13 +86,13 @@ class SaxEventLogger : public nlohmann::json::json_sax
return true;
}
bool key(const std::string& val) override
bool key(std::string&& val) override
{
events.push_back("key(" + val + ")");
return true;
}
bool end_object()override
bool end_object() override
{
events.push_back("end_object()");
return true;
@ -100,7 +100,7 @@ class SaxEventLogger : public nlohmann::json::json_sax
bool start_array(std::size_t elements) override
{
if (elements == std::size_t(-1))
if (elements == no_limit)
{
events.push_back("start_array()");
}
@ -134,6 +134,129 @@ class SaxEventLogger : public nlohmann::json::json_sax
bool errored = false;
};
class SaxDomParser : public nlohmann::json::json_sax_t
{
public:
bool null() override
{
handle_value(nullptr);
return true;
}
bool boolean(bool val) override
{
handle_value(val);
return true;
}
bool number_integer(json::number_integer_t val) override
{
handle_value(val);
return true;
}
bool number_unsigned(json::number_unsigned_t val) override
{
handle_value(val);
return true;
}
bool number_float(json::number_float_t val, const std::string&) override
{
handle_value(val);
return true;
}
bool string(std::string&& val) override
{
handle_value(val);
return true;
}
bool start_object(std::size_t) override
{
ref_stack.push_back(handle_value(json::value_t::object));
return true;
}
bool key(std::string&& val) override
{
last_key = val;
return true;
}
bool end_object() override
{
ref_stack.pop_back();
return true;
}
bool start_array(std::size_t) override
{
ref_stack.push_back(handle_value(json::value_t::array));
return true;
}
bool end_array() override
{
ref_stack.pop_back();
return true;
}
bool binary(const std::vector<uint8_t>&) override
{
return true;
}
bool parse_error(std::size_t position, const std::string&) override
{
return false;
}
json& get_value()
{
return root;
}
private:
/// the parsed JSON value
json root;
/// stack to model hierarchy of values
std::vector<json*> ref_stack;
/// helper variable for object keys
std::string last_key;
/*!
@invariant If the ref stack is empty, then the passed value will be the new
root.
@invariant If the ref stack contains a value, then it is an array or an
object to which we can add elements
*/
json* handle_value(json&& j)
{
if (ref_stack.empty())
{
assert(root.is_null());
root = j;
return &root;
}
else
{
assert(ref_stack.back()->is_array() or ref_stack.back()->is_object());
if (ref_stack.back()->is_array())
{
ref_stack.back()->push_back(j);
return &(ref_stack.back()->back());
}
else
{
json& r = ref_stack.back()->operator[](last_key) = j;
return &r;
}
}
}
};
json parser_helper(const std::string& s);
bool accept_helper(const std::string& s);
@ -148,6 +271,10 @@ json parser_helper(const std::string& s)
CHECK_NOTHROW(json::parser(nlohmann::detail::input_adapter(s), nullptr, false).parse(true, j_nothrow));
CHECK(j_nothrow == j);
SaxDomParser sdp;
json::sax_parse(s, &sdp);
CHECK(sdp.get_value() == j);
return j;
}