Skip to content

Commit

Permalink
🚧 extend API
Browse files Browse the repository at this point in the history
  • Loading branch information
nlohmann committed Jun 17, 2020
1 parent e9bfcf7 commit 74520d8
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 66 deletions.
8 changes: 8 additions & 0 deletions include/nlohmann/detail/input/lexer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -837,6 +837,7 @@ class lexer : public lexer_base<BasicJsonType>
{
switch (get())
{
// single-line comments skip input until a newline or EOF is read
case '/':
{
while (true)
Expand All @@ -845,6 +846,8 @@ class lexer : public lexer_base<BasicJsonType>
{
case '\n':
case '\r':
case std::char_traits<char_type>::eof():
case '\0':
return true;

default:
Expand All @@ -853,6 +856,7 @@ class lexer : public lexer_base<BasicJsonType>
}
}

// multi-line comments skip input until */ is read
case '*':
{
while (true)
Expand All @@ -877,10 +881,14 @@ class lexer : public lexer_base<BasicJsonType>
}
}
}

default:
break;
}
}
}

// unexpected character after reading '/'
default:
return false;
}
Expand Down
7 changes: 5 additions & 2 deletions include/nlohmann/detail/input/parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,11 @@ class parser
/// a parser reading from an input adapter
explicit parser(InputAdapterType&& adapter,
const parser_callback_t<BasicJsonType> cb = nullptr,
const bool allow_exceptions_ = true)
: callback(cb), m_lexer(std::move(adapter)), allow_exceptions(allow_exceptions_)
const bool allow_exceptions_ = true,
const bool skip_comments = false)
: callback(cb)
, m_lexer(std::move(adapter), skip_comments)
, allow_exceptions(allow_exceptions_)
{
// read first token
get_token();
Expand Down
63 changes: 42 additions & 21 deletions include/nlohmann/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -196,10 +196,12 @@ class basic_json
static ::nlohmann::detail::parser<basic_json, InputAdapterType> parser(
InputAdapterType adapter,
detail::parser_callback_t<basic_json>cb = nullptr,
bool allow_exceptions = true
const bool allow_exceptions = true,
const bool ignore_comments = false
)
{
return ::nlohmann::detail::parser<basic_json, InputAdapterType>(std::move(adapter), std::move(cb), allow_exceptions);
return ::nlohmann::detail::parser<basic_json, InputAdapterType>(std::move(adapter),
std::move(cb), allow_exceptions, ignore_comments);
}

using primitive_iterator_t = ::nlohmann::detail::primitive_iterator_t;
Expand Down Expand Up @@ -6563,6 +6565,8 @@ class basic_json
(optional)
@param[in] allow_exceptions whether to throw exceptions in case of a
parse error (optional, true by default)
@param[in] ignore_comments whether comments should be ignored (true) or
yield a parse error (true); (optional, false by default)
@return deserialized JSON value; in case of a parse error and
@a allow_exceptions set to `false`, the return value will be
Expand Down Expand Up @@ -6591,16 +6595,18 @@ class basic_json
@liveexample{The example below demonstrates the `parse()` function reading
from a contiguous container.,parse__contiguouscontainer__parser_callback_t}
@since version 2.0.3 (contiguous containers)
@since version 2.0.3 (contiguous containers); version 3.9.0 allowed to
ignore comments.
*/
template<typename InputType>
JSON_HEDLEY_WARN_UNUSED_RESULT
static basic_json parse(InputType&& i,
const parser_callback_t cb = nullptr,
const bool allow_exceptions = true)
const bool allow_exceptions = true,
const bool ignore_comments = false)
{
basic_json result;
parser(detail::input_adapter(std::forward<InputType>(i)), cb, allow_exceptions).parse(true, result);
parser(detail::input_adapter(std::forward<InputType>(i)), cb, allow_exceptions, ignore_comments).parse(true, result);
return result;
}

Expand All @@ -6617,6 +6623,8 @@ class basic_json
(optional)
@param[in] allow_exceptions whether to throw exceptions in case of a
parse error (optional, true by default)
@param[in] ignore_comments whether comments should be ignored (true) or
yield a parse error (true); (optional, false by default)
@return deserialized JSON value; in case of a parse error and
@a allow_exceptions set to `false`, the return value will be
Expand All @@ -6632,21 +6640,23 @@ class basic_json
static basic_json parse(IteratorType first,
IteratorType last,
const parser_callback_t cb = nullptr,
const bool allow_exceptions = true)
const bool allow_exceptions = true,
const bool ignore_comments = false)
{
basic_json result;
parser(detail::input_adapter(std::move(first), std::move(last)), cb, allow_exceptions).parse(true, result);
parser(detail::input_adapter(std::move(first), std::move(last)), cb, allow_exceptions, ignore_comments).parse(true, result);
return result;
}

