telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2017 Arm Ltd. All rights reserved. |
David Beck | ecb56cd | 2018-09-05 12:52:57 +0100 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 4 | // |
| 5 | |
| 6 | #include "JsonPrinter.hpp" |
| 7 | |
| 8 | #include <iomanip> |
| 9 | #include <iostream> |
Derek Lamberti | 6b4dfc2 | 2019-08-07 17:01:57 +0100 | [diff] [blame] | 10 | #include <sstream> |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 11 | |
| 12 | namespace armnn |
| 13 | { |
| 14 | |
Derek Lamberti | 6b4dfc2 | 2019-08-07 17:01:57 +0100 | [diff] [blame] | 15 | void JsonPrinter::PrintJsonChildObject(const JsonChildObject& object, size_t& id) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 16 | { |
Derek Lamberti | 6b4dfc2 | 2019-08-07 17:01:57 +0100 | [diff] [blame] | 17 | if (object.GetType() == JsonObjectType::Event) |
| 18 | { |
| 19 | // Increase the Id for new events. This ensures a new event has a unique ID and any measurements belonging |
| 20 | // to the event have the same id. This id is appended to the name during the call to PrintLabel() below. |
| 21 | id++; |
| 22 | } |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 23 | |
Derek Lamberti | 6b4dfc2 | 2019-08-07 17:01:57 +0100 | [diff] [blame] | 24 | PrintLabel(object.m_Label, id); |
| 25 | PrintType(object.m_Type); |
| 26 | |
| 27 | if (!object.m_Measurements.empty() || !object.m_Children.empty()) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 28 | { |
| 29 | PrintSeparator(); |
| 30 | PrintNewLine(); |
Derek Lamberti | 6b4dfc2 | 2019-08-07 17:01:57 +0100 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | if (object.GetType() == JsonObjectType::Measurement) |
| 34 | { |
| 35 | PrintMeasurementsList(object.m_Measurements); |
| 36 | PrintSeparator(); |
| 37 | PrintNewLine(); |
| 38 | PrintUnit(object.m_Unit); |
| 39 | } |
| 40 | if (!object.m_Children.empty()) |
| 41 | { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 42 | for (unsigned int childIndex = 0; childIndex < object.m_Children.size(); ++childIndex) |
| 43 | { |
Derek Lamberti | 6b4dfc2 | 2019-08-07 17:01:57 +0100 | [diff] [blame] | 44 | PrintJsonChildObject(object.m_Children[childIndex], id); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 45 | // Only print separator and new line if current child is not the last element. |
| 46 | if (&object.m_Children[childIndex] != &object.m_Children.back()) |
| 47 | { |
| 48 | PrintSeparator(); |
| 49 | PrintNewLine(); |
| 50 | } |
| 51 | } |
| 52 | } |
| 53 | PrintNewLine(); |
| 54 | PrintFooter(); |
| 55 | } |
| 56 | |
| 57 | void JsonPrinter::PrintHeader() |
| 58 | { |
| 59 | m_OutputStream << "{" << std::endl; |
| 60 | IncrementNumberOfTabs(); |
| 61 | } |
| 62 | |
| 63 | void JsonPrinter::PrintArmNNHeader() |
| 64 | { |
| 65 | PrintTabs(); |
| 66 | m_OutputStream << R"("ArmNN": {)" << std::endl; |
| 67 | IncrementNumberOfTabs(); |
| 68 | } |
| 69 | |
Derek Lamberti | 6b4dfc2 | 2019-08-07 17:01:57 +0100 | [diff] [blame] | 70 | std::string JsonPrinter::MakeKey(const std::string& label, size_t id) |
| 71 | { |
| 72 | std::stringstream ss; |
| 73 | ss << label << std::string("_#") << id; |
| 74 | return ss.str(); |
| 75 | } |
| 76 | |
| 77 | void JsonPrinter::PrintLabel(const std::string& label, size_t id) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 78 | { |
| 79 | PrintTabs(); |
Derek Lamberti | 6b4dfc2 | 2019-08-07 17:01:57 +0100 | [diff] [blame] | 80 | m_OutputStream << R"(")" << MakeKey(label, id) << R"(": {)" << std::endl; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 81 | IncrementNumberOfTabs(); |
| 82 | } |
| 83 | |
| 84 | void JsonPrinter::PrintUnit(armnn::Measurement::Unit unit) |
| 85 | { |
| 86 | PrintTabs(); |
| 87 | m_OutputStream << R"("unit": ")"; |
| 88 | m_OutputStream << armnn::Measurement::ToString(unit); |
| 89 | m_OutputStream << R"(")"; |
| 90 | } |
| 91 | |
Derek Lamberti | 6b4dfc2 | 2019-08-07 17:01:57 +0100 | [diff] [blame] | 92 | void JsonPrinter::PrintType(armnn::JsonObjectType type) |
| 93 | { |
| 94 | auto ToString = [](armnn::JsonObjectType type) |
| 95 | { |
| 96 | switch (type) |
| 97 | { |
| 98 | case JsonObjectType::Measurement: |
| 99 | { |
| 100 | return "Measurement"; |
| 101 | } |
| 102 | case JsonObjectType::Event: |
| 103 | { |
| 104 | return "Event"; |
| 105 | } |
| 106 | default: |
| 107 | { |
| 108 | return "Unknown"; |
| 109 | } |
| 110 | } |
| 111 | }; |
| 112 | PrintTabs(); |
| 113 | m_OutputStream << R"("type": ")"; |
| 114 | m_OutputStream << ToString(type); |
| 115 | m_OutputStream << R"(")"; |
| 116 | } |
| 117 | |
| 118 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 119 | void JsonPrinter::PrintMeasurementsList(const std::vector<double>& measurementsVector) |
| 120 | { |
| 121 | if (measurementsVector.empty()) |
| 122 | { |
| 123 | return; |
| 124 | } |
| 125 | |
| 126 | PrintTabs(); |
| 127 | m_OutputStream << R"("raw": [)" << std::endl; |
| 128 | IncrementNumberOfTabs(); |
| 129 | PrintTabs(); |
| 130 | auto iter = measurementsVector.begin(); |
| 131 | m_OutputStream << *iter; |
| 132 | for (iter = std::next(iter); iter != measurementsVector.end(); ++iter) |
| 133 | { |
| 134 | m_OutputStream << "," << std::endl; |
| 135 | PrintTabs(); |
| 136 | m_OutputStream << *iter; |
| 137 | } |
| 138 | m_OutputStream << std::endl; |
| 139 | DecrementNumberOfTabs(); |
| 140 | PrintTabs(); |
| 141 | m_OutputStream << "]"; |
| 142 | } |
| 143 | |
| 144 | void JsonPrinter::PrintTabs() |
| 145 | { |
| 146 | unsigned int numTabs = m_NumTabs; |
| 147 | while (numTabs-- > 0) |
| 148 | { |
| 149 | m_OutputStream << "\t"; |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | void JsonPrinter::PrintSeparator() |
| 154 | { |
| 155 | m_OutputStream << ","; |
| 156 | } |
| 157 | |
| 158 | void JsonPrinter::PrintNewLine() |
| 159 | { |
| 160 | m_OutputStream << std::endl; |
| 161 | } |
| 162 | |
| 163 | void JsonPrinter::PrintFooter() |
| 164 | { |
| 165 | DecrementNumberOfTabs(); |
| 166 | PrintTabs(); |
| 167 | m_OutputStream << "}"; |
| 168 | } |
| 169 | |
| 170 | void JsonPrinter::DecrementNumberOfTabs() |
| 171 | { |
| 172 | if (m_NumTabs == 0) |
| 173 | { |
| 174 | return; |
| 175 | } |
| 176 | --m_NumTabs; |
| 177 | } |
| 178 | |
| 179 | void JsonPrinter::IncrementNumberOfTabs() |
| 180 | { |
| 181 | ++m_NumTabs; |
| 182 | } |
| 183 | |
| 184 | } // namespace armnn |