mirror of
https://github.com/nlohmann/json.git
synced 2025-07-25 13:41:56 +03:00
use templates in the sax interface instead of virtuals
This commit is contained in:
@ -35,48 +35,48 @@ using nlohmann::json;
|
||||
|
||||
#include <valarray>
|
||||
|
||||
class SaxEventLogger : public nlohmann::json::json_sax_t
|
||||
class SaxEventLogger
|
||||
{
|
||||
public:
|
||||
bool null() override
|
||||
bool null()
|
||||
{
|
||||
events.push_back("null()");
|
||||
return true;
|
||||
}
|
||||
|
||||
bool boolean(bool val) override
|
||||
bool boolean(bool val)
|
||||
{
|
||||
events.push_back(val ? "boolean(true)" : "boolean(false)");
|
||||
return true;
|
||||
}
|
||||
|
||||
bool number_integer(json::number_integer_t val) override
|
||||
bool number_integer(json::number_integer_t val)
|
||||
{
|
||||
events.push_back("number_integer(" + std::to_string(val) + ")");
|
||||
return true;
|
||||
}
|
||||
|
||||
bool number_unsigned(json::number_unsigned_t val) override
|
||||
bool number_unsigned(json::number_unsigned_t val)
|
||||
{
|
||||
events.push_back("number_unsigned(" + std::to_string(val) + ")");
|
||||
return true;
|
||||
}
|
||||
|
||||
bool number_float(json::number_float_t, const std::string& s) override
|
||||
bool number_float(json::number_float_t, const std::string& s)
|
||||
{
|
||||
events.push_back("number_float(" + s + ")");
|
||||
return true;
|
||||
}
|
||||
|
||||
bool string(std::string& val) override
|
||||
bool string(std::string& val)
|
||||
{
|
||||
events.push_back("string(" + val + ")");
|
||||
return true;
|
||||
}
|
||||
|
||||
bool start_object(std::size_t elements) override
|
||||
bool start_object(std::size_t elements = -1)
|
||||
{
|
||||
if (elements == no_limit)
|
||||
if (elements == std::size_t(-1))
|
||||
{
|
||||
events.push_back("start_object()");
|
||||
}
|
||||
@ -87,21 +87,21 @@ class SaxEventLogger : public nlohmann::json::json_sax_t
|
||||
return true;
|
||||
}
|
||||
|
||||
bool key(std::string& val) override
|
||||
bool key(std::string& val)
|
||||
{
|
||||
events.push_back("key(" + val + ")");
|
||||
return true;
|
||||
}
|
||||
|
||||
bool end_object() override
|
||||
bool end_object()
|
||||
{
|
||||
events.push_back("end_object()");
|
||||
return true;
|
||||
}
|
||||
|
||||
bool start_array(std::size_t elements) override
|
||||
bool start_array(std::size_t elements = -1)
|
||||
{
|
||||
if (elements == no_limit)
|
||||
if (elements == std::size_t(-1))
|
||||
{
|
||||
events.push_back("start_array()");
|
||||
}
|
||||
@ -112,13 +112,13 @@ class SaxEventLogger : public nlohmann::json::json_sax_t
|
||||
return true;
|
||||
}
|
||||
|
||||
bool end_array() override
|
||||
bool end_array()
|
||||
{
|
||||
events.push_back("end_array()");
|
||||
return true;
|
||||
}
|
||||
|
||||
bool parse_error(std::size_t position, const std::string&, const json::exception&) override
|
||||
bool parse_error(std::size_t position, const std::string&, const json::exception&)
|
||||
{
|
||||
errored = true;
|
||||
events.push_back("parse_error(" + std::to_string(position) + ")");
|
||||
@ -129,68 +129,68 @@ class SaxEventLogger : public nlohmann::json::json_sax_t
|
||||
bool errored = false;
|
||||
};
|
||||
|
||||
class SaxCountdown : public nlohmann::json::json_sax_t
|
||||
class SaxCountdown
|
||||
{
|
||||
public:
|
||||
explicit SaxCountdown(const int count) : events_left(count)
|
||||
{}
|
||||
|
||||
bool null() override
|
||||
bool null()
|
||||
{
|
||||
return events_left-- > 0;
|
||||
}
|
||||
|
||||
bool boolean(bool) override
|
||||
bool boolean(bool)
|
||||
{
|
||||
return events_left-- > 0;
|
||||
}
|
||||
|
||||
bool number_integer(json::number_integer_t) override
|
||||
bool number_integer(json::number_integer_t)
|
||||
{
|
||||
return events_left-- > 0;
|
||||
}
|
||||
|
||||
bool number_unsigned(json::number_unsigned_t) override
|
||||
bool number_unsigned(json::number_unsigned_t)
|
||||
{
|
||||
return events_left-- > 0;
|
||||
}
|
||||
|
||||
bool number_float(json::number_float_t, const std::string&) override
|
||||
bool number_float(json::number_float_t, const std::string&)
|
||||
{
|
||||
return events_left-- > 0;
|
||||
}
|
||||
|
||||
bool string(std::string&) override
|
||||
bool string(std::string&)
|
||||
{
|
||||
return events_left-- > 0;
|
||||
}
|
||||
|
||||
bool start_object(std::size_t) override
|
||||
bool start_object(std::size_t = -1)
|
||||
{
|
||||
return events_left-- > 0;
|
||||
}
|
||||
|
||||
bool key(std::string&) override
|
||||
bool key(std::string&)
|
||||
{
|
||||
return events_left-- > 0;
|
||||
}
|
||||
|
||||
bool end_object() override
|
||||
bool end_object()
|
||||
{
|
||||
return events_left-- > 0;
|
||||
}
|
||||
|
||||
bool start_array(std::size_t) override
|
||||
bool start_array(std::size_t = -1)
|
||||
{
|
||||
return events_left-- > 0;
|
||||
}
|
||||
|
||||
bool end_array() override
|
||||
bool end_array()
|
||||
{
|
||||
return events_left-- > 0;
|
||||
}
|
||||
|
||||
bool parse_error(std::size_t, const std::string&, const json::exception&) override
|
||||
bool parse_error(std::size_t, const std::string&, const json::exception&)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
Reference in New Issue
Block a user