1
0
mirror of https://github.com/nlohmann/json.git synced 2025-08-09 05:22:48 +03:00

Add ignore_trailing_commas option (#4609)

Added examples and modified the corresponding documents and unit tests.

Signed-off-by: chirsz-ever <chirsz-ever@outlook.com>
Co-authored-by: Niels Lohmann <niels.lohmann@gmail.com>
This commit is contained in:
chirsz
2025-05-22 14:01:46 +08:00
committed by GitHub
parent 2d9a251266
commit 4b17f90f65
15 changed files with 386 additions and 148 deletions

View File

@@ -71,10 +71,12 @@ class parser
explicit parser(InputAdapterType&& adapter,
parser_callback_t<BasicJsonType> cb = nullptr,
const bool allow_exceptions_ = true,
const bool skip_comments = false)
const bool ignore_comments = false,
const bool ignore_trailing_commas_ = false)
: callback(std::move(cb))
, m_lexer(std::move(adapter), skip_comments)
, m_lexer(std::move(adapter), ignore_comments)
, allow_exceptions(allow_exceptions_)
, ignore_trailing_commas(ignore_trailing_commas_)
{
// read first token
get_token();
@@ -384,11 +386,17 @@ class parser
if (states.back()) // array
{
// comma -> next value
// or end of array (ignore_trailing_commas = true)
if (get_token() == token_type::value_separator)
{
// parse a new value
get_token();
continue;
// if ignore_trailing_commas and last_token is ], we can continue to "closing ]"
if (!(ignore_trailing_commas && last_token == token_type::end_array))
{
continue;
}
}
// closing ]
@@ -417,32 +425,39 @@ class parser
// states.back() is false -> object
// comma -> next value
// or end of object (ignore_trailing_commas = true)
if (get_token() == token_type::value_separator)
{
// parse key
if (JSON_HEDLEY_UNLIKELY(get_token() != token_type::value_string))
{
return sax->parse_error(m_lexer.get_position(),
m_lexer.get_token_string(),
parse_error::create(101, m_lexer.get_position(), exception_message(token_type::value_string, "object key"), nullptr));
}
if (JSON_HEDLEY_UNLIKELY(!sax->key(m_lexer.get_string())))
{
return false;
}
// parse separator (:)
if (JSON_HEDLEY_UNLIKELY(get_token() != token_type::name_separator))
{
return sax->parse_error(m_lexer.get_position(),
m_lexer.get_token_string(),
parse_error::create(101, m_lexer.get_position(), exception_message(token_type::name_separator, "object separator"), nullptr));
}
// parse values
get_token();
continue;
// if ignore_trailing_commas and last_token is }, we can continue to "closing }"
if (!(ignore_trailing_commas && last_token == token_type::end_object))
{
// parse key
if (JSON_HEDLEY_UNLIKELY(last_token != token_type::value_string))
{
return sax->parse_error(m_lexer.get_position(),
m_lexer.get_token_string(),
parse_error::create(101, m_lexer.get_position(), exception_message(token_type::value_string, "object key"), nullptr));
}
if (JSON_HEDLEY_UNLIKELY(!sax->key(m_lexer.get_string())))
{
return false;
}
// parse separator (:)
if (JSON_HEDLEY_UNLIKELY(get_token() != token_type::name_separator))
{
return sax->parse_error(m_lexer.get_position(),
m_lexer.get_token_string(),
parse_error::create(101, m_lexer.get_position(), exception_message(token_type::name_separator, "object separator"), nullptr));
}
// parse values
get_token();
continue;
}
}
// closing }
@@ -513,6 +528,8 @@ class parser
lexer_t m_lexer;
/// whether to throw exceptions in case of errors
const bool allow_exceptions = true;
/// whether trailing commas in objects and arrays should be ignored (true) or signaled as errors (false)
const bool ignore_trailing_commas = false;
};
} // namespace detail