From b34363ac0fd40e2d1a2b311e566f3db5e2f30da8 Mon Sep 17 00:00:00 2001 From: Aran85 Date: Thu, 6 Aug 2015 17:45:43 +0800 Subject: [PATCH] cleanup: fix the eol dumping in JSONFormatter flush & close_section Signed-off-by: Aran85 zhangzengran@h3c.com --- src/common/Formatter.cc | 4 ++-- src/test/crush/CrushWrapper.cc | 4 +++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/common/Formatter.cc b/src/common/Formatter.cc index 7c166ef09865f..32588750997e1 100644 --- a/src/common/Formatter.cc +++ b/src/common/Formatter.cc @@ -126,8 +126,6 @@ void JSONFormatter::flush(std::ostream& os) { finish_pending_string(); os << m_ss.str(); - if (m_pretty) - os << "\n"; m_ss.clear(); m_ss.str(""); } @@ -238,6 +236,8 @@ void JSONFormatter::close_section() } m_ss << (entry.is_array ? ']' : '}'); m_stack.pop_back(); + if (m_pretty && m_stack.empty()) + m_ss << "\n"; } void JSONFormatter::finish_pending_string() diff --git a/src/test/crush/CrushWrapper.cc b/src/test/crush/CrushWrapper.cc index c690ada4f1676..bbf40ec6d639d 100644 --- a/src/test/crush/CrushWrapper.cc +++ b/src/test/crush/CrushWrapper.cc @@ -686,11 +686,13 @@ TEST(CrushWrapper, dump_rules) { // no ruleset by default { Formatter *f = Formatter::create("json-pretty"); + f->open_array_section("rules"); c->dump_rules(f); + f->close_section(); stringstream ss; f->flush(ss); delete f; - EXPECT_EQ("\n", ss.str()); + EXPECT_EQ("[]\n", ss.str()); } string name("NAME"); -- 2.39.5