Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace default_callback function with nullptr and check for null… #72

Merged
merged 1 commit into from
May 10, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 12 additions & 18 deletions src/json.hpp.re2c
Original file line number Diff line number Diff line change
Expand Up @@ -291,12 +291,6 @@ class basic_json
using parser_callback_t = std::function<bool(int depth, parse_event_t event,
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

Expand Down Expand Up @@ -2002,13 +1996,13 @@ class basic_json
/////////////////////

/// 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();
}

/// 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();
}
Expand Down Expand Up @@ -3893,14 +3887,14 @@ class basic_json
{
public:
/// 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
get_token();
}

/// 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)
{
// read first token
Expand All @@ -3927,7 +3921,7 @@ class basic_json
{
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 {}
result.m_type = value_t::object;
Expand All @@ -3941,7 +3935,7 @@ class basic_json
if (last_token == lexer::token_type::end_object)
{
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);
}
Expand All @@ -3967,7 +3961,7 @@ class basic_json
bool keep_tag = false;
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 (:)
Expand All @@ -3987,7 +3981,7 @@ class basic_json
// closing }
expect(lexer::token_type::end_object);
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);
}
Expand All @@ -3997,7 +3991,7 @@ class basic_json

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 []
result.m_type = value_t::array;
Expand All @@ -4011,7 +4005,7 @@ class basic_json
if (last_token == lexer::token_type::end_array)
{
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);
}
Expand Down Expand Up @@ -4042,7 +4036,7 @@ class basic_json
// closing ]
expect(lexer::token_type::end_array);
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);
}
Expand Down Expand Up @@ -4119,7 +4113,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);
}
Expand Down