1
0
mirror of https://github.com/nlohmann/json synced 2024-11-27 06:10:06 +00:00

Merge pull request #72 from aburgh/null-callback

Replace `default_callback` function with `nullptr` and check for null…
This commit is contained in:
Niels 2015-05-10 17:10:47 +02:00
commit 072dc25521

View File

@ -291,12 +291,6 @@ class basic_json
using parser_callback_t = std::function<bool(int depth, parse_event_t event, using parser_callback_t = std::function<bool(int depth, parse_event_t event,
const basic_json& parsed)>; const basic_json& parsed)>;
/// default parser callback returns true to keep all elements
static bool default_callback(int, parse_event_t, const basic_json&)
{
return true;
}
/*! /*!
@brief comparison operator for JSON value types @brief comparison operator for JSON value types
@ -1994,13 +1988,13 @@ class basic_json
///////////////////// /////////////////////
/// deserialize from string /// deserialize from string
static basic_json parse(const string_t& s, parser_callback_t cb = default_callback) static basic_json parse(const string_t& s, parser_callback_t cb = nullptr)
{ {
return parser(s, cb).parse(); return parser(s, cb).parse();
} }
/// deserialize from stream /// deserialize from stream
static basic_json parse(std::istream& i, parser_callback_t cb = default_callback) static basic_json parse(std::istream& i, parser_callback_t cb = nullptr)
{ {
return parser(i, cb).parse(); return parser(i, cb).parse();
} }
@ -3885,14 +3879,14 @@ class basic_json
{ {
public: public:
/// constructor for strings /// constructor for strings
inline parser(const string_t& s, parser_callback_t cb = default_callback) : callback(cb), m_lexer(s) inline parser(const string_t& s, parser_callback_t cb = nullptr) : callback(cb), m_lexer(s)
{ {
// read first token // read first token
get_token(); get_token();
} }
/// a parser reading from an input stream /// a parser reading from an input stream
inline parser(std::istream& _is, parser_callback_t cb = default_callback) : callback(cb), inline parser(std::istream& _is, parser_callback_t cb = nullptr) : callback(cb),
m_lexer(&_is) m_lexer(&_is)
{ {
// read first token // read first token
@ -3919,7 +3913,7 @@ class basic_json
{ {
case (lexer::token_type::begin_object): case (lexer::token_type::begin_object):
{ {
if (keep and (keep = callback(depth++, parse_event_t::object_start, result))) if (keep and (not callback or (keep = callback(depth++, parse_event_t::object_start, result))))
{ {
// explicitly set result to object to cope with {} // explicitly set result to object to cope with {}
result.m_type = value_t::object; result.m_type = value_t::object;
@ -3933,7 +3927,7 @@ class basic_json
if (last_token == lexer::token_type::end_object) if (last_token == lexer::token_type::end_object)
{ {
get_token(); get_token();
if (keep and not (keep = callback(--depth, parse_event_t::object_end, result))) if (keep and callback and not callback(--depth, parse_event_t::object_end, result))
{ {
result = basic_json(value_t::discarded); result = basic_json(value_t::discarded);
} }
@ -3959,7 +3953,7 @@ class basic_json
bool keep_tag = false; bool keep_tag = false;
if (keep) if (keep)
{ {
keep_tag = callback(depth, parse_event_t::key, basic_json(key)); keep_tag = callback ? callback(depth, parse_event_t::key, basic_json(key)) : true;
} }
// parse separator (:) // parse separator (:)
@ -3979,7 +3973,7 @@ class basic_json
// closing } // closing }
expect(lexer::token_type::end_object); expect(lexer::token_type::end_object);
get_token(); get_token();
if (keep and not callback(--depth, parse_event_t::object_end, result)) if (keep and callback and not callback(--depth, parse_event_t::object_end, result))
{ {
result = basic_json(value_t::discarded); result = basic_json(value_t::discarded);
} }
@ -3989,7 +3983,7 @@ class basic_json
case (lexer::token_type::begin_array): case (lexer::token_type::begin_array):
{ {
if (keep and (keep = callback(depth++, parse_event_t::array_start, result))) if (keep and (not callback or (keep = callback(depth++, parse_event_t::array_start, result))))
{ {
// explicitly set result to object to cope with [] // explicitly set result to object to cope with []
result.m_type = value_t::array; result.m_type = value_t::array;
@ -4003,7 +3997,7 @@ class basic_json
if (last_token == lexer::token_type::end_array) if (last_token == lexer::token_type::end_array)
{ {
get_token(); get_token();
if (not callback(--depth, parse_event_t::array_end, result)) if (callback and not callback(--depth, parse_event_t::array_end, result))
{ {
result = basic_json(value_t::discarded); result = basic_json(value_t::discarded);
} }
@ -4034,7 +4028,7 @@ class basic_json
// closing ] // closing ]
expect(lexer::token_type::end_array); expect(lexer::token_type::end_array);
get_token(); get_token();
if (keep and not callback(--depth, parse_event_t::array_end, result)) if (keep and callback and not callback(--depth, parse_event_t::array_end, result))
{ {
result = basic_json(value_t::discarded); result = basic_json(value_t::discarded);
} }
@ -4111,7 +4105,7 @@ class basic_json
} }
} }
if (keep and not callback(depth, parse_event_t::value, result)) if (keep and callback and not callback(depth, parse_event_t::value, result))
{ {
result = basic_json(value_t::discarded); result = basic_json(value_t::discarded);
} }