JSON_HEDLEY_WARN_UNUSED_RESULT
JSON_HEDLEY_DEPRECATED_FOR(3.8.0, parse(ptr, ptr + len))
static basic_json parse(detail::span_input_adapter&& i,
const parser_callback_t cb = nullptr,
const bool allow_exceptions = true)
const bool allow_exceptions = true,
const bool ignore_comments = false)
{
basic_json result;
parser(i.get(), cb, allow_exceptions).parse(true, result);
parser(i.get(), cb, allow_exceptions, ignore_comments).parse(true, result);
return result;
}

Expand All @@ -6666,6 +6676,8 @@ class basic_json
iterators.
@param[in] i input to read from
@param[in] ignore_comments whether comments should be ignored (true) or
yield a parse error (true); (optional, false by default)
@return Whether the input read from @a i is valid JSON.
Expand All @@ -6678,22 +6690,25 @@ class basic_json
from a string.,accept__string}
*/
template<typename InputType>
static bool accept(InputType&& i)
static bool accept(InputType&& i,
const bool ignore_comments = false)
{
return parser(detail::input_adapter(std::forward<InputType>(i))).accept(true);
return parser(detail::input_adapter(std::forward<InputType>(i)), nullptr, false, ignore_comments).accept(true);
}

template<typename IteratorType>
static bool accept(IteratorType first, IteratorType last)
static bool accept(IteratorType first, IteratorType last,
const bool ignore_comments = false)
{
return parser(detail::input_adapter(std::move(first), std::move(last))).accept(true);
return parser(detail::input_adapter(std::move(first), std::move(last)), nullptr, false, ignore_comments).accept(true);
}

JSON_HEDLEY_WARN_UNUSED_RESULT
JSON_HEDLEY_DEPRECATED_FOR(3.8.0, accept(ptr, ptr + len))
static bool accept(detail::span_input_adapter&& i)
static bool accept(detail::span_input_adapter&& i,
const bool ignore_comments = false)
{
return parser(i.get()).accept(true);
return parser(i.get(), nullptr, false, ignore_comments).accept(true);
}

/*!
Expand All @@ -6713,6 +6728,9 @@ class basic_json
@param[in,out] sax SAX event listener
@param[in] format the format to parse (JSON, CBOR, MessagePack, or UBJSON)
@param[in] strict whether the input has to be consumed completely
@param[in] ignore_comments whether comments should be ignored (true) or
yield a parse error (true); (optional, false by default); only applieds to
the JSON file format.
@return return value of the last processed SAX event
Expand All @@ -6737,23 +6755,25 @@ class basic_json
JSON_HEDLEY_NON_NULL(2)
static bool sax_parse(InputType&& i, SAX* sax,
input_format_t format = input_format_t::json,
const bool strict = true)
const bool strict = true,
const bool ignore_comments = false)
{
auto ia = detail::input_adapter(std::forward<InputType>(i));
return format == input_format_t::json
? parser(std::move(ia)).sax_parse(sax, strict)
? parser(std::move(ia), nullptr, true, ignore_comments).sax_parse(sax, strict)
: detail::binary_reader<basic_json, decltype(ia), SAX>(std::move(ia)).sax_parse(format, sax, strict);
}

template<class IteratorType, class SAX>
JSON_HEDLEY_NON_NULL(3)
static bool sax_parse(IteratorType first, IteratorType last, SAX* sax,
input_format_t format = input_format_t::json,
const bool strict = true)
const bool strict = true,
const bool ignore_comments = false)
{
auto ia = detail::input_adapter(std::move(first), std::move(last));
return format == input_format_t::json
? parser(std::move(ia)).sax_parse(sax, strict)
? parser(std::move(ia), nullptr, true, ignore_comments).sax_parse(sax, strict)
: detail::binary_reader<basic_json, decltype(ia), SAX>(std::move(ia)).sax_parse(format, sax, strict);
}

Expand All @@ -6762,11 +6782,12 @@ class basic_json
JSON_HEDLEY_NON_NULL(2)
static bool sax_parse(detail::span_input_adapter&& i, SAX* sax,
input_format_t format = input_format_t::json,
const bool strict = true)
const bool strict = true,
const bool ignore_comments = false)
{
auto ia = i.get();
return format == input_format_t::json
? parser(std::move(ia)).sax_parse(sax, strict)
? parser(std::move(ia), nullptr, true, ignore_comments).sax_parse(sax, strict)
: detail::binary_reader<basic_json, decltype(ia), SAX>(std::move(ia)).sax_parse(format, sax, strict);
}

Expand Down
Loading

0 comments on commit 74520d8

Please sign in to comment.