Add option to ignore comments in parse/accept functions
This PR adds an option `ignore_comments` to the `parse` and `accept` family that allows to ignore `//` and `/* */` style comments.

PS: I still think there is a good reason JSON has no comment support. But it was by far the most popular request, and I understand that the lack of comments was a problem for a lot of folks that use JSON as configuration format. However, comments are still treated as parse error by default.

Closes #2061
Closes #1513
Closes #597
Closes #376
Closes #363
Closes #294

Related to #2090 #348 (comments only, no full JSON5 support).
Related to #311 (comments only, no JSON Schema support)
{"label": "nlohmann:develop", "ref": "develop", "sha": "8575fdf9ad41844692b18fb9db4d28fea4e9282a"}
While comments are not part of JSON, some people (particularly those writting JSON files by hand sometimes add comments to them). That makes those files not JSON anymore. The typical solution when one wants to process one of those files as JSON is to "minify" them first.

Is it possible to strip comments / minify json files before parsing them as json?

Strip comments / Minify
diff --git a/README.md b/README.md
index 42d057a35b..0699bdc307 100644
--- a/README.md
+++ b/README.md
@@ -1508,7 +1508,7 @@ The library supports **Unicode input** as follows:
 
 ### Comments in JSON
 
-This library does not support comments. It does so for three reasons:
+This library does not support comments by default. It does so for three reasons:
 
 1. Comments are not part of the [JSON specification](https://tools.ietf.org/html/rfc8259). You may argue that `//` or `/* */` are allowed in JavaScript, but JSON is not JavaScript.
 2. This was not an oversight: Douglas Crockford [wrote on this](https://plus.google.com/118095276221607585885/posts/RK8qyGVaGSr) in May 2012:
@@ -1519,11 +1519,7 @@ This library does not support comments. It does so for three reasons:
 
 3. It is dangerous for interoperability if some libraries would add comment support while others don't. Please check [The Harmful Consequences of the Robustness Principle](https://tools.ietf.org/html/draft-iab-protocol-maintenance-01) on this.
 
-This library will not support comments in the future. If you wish to use comments, I see three options:
-
-1. Strip comments before using this library.
-2. Use a different JSON library with comment support.
-3. Use a format that natively supports comments (e.g., YAML or JSON5).
+However, you can pass set parameter `ignore_comments` to true in the `parse` function to ignore `//` or `/* */` comments. Comments will then be treated as whitespace.
 
 ### Order of object keys
 
diff --git a/include/nlohmann/detail/input/lexer.hpp b/include/nlohmann/detail/input/lexer.hpp
index 0ff0c7362d..8042f3c4ef 100644
--- a/include/nlohmann/detail/input/lexer.hpp
+++ b/include/nlohmann/detail/input/lexer.hpp
@@ -112,8 +112,11 @@ class lexer : public lexer_base<BasicJsonType>
   public:
     using token_type = typename lexer_base<BasicJsonType>::token_type;
 
-    explicit lexer(InputAdapterType&& adapter)
-        : ia(std::move(adapter)), decimal_point_char(static_cast<char_int_type>(get_decimal_point())) {}
+    explicit lexer(InputAdapterType&& adapter, bool ignore_comments_ = false)
+        : ia(std::move(adapter))
+        , ignore_comments(ignore_comments_)
+        , decimal_point_char(static_cast<char_int_type>(get_decimal_point()))
+    {}
 
     // delete because of pointer members
     lexer(const lexer&) = delete;
@@ -131,7 +134,7 @@ class lexer : public lexer_base<BasicJsonType>
     JSON_HEDLEY_PURE
     static char get_decimal_point() noexcept
     {
-        const auto loc = localeconv();
+        const auto* loc = localeconv();
         assert(loc != nullptr);
         return (loc->decimal_point == nullptr) ? '.' : *(loc->decimal_point);
     }
@@ -826,6 +829,77 @@ class lexer : public lexer_base<BasicJsonType>
         }
     }
 
