From 82393aa9b59307a443677df7340a1dd38847a5ef Mon Sep 17 00:00:00 2001 From: Yuan-Chen Cheng Date: Fri, 25 Oct 2019 11:15:24 -0400 Subject: [PATCH] correct "JSON output format" without/with all class. test pass on my machine. Signed-off-by: Yuan-Chen Cheng --- src/core/hw.cc | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/src/core/hw.cc b/src/core/hw.cc index 4522c1a..8232337 100644 --- a/src/core/hw.cc +++ b/src/core/hw.cc @@ -1650,13 +1650,29 @@ string hwNode::asJSON(unsigned level) resources.clear(); } - for (unsigned int i = 0; i < countChildren(); i++) + if(countChildren()>0) { - out << getChild(i)->asJSON(visible(getClassName()) ? level + 2 : 1); - if (visible(getChild(i)->getClassName())) + bool childOutputed = false; + for (unsigned int i = 0; i < countChildren(); i++) { - out << "," << endl; + string childJSON = getChild(i)->asJSON(visible(getClassName()) ? level + 2 : 1); + string childJSONstrip = strip(childJSON); + if (childJSONstrip.length() > 0) + { + if (!childOutputed) + { + if(visible(getClassName())) + out << "," << endl << spaces(2*level+2) << "\"children\" : [" << endl; + } + else + out << "," << endl; + childOutputed = true; + out << childJSON; + } } + + if(visible(getClassName()) && childOutputed) + out << endl << spaces(2*level+2) << "]"; } if(visible(getClassName())) @@ -1667,7 +1683,6 @@ string hwNode::asJSON(unsigned level) if (level == 0) { - out.seekp(-2, std::ios_base::end); out << endl << "]" << endl; }