From 20b636220b75bdc88f2e7ef86d3490bb668f71e6 Mon Sep 17 00:00:00 2001 From: Yuan-Chen Cheng Date: Fri, 25 Oct 2019 11:15:24 -0400 Subject: [PATCH] [PATCH] correct "JSON output format" without/with all class. test pass on my machine. Signed-off-by: Yuan-Chen Cheng Gbp-Pq: Name 82393aa9b59307a443677df7340a1dd38847a5ef.patch --- 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 4fe24d8..7f9e18a 100644 --- a/src/core/hw.cc +++ b/src/core/hw.cc @@ -1653,13 +1653,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())) @@ -1670,7 +1686,6 @@ string hwNode::asJSON(unsigned level) if (level == 0) { - out.seekp(-2, std::ios_base::end); out << endl << "]" << endl; }