+    /*!
+     * @brief scan a comment
+     * @return whether comment could be scanned successfully
+     */
+    bool scan_comment()
+    {
+        switch (get())
+        {
+            // single-line comments skip input until a newline or EOF is read
+            case '/':
+            {
+                while (true)
+                {
+                    switch (get())
+                    {
+                        case '\n':
+                        case '\r':
+                        case std::char_traits<char_type>::eof():
+                        case '\0':
+                            return true;
+
+                        default:
+                            break;
+                    }
+                }
+            }
+
+            // multi-line comments skip input until */ is read
+            case '*':
+            {
+                while (true)
+                {
+                    switch (get())
+                    {
+                        case std::char_traits<char_type>::eof():
+                        case '\0':
+                        {
+                            error_message = "invalid comment; missing closing '*/'";
+                            return false;
+                        }
+
+                        case '*':
+                        {
+                            switch (get())
+                            {
+                                case '/':
+                                    return true;
+
+                                default:
+                                {
+                                    unget();
+                                    break;
+                                }
+                            }
+                        }
+
+                        default:
+                            break;
+                    }
+                }
+            }
+
+            // unexpected character after reading '/'
+            default:
+            {
+                error_message = "invalid comment; expecting '/' or '*' after '/'";
+                return false;
+            }
+        }
+    }
+
     JSON_HEDLEY_NON_NULL(2)
     static void strtof(float& f, const char* str, char** endptr) noexcept
     {
@@ -1415,6 +1489,15 @@ class lexer : public lexer_base<BasicJsonType>
         return true;
     }
 
+    void skip_whitespace()
+    {
+        do
+        {
+            get();
+        }
+        while (current == ' ' or current == '\t' or current == '\n' or current == '\r');
+    }
+
     token_type scan()
     {
         // initially, skip the BOM
@@ -1425,11 +1508,19 @@ class lexer : public lexer_base<BasicJsonType>
         }
 
         // read next character and ignore whitespace
-        do
+        skip_whitespace();
+
+        // ignore comments
+        if (ignore_comments and current == '/')
         {
-            get();
+            if (not scan_comment())
+            {
+                return token_type::parse_error;
+            }
+
+            // skip following whitespace
+            skip_whitespace();
         }
-        while (current == ' ' or current == '\t' or current == '\n' or current == '\r');
 
         switch (current)
         {
@@ -1499,6 +1590,9 @@ class lexer : public lexer_base<BasicJsonType>
     /// input adapter
     InputAdapterType ia;
 
+    /// whether comments should be ignored (true) or signaled as errors (false)
+    const bool ignore_comments = false;
+
     /// the current character
     char_int_type current = std::char_traits<char_type>::eof();
 
diff --git a/include/nlohmann/detail/input/parser.hpp b/include/nlohmann/detail/input/parser.hpp
index 0546b88cb8..c79b492aab 100644
--- a/include/nlohmann/detail/input/parser.hpp
+++ b/include/nlohmann/detail/input/parser.hpp
@@ -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();
diff --git a/include/nlohmann/json.hpp b/include/nlohmann/json.hpp
index 790ecd4b25..cc7e3fbc2a 100644
--- a/include/nlohmann/json.hpp
+++ b/include/nlohmann/json.hpp
@@ -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;
@@ -6563,6 +6565,9 @@ 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 and treated
+    like whitespace (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
@@ -6591,16 +6596,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;
     }
 
@@ -6617,6 +6624,9 @@ 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 and treated
+    like whitespace (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
@@ -6632,10 +6642,11 @@ 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;
     }
 
@@ -6643,10 +6654,11 @@ class basic_json
     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;
     }
 
@@ -6666,6 +6678,9 @@ class basic_json
       iterators.
 
     @param[in] i input to read from
+    @param[in] ignore_comments  whether comments should be ignored and treated
+    like whitespace (true) or yield a parse error (true); (optional, false by
+    default)
 
     @return Whether the input read from @a i is valid JSON.
 
@@ -6678,22 +6693,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);
     }
 
     /*!
@@ -6713,6 +6731,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 and treated
+    like whitespace (true) or yield a parse error (true); (optional, false by
+    default); only applies to the JSON file format.
 
     @return return value of the last processed SAX event
 
@@ -6737,11 +6758,12 @@ 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);
     }
 
@@ -6749,11 +6771,12 @@ class basic_json
     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);
     }
 
@@ -6762,11 +6785,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);
     }
 
diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp
index 33d0c35776..683e2d5608 100644
--- a/single_include/nlohmann/json.hpp
+++ b/single_include/nlohmann/json.hpp
@@ -8179,8 +8179,11 @@ class lexer : public lexer_base<BasicJsonType>
   public:
     using token_type = typename lexer_base<BasicJsonType>::token_type;
 
-    explicit lexer(InputAdapterType&& adapter)
-        : ia(std::move(adapter)), decimal_point_char(static_cast<char_int_type>(get_decimal_point())) {}
+    explicit lexer(InputAdapterType&& adapter, bool ignore_comments_ = false)
+        : ia(std::move(adapter))
+        , ignore_comments(ignore_comments_)
+        , decimal_point_char(static_cast<char_int_type>(get_decimal_point()))
+    {}
 
     // delete because of pointer members
     lexer(const lexer&) = delete;
@@ -8198,7 +8201,7 @@ class lexer : public lexer_base<BasicJsonType>
     JSON_HEDLEY_PURE
     static char get_decimal_point() noexcept
     {
-        const auto loc = localeconv();
+        const auto* loc = localeconv();
         assert(loc != nullptr);
         return (loc->decimal_point == nullptr) ? '.' : *(loc->decimal_point);
     }
@@ -8893,6 +8896,77 @@ class lexer : public lexer_base<BasicJsonType>
         }
     }
 
+    /*!
+     * @brief scan a comment
+     * @return whether comment could be scanned successfully
+     */
+    bool scan_comment()
+    {
+        switch (get())
+        {
+            // single-line comments skip input until a newline or EOF is read
+            case '/':
+            {
+                while (true)
+                {
+                    switch (get())
+                    {
+                        case '\n':
+                        case '\r':
+                        case std::char_traits<char_type>::eof():
+                        case '\0':
+                            return true;
+
+                        default:
+                            break;
+                    }
+                }
+            }
+
+            // multi-line comments skip input until */ is read
+            case '*':
+            {
+                while (true)
+                {
+                    switch (get())
+                    {
+                        case std::char_traits<char_type>::eof():
+                        case '\0':
+                        {
+                            error_message = "invalid comment; missing closing '*/'";
+                            return false;
+                        }
+
+                        case '*':
+                        {
+                            switch (get())
+                            {
+                                case '/':
+                                    return true;
+
+                                default:
+                                {
+                                    unget();
+                                    break;
+                                }
+                            }
+                        }
+
+                        default:
+                            break;
+                    }
+                }
+            }
+
+            // unexpected character after reading '/'
+            default:
+            {
+                error_message = "invalid comment; expecting '/' or '*' after '/'";
+                return false;
+            }
+        }
+    }
+
     JSON_HEDLEY_NON_NULL(2)
     static void strtof(float& f, const char* str, char** endptr) noexcept
     {
@@ -9482,6 +9556,15 @@ class lexer : public lexer_base<BasicJsonType>
         return true;
     }
 
+    void skip_whitespace()
+    {
+        do
+        {
+            get();
+        }
+        while (current == ' ' or current == '\t' or current == '\n' or current == '\r');
+    }
+
     token_type scan()
     {
         // initially, skip the BOM
@@ -9492,11 +9575,19 @@ class lexer : public lexer_base<BasicJsonType>
         }
 
         // read next character and ignore whitespace
