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

Feature request: Comments #597

Closed
hunyadix opened this issue May 30, 2017 · 11 comments · Fixed by #2212
Closed

Feature request: Comments #597

hunyadix opened this issue May 30, 2017 · 11 comments · Fixed by #2212
Labels
solution: duplicate the issue is a duplicate; refer to the linked issue instead solution: proposed fix a fix for the issue has been proposed and waits for confirmation
Milestone

Comments

@hunyadix
Copy link

Many JSON parsers support the option to comment out fields. Is it possible to request such a feature in the nlohmann parser too?

@nlohmann
Copy link
Owner

For this library, we do not plan to support any input which is not compliant to RFC 7159.

See #376, #363, #311, #294, #192.

@nlohmann nlohmann added the solution: duplicate the issue is a duplicate; refer to the linked issue instead label May 30, 2017
@kinchungwong
Copy link

kinchungwong commented Jan 4, 2018

@nlohmann

  1. Would it make sense to just document the fact that comments are not supported on the project front page, or on an FAQ page?
  2. Would it make sense to mention that users who need to process json inputs containing comments should use a separate comment stripper (that does not come with this library), and some recommendations?

Thanks.

@nlohmann
Copy link
Owner

nlohmann commented Jan 4, 2018

@kinchungwong

  1. As comments are not part of JSON, I do not think that an explicit FAQ entry that comments are not supported by this library would be helpful.
  2. Can you propose such a comment stripper?

@fawdlstty
Copy link

It's been a long time, and I think we can consider replanning the introduction of json comments

@fawdlstty
Copy link

Not support for JSON comments is a very sad thing, as is the ability to debug data, indicate what values are available, and so on.If you need to enforce the JSON standard, consider adding a separate interface for parsing JSON strings with JSON comments

@fawdlstty
Copy link

Such as:

string s = "{/**///\n}";
s = nlohmann::json::remove_comments (s);
auto o = nlohmann::json::parse (s);

@nlohmann
Copy link
Owner

nlohmann commented Jun 3, 2020

But removing comments is not possible by just a regular expression. It would mean integration into the lexer.

@fawdlstty
Copy link

So can reopen the question?

@nlohmann
Copy link
Owner

nlohmann commented Jun 4, 2020

No, because this library will not support comments. If anyone wants to provide a pull request with a compact comment stripper, I will have a look.

@fawdlstty
Copy link

I think can add this code to the project:

#ifndef __JSON_PREPROCESS_HPP__
#define __JSON_PREPROCESS_HPP__

#include <string>

class JsonPreprocess {
	enum class CurrentState {
		Normal, Quote, QuoteIgnore, BeginComment, CommentLine, CommentBlock, CommentBlockEnd,
	};
public:
	static std::string remove_comments (std::string _data) {
		std::string _ret = "";
		auto _state = CurrentState::Normal;
		for (char ch : _data) {
			if (_state == CurrentState::Normal) {
				if (ch == '"') {
					_ret += ch;
					_state = CurrentState::Quote;
				} else if (ch == '/') {
					_state = CurrentState::BeginComment;
				} else {
					_ret += ch;
				}
			} else if (_state == CurrentState::Quote) {
				if (ch == '"') {
					_ret += ch;
					_state = CurrentState::Normal;
				} else if (ch == '\\') {
					_ret += ch;
					_state = CurrentState::QuoteIgnore;
				} else {
					_ret += ch;
				}
			} else if (_state == CurrentState::QuoteIgnore) {
				_ret += ch;
				_state = CurrentState::Quote;
			} else if (_state == CurrentState::BeginComment) {
				_state = (ch == '/' ? CurrentState::CommentLine : CurrentState::CommentBlock);
			} else if (_state == CurrentState::CommentLine) {
				if (ch == '\r' || ch == '\n') {
					_ret += ch;
					_state = CurrentState::Normal;
				} else {
					_state = CurrentState::CommentLine;
				}
			} else if (_state == CurrentState::CommentBlock) {
				_state = (ch == '*' ? CurrentState::CommentBlockEnd : CurrentState::CommentBlock);
			} else if (_state == CurrentState::CommentBlockEnd) {
				_state = (ch == '*' ? CurrentState::CommentBlockEnd : (ch == '/' ? CurrentState::Normal : CurrentState::CommentBlock));
			}
		}
		return _ret;
	}
};

#endif //__JSON_PREPROCESS_HPP__

@nlohmann
Copy link
Owner

I'm currently working on extending the parser and will open a PR later this week.

@nlohmann nlohmann added the solution: proposed fix a fix for the issue has been proposed and waits for confirmation label Jun 23, 2020
@nlohmann nlohmann reopened this Jun 23, 2020
@nlohmann nlohmann added this to the Release 3.8.1 milestone Jun 27, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
solution: duplicate the issue is a duplicate; refer to the linked issue instead solution: proposed fix a fix for the issue has been proposed and waits for confirmation
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants