40 const std::string &json_string,
41 bool with_whitespace =
false,
42 bool preserve_newlines =
true) {
43 enum CommentType { NO_COMMENT, SINGLE_COMMENT, MULTI_COMMENT };
44 CommentType inside_comment = CommentType::NO_COMMENT;
45 bool inside_string =
false;
48 const size_t json_string_size = json_string.size();
49 const size_t json_max_index = json_string_size - 1;
51 result.reserve(json_string_size);
53 for (
size_t i = 0; i < json_string_size; ++i) {
54 const char current_character = json_string[i];
55 const char next_character = i < json_max_index ? json_string[i + 1] : 0;
57 if (inside_comment == CommentType::NO_COMMENT && current_character ==
'"') {
58 if (!
check_escaped(json_string, i)) inside_string = !inside_string;
61 if (inside_string)
continue;
63 switch (inside_comment) {
64 case CommentType::NO_COMMENT:
65 if (current_character ==
'#' || (current_character ==
'/' && next_character ==
'/')) {
66 inside_comment = SINGLE_COMMENT;
67 result.append(json_string, offset, i - offset);
69 i += (next_character ==
'/') ? 2 : 1;
72 if (current_character ==
'/' && next_character ==
'*') {
73 inside_comment = MULTI_COMMENT;
74 result.append(json_string, offset, i - offset);
80 case CommentType::SINGLE_COMMENT:
81 if ((current_character ==
'\r' && next_character ==
'\n') || current_character ==
'\n') {
82 inside_comment = NO_COMMENT;
83 if (with_whitespace) {
84 result.append(std::string(i - offset,
' '));
85 if (current_character ==
'\r') {
93 i += (current_character ==
'\r') ? 2 : 1;
99 case CommentType::MULTI_COMMENT:
100 if (current_character ==
'*' && next_character ==
'/') {
101 inside_comment = CommentType::NO_COMMENT;
103 if (with_whitespace) {
104 if (preserve_newlines) {
105 std::string temp = json_string.substr(offset, i - offset + 1);
106 std::replace_if(temp.begin(), temp.end(), [](
char ch){
107 return ch !=
'\n' && ch !=
'\r';
111 result.append(std::string(i - offset + 1,
' '));
123 switch (inside_comment) {
124 case CommentType::NO_COMMENT:
125 result += json_string.substr(offset);
127 case CommentType::SINGLE_COMMENT:
128 if (with_whitespace) {
129 result.append(std::string(json_max_index - offset,
' '));
132 case CommentType::MULTI_COMMENT:
133 if (with_whitespace) {
134 if (preserve_newlines) {
135 std::string temp = json_string.substr(offset, json_max_index - offset);
136 std::replace_if(temp.begin(), temp.end(), [](
char ch){
137 return ch !=
'\n' && ch !=
'\r';
141 result.append(std::string(json_max_index - offset,
' '));
*The resulting JSON string may optionally retain whitespace or newlines *where the comments were removed **param json_string The JSON string to process *param with_whitespace If comments are replaced with equivalent whitespace *If comments are removed without leaving whitespace *param preserve_newlines If true and with_whitespace is newline characters *in comments are preserved all characters in the *comments are replaced with whitespace *return A JSON string with comments removed *std::string strip_json_comments(const std::string &json_string, bool with_whitespace=false, bool preserve_newlines=true)