-        do
+        skip_whitespace();
+
+        // ignore comments
+        if (ignore_comments and current == '/')
         {
-            get();
+            if (not scan_comment())
+            {
+                return token_type::parse_error;
+            }
+
+            // skip following whitespace
+            skip_whitespace();
         }
-        while (current == ' ' or current == '\t' or current == '\n' or current == '\r');
 
         switch (current)
         {
@@ -9566,6 +9657,9 @@ class lexer : public lexer_base<BasicJsonType>
     /// input adapter
     InputAdapterType ia;
 
+    /// whether comments should be ignored (true) or signaled as errors (false)
+    const bool ignore_comments = false;
+
     /// the current character
     char_int_type current = std::char_traits<char_type>::eof();
 
@@ -9668,8 +9762,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();
@@ -15986,10 +16083,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;
@@ -22353,6 +22452,9 @@ 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 and treated
+    like whitespace (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
@@ -22381,16 +22483,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;
     }
 
@@ -22407,6 +22511,9 @@ 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 and treated
+    like whitespace (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
@@ -22422,10 +22529,11 @@ 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;
     }
 
@@ -22433,10 +22541,11 @@ class basic_json
     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;
     }
 
@@ -22456,6 +22565,9 @@ class basic_json
       iterators.
 
     @param[in] i input to read from
+    @param[in] ignore_comments  whether comments should be ignored and treated
+    like whitespace (true) or yield a parse error (true); (optional, false by
+    default)
 
     @return Whether the input read from @a i is valid JSON.
 
@@ -22468,22 +22580,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);
     }
 
     /*!
@@ -22503,6 +22618,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 and treated
+    like whitespace (true) or yield a parse error (true); (optional, false by
+    default); only applies to the JSON file format.
 
     @return return value of the last processed SAX event
 
@@ -22527,11 +22645,12 @@ 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);
     }
 
@@ -22539,11 +22658,12 @@ class basic_json
     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);
     }
 
@@ -22552,11 +22672,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);
     }
 

diff --git a/test/src/unit-class_lexer.cpp b/test/src/unit-class_lexer.cpp
index 0e1b53785a..d8304ccf17 100644
--- a/test/src/unit-class_lexer.cpp
+++ b/test/src/unit-class_lexer.cpp
@@ -37,14 +37,23 @@ using nlohmann::json;
 namespace
 {
 // shortcut to scan a string literal
-json::lexer::token_type scan_string(const char* s);
-json::lexer::token_type scan_string(const char* s)
+json::lexer::token_type scan_string(const char* s, const bool ignore_comments = false);
+json::lexer::token_type scan_string(const char* s, const bool ignore_comments)
 {
     auto ia = nlohmann::detail::input_adapter(s);
-    return nlohmann::detail::lexer<json, decltype(ia)>(std::move(ia)).scan();
+    return nlohmann::detail::lexer<json, decltype(ia)>(std::move(ia), ignore_comments).scan();
 }
 }
 
+std::string get_error_message(const char* s, const bool ignore_comments = false);
+std::string get_error_message(const char* s, const bool ignore_comments)
+{
+    auto ia = nlohmann::detail::input_adapter(s);
+    auto lexer = nlohmann::detail::lexer<json, decltype(ia)>(std::move(ia), ignore_comments);
+    lexer.scan();
+    return lexer.get_error_message();
+}
+
 TEST_CASE("lexer class")
 {
     SECTION("scan")
@@ -127,6 +136,8 @@ TEST_CASE("lexer class")
             // store scan() result
             const auto res = scan_string(s.c_str());
 
+            CAPTURE(s);
+
             switch (c)
             {
                 // single characters that are valid tokens
@@ -179,4 +190,56 @@ TEST_CASE("lexer class")
         s += "\"";
         CHECK((scan_string(s.c_str()) == json::lexer::token_type::value_string));
     }
+
+    SECTION("fail on comments")
+    {
+        CHECK((scan_string("/", false) == json::lexer::token_type::parse_error));
+        CHECK(get_error_message("/", false) == "invalid literal");
+
+        CHECK((scan_string("/!", false) == json::lexer::token_type::parse_error));
+        CHECK(get_error_message("/!", false) == "invalid literal");
+        CHECK((scan_string("/*", false) == json::lexer::token_type::parse_error));
+        CHECK(get_error_message("/*", false) == "invalid literal");
+        CHECK((scan_string("/**", false) == json::lexer::token_type::parse_error));
+        CHECK(get_error_message("/**", false) == "invalid literal");
+
+        CHECK((scan_string("//", false) == json::lexer::token_type::parse_error));
+        CHECK(get_error_message("//", false) == "invalid literal");
+        CHECK((scan_string("/**/", false) == json::lexer::token_type::parse_error));
+        CHECK(get_error_message("/**/", false) == "invalid literal");
+        CHECK((scan_string("/** /", false) == json::lexer::token_type::parse_error));
+        CHECK(get_error_message("/** /", false) == "invalid literal");
+
+        CHECK((scan_string("/***/", false) == json::lexer::token_type::parse_error));
+        CHECK(get_error_message("/***/", false) == "invalid literal");
+        CHECK((scan_string("/* true */", false) == json::lexer::token_type::parse_error));
+        CHECK(get_error_message("/* true */", false) == "invalid literal");
+        CHECK((scan_string("/*/**/", false) == json::lexer::token_type::parse_error));
+        CHECK(get_error_message("/*/**/", false) == "invalid literal");
+        CHECK((scan_string("/*/* */", false) == json::lexer::token_type::parse_error));
+        CHECK(get_error_message("/*/* */", false) == "invalid literal");
+    }
+
+    SECTION("ignore comments")
+    {
+        CHECK((scan_string("/", true) == json::lexer::token_type::parse_error));
+        CHECK(get_error_message("/", true) == "invalid comment; expecting '/' or '*' after '/'");
+
+        CHECK((scan_string("/!", true) == json::lexer::token_type::parse_error));
+        CHECK(get_error_message("/!", true) == "invalid comment; expecting '/' or '*' after '/'");
+        CHECK((scan_string("/*", true) == json::lexer::token_type::parse_error));
+        CHECK(get_error_message("/*", true) == "invalid comment; missing closing '*/'");
+        CHECK((scan_string("/**", true) == json::lexer::token_type::parse_error));
+        CHECK(get_error_message("/**", true) == "invalid comment; missing closing '*/'");
+
+        CHECK((scan_string("//", true) == json::lexer::token_type::end_of_input));
+        CHECK((scan_string("/**/", true) == json::lexer::token_type::end_of_input));
+        CHECK((scan_string("/** /", true) == json::lexer::token_type::parse_error));
+        CHECK(get_error_message("/** /", true) == "invalid comment; missing closing '*/'");
+
+        CHECK((scan_string("/***/", true) == json::lexer::token_type::end_of_input));
+        CHECK((scan_string("/* true */", true) == json::lexer::token_type::end_of_input));
+        CHECK((scan_string("/*/**/", true) == json::lexer::token_type::end_of_input));
+        CHECK((scan_string("/*/* */", true) == json::lexer::token_type::end_of_input));
+    }
 }
diff --git a/test/src/unit-class_parser.cpp b/test/src/unit-class_parser.cpp
index 1912094aad..da16ffcab7 100644
--- a/test/src/unit-class_parser.cpp
+++ b/test/src/unit-class_parser.cpp
@@ -224,6 +224,7 @@ class SaxCountdown : public nlohmann::json::json_sax_t
 
 json parser_helper(const std::string& s);
 bool accept_helper(const std::string& s);
+void comments_helper(const std::string& s);
 
 json parser_helper(const std::string& s)
 {
@@ -241,6 +242,8 @@ json parser_helper(const std::string& s)
     json::sax_parse(s, &sdp);
     CHECK(j_sax == j);
 
+    comments_helper(s);
+
     return j;
 }
 
@@ -275,11 +278,51 @@ bool accept_helper(const std::string& s)
     // 6. check if this approach came to the same result
     CHECK(ok_noexcept == ok_noexcept_cb);
 
-    // 7. return result
+    // 7. check if comments are properly ignored
+    if (ok_accept)
+    {
+        comments_helper(s);
+    }
+
+    // 8. return result
     return ok_accept;
 }
+
+void comments_helper(const std::string& s)
+{
+    json _;
+
+    // parse/accept with default parser
+    CHECK_NOTHROW(_ = json::parse(s));
+    CHECK(json::accept(s));
+
+    // parse/accept while skipping comments
+    CHECK_NOTHROW(_ = json::parse(s, nullptr, false, true));
+    CHECK(json::accept(s, true));
+
+    std::vector<std::string> json_with_comments;
+
+    // start with a comment
+    json_with_comments.push_back(std::string("// this is a comment\n") + s);
+    json_with_comments.push_back(std::string("/* this is a comment */") + s);
+    // end with a comment
+    json_with_comments.push_back(s + "// this is a comment");
+    json_with_comments.push_back(s + "/* this is a comment */");
+
+    // check all strings
+    for (const auto& json_with_comment : json_with_comments)
+    {
+        CAPTURE(json_with_comment)
+        CHECK_THROWS_AS(_ = json::parse(json_with_comment), json::parse_error);
+        CHECK(not json::accept(json_with_comment));
+
+        CHECK_NOTHROW(_ = json::parse(json_with_comment, nullptr, true, true));
+        CHECK(json::accept(json_with_comment, true));
+    }
 }
 
+} // namespace
+
 TEST_CASE("parser class")
 {
     SECTION("parse")
@@ -1834,4 +1877,10 @@ TEST_CASE("parser class")
             }
         }
     }
+
+    SECTION("error messages for comments")
+    {
+        CHECK_THROWS_WITH_AS(json::parse("/a", nullptr, true, true), "[json.exception.parse_error.101] parse error at line 1, column 2: syntax error while parsing value - invalid comment; expecting '/' or '*' after '/'; last read: '/a'", json::parse_error);
+        CHECK_THROWS_WITH_AS(json::parse("/*", nullptr, true, true), "[json.exception.parse_error.101] parse error at line 1, column 3: syntax error while parsing value - invalid comment; missing closing '*/'; last read: '/*<U+0000>'", json::parse_error);
+    }
 }

{"test-noexcept": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "test-large_json": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "test-testsuites": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "test-convenience": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "cmake_fetch_content_build": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "test-reference_access": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "test-wstring": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "cmake_import_build": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "test-to_chars": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "download_test_data": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "test-ubjson": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "test-bson": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "test-allocator": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "test-element_access2": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "cmake_import_minver_build": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "test-cbor": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "test-regression": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "cmake_import_minver_configure": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "test-user_defined_input": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "test-capacity": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "test-class_lexer": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "test-udt": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "test-concepts": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "test-comparison": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "test-modifiers": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "cmake_add_subdirectory_configure": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "test-unicode": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "test-class_iterator": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "test-constructor2": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "test-readme": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "test-iterators1": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "test-conversions": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "test-msgpack": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "test-iterators2": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "test-inspection": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "test-meta": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "test-items": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "test-constructor1": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "test-json_patch": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "cmake_fetch_content_configure": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "test-element_access1": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "test-merge_patch": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "test-serialization": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "test-json_pointer": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "test-deserialization": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "test-pointer_access": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "test-alt-string": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "cmake_import_configure": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "test-algorithms": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "test-class_parser": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "test-class_const_iterator": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "cmake_add_subdirectory_build": {"run": "PASS", "test": "NONE", "fix": "PASS"}}
{"test-noexcept": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "test-large_json": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "test-testsuites": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "test-convenience": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "cmake_fetch_content_build": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "test-reference_access": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "test-wstring": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "cmake_import_build": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "test-to_chars": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "download_test_data": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "test-ubjson": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "test-bson": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "test-allocator": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "test-element_access2": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "cmake_import_minver_build": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "test-cbor": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "test-regression": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "cmake_import_minver_configure": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "test-user_defined_input": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "test-capacity": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "test-class_lexer": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "test-udt": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "test-concepts": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "test-comparison": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "test-modifiers": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "cmake_add_subdirectory_configure": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "test-unicode": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "test-class_iterator": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "test-constructor2": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "test-readme": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "test-iterators1": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "test-conversions": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "test-msgpack": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "test-iterators2": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "test-inspection": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "test-meta": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "test-items": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "test-constructor1": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "test-json_patch": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "cmake_fetch_content_configure": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "test-element_access1": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "test-merge_patch": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "test-serialization": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "test-json_pointer": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "test-deserialization": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "test-pointer_access": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "test-alt-string": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "cmake_import_configure": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "test-algorithms": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "test-class_parser": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "test-class_const_iterator": {"run": "PASS", "test": "NONE", "fix": "PASS"}, "cmake_add_subdirectory_build": {"run": "PASS", "test": "NONE", "fix": "PASS"}}
{"passed_count": 52, "failed_count": 0, "skipped_count": 0, "passed_tests": ["test-noexcept", "test-large_json", "test-testsuites", "test-convenience", "cmake_fetch_content_build", "test-reference_access", "test-wstring", "cmake_import_build", "test-to_chars", "download_test_data", "test-ubjson", "test-bson", "test-allocator", "test-element_access2", "cmake_import_minver_build", "test-cbor", "test-regression", "cmake_import_minver_configure", "test-user_defined_input", "test-capacity", "test-class_lexer", "test-udt", "test-concepts", "test-comparison", "test-class_const_iterator", "test-modifiers", "cmake_add_subdirectory_configure", "test-unicode", "test-class_iterator", "test-constructor2", "test-readme", "test-iterators1", "test-conversions", "test-msgpack", "test-iterators2", "test-inspection", "test-meta", "test-items", "test-constructor1", "test-json_patch", "cmake_fetch_content_configure", "test-element_access1", "test-merge_patch", "test-serialization", "test-json_pointer", "test-pointer_access", "test-alt-string", "cmake_import_configure", "test-algorithms", "test-class_parser", "test-deserialization", "cmake_add_subdirectory_build"], "failed_tests": [], "skipped_tests": []}
{"passed_count": 0, "failed_count": 0, "skipped_count": 0, "passed_tests": [], "failed_tests": [], "skipped_tests": []}
{"passed_count": 52, "failed_count": 0, "skipped_count": 0, "passed_tests": ["test-noexcept", "test-large_json", "test-testsuites", "test-convenience", "cmake_fetch_content_build", "test-reference_access", "test-wstring", "cmake_import_build", "test-to_chars", "download_test_data", "test-ubjson", "test-bson", "test-allocator", "test-element_access2", "cmake_import_minver_build", "test-cbor", "test-regression", "cmake_import_minver_configure", "test-user_defined_input", "test-capacity", "test-class_lexer", "test-udt", "test-concepts", "test-comparison", "test-class_const_iterator", "test-modifiers", "cmake_add_subdirectory_configure", "test-unicode", "test-class_iterator", "test-constructor2", "test-readme", "test-iterators1", "test-conversions", "test-msgpack", "test-iterators2", "test-inspection", "test-meta", "test-items", "test-constructor1", "test-json_patch", "cmake_fetch_content_configure", "test-element_access1", "test-merge_patch", "test-serialization", "test-json_pointer", "test-pointer_access", "test-alt-string", "cmake_import_configure", "test-algorithms", "test-class_parser", "test-deserialization", "cmake_add_subdirectory_build"], "failed_tests": [], "skipped_tests": []}