diff --git a/src/lib_json/json_reader.cpp b/src/lib_json/json_reader.cpp index 0acbcd0..b9165a4 100644 --- a/src/lib_json/json_reader.cpp +++ b/src/lib_json/json_reader.cpp @@ -191,6 +191,17 @@ Reader::readValue() if ( collectComments_ && !commentsBefore_.empty() ) { + // Remove newline characters at the end of the comments + size_t lastNonNewline = commentsBefore_.find_last_not_of("\r\n"); + if (lastNonNewline != std::string::npos) + { + commentsBefore_.erase(lastNonNewline+1); + } + else + { + commentsBefore_.clear(); + } + currentValue().setComment( commentsBefore_, commentBefore ); commentsBefore_ = ""; } diff --git a/src/lib_json/json_writer.cpp b/src/lib_json/json_writer.cpp index dfbaa52..242bc57 100644 --- a/src/lib_json/json_writer.cpp +++ b/src/lib_json/json_writer.cpp @@ -483,7 +483,20 @@ StyledWriter::writeCommentBeforeValue( const Value &root ) { if ( !root.hasComment( commentBefore ) ) return; - document_ += normalizeEOL( root.getComment( commentBefore ) ); + + document_ += "\n"; + writeIndent(); + std::string normalizedComment = normalizeEOL( root.getComment( commentBefore ) ); + std::string::const_iterator iter = normalizedComment.begin(); + while ( iter != normalizedComment.end() ) + { + document_ += *iter; + if ( *iter == '\n' && *(iter+1) == '/' ) + writeIndent(); + ++iter; + } + + // Comments are stripped of newlines, so add one here document_ += "\n"; } diff --git a/test/data/test_comment_02.expected b/test/data/test_comment_02.expected new file mode 100644 index 0000000..09c745d --- /dev/null +++ b/test/data/test_comment_02.expected @@ -0,0 +1,7 @@ +.={} +.c-test={} +.c-test.a=1 +.c-test.b=2 +.cpp-test={} +.cpp-test.c=3 +.cpp-test.d=4 diff --git a/test/data/test_comment_02.json b/test/data/test_comment_02.json new file mode 100644 index 0000000..ccf631f --- /dev/null +++ b/test/data/test_comment_02.json @@ -0,0 +1,16 @@ +{ + /* C-style comment + + C-style-2 comment */ + "c-test" : { + "a" : 1, + /* Internal comment c-style */ + "b" : 2 + }, + // C++-style comment + "cpp-test" : { + // Internal comment cpp-style + "c" : 3, + "d" : 4 + } +}