blob: 9dc648c53814b43cc1cb564b84bf154759670c23 [file] [log] [blame]
telsoa01c577f2c2018-08-31 09:22:23 +01001//
2// Copyright © 2017 Arm Ltd. All rights reserved.
David Beckecb56cd2018-09-05 12:52:57 +01003// SPDX-License-Identifier: MIT
telsoa01c577f2c2018-08-31 09:22:23 +01004//
5
6#include "JsonPrinter.hpp"
7
8#include <iomanip>
9#include <iostream>
Derek Lamberti6b4dfc22019-08-07 17:01:57 +010010#include <sstream>
telsoa01c577f2c2018-08-31 09:22:23 +010011
12namespace armnn
13{
14
Derek Lamberti6b4dfc22019-08-07 17:01:57 +010015void JsonPrinter::PrintJsonChildObject(const JsonChildObject& object, size_t& id)
telsoa01c577f2c2018-08-31 09:22:23 +010016{
Derek Lamberti6b4dfc22019-08-07 17:01:57 +010017 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 }
telsoa01c577f2c2018-08-31 09:22:23 +010023
Derek Lamberti6b4dfc22019-08-07 17:01:57 +010024 PrintLabel(object.m_Label, id);
25 PrintType(object.m_Type);
26
27 if (!object.m_Measurements.empty() || !object.m_Children.empty())
telsoa01c577f2c2018-08-31 09:22:23 +010028 {
29 PrintSeparator();
30 PrintNewLine();
Derek Lamberti6b4dfc22019-08-07 17:01:57 +010031 }
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 {
telsoa01c577f2c2018-08-31 09:22:23 +010042 for (unsigned int childIndex = 0; childIndex < object.m_Children.size(); ++childIndex)
43 {
Derek Lamberti6b4dfc22019-08-07 17:01:57 +010044 PrintJsonChildObject(object.m_Children[childIndex], id);
telsoa01c577f2c2018-08-31 09:22:23 +010045 // 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
57void JsonPrinter::PrintHeader()
58{
59 m_OutputStream << "{" << std::endl;
60 IncrementNumberOfTabs();
61}
62
63void JsonPrinter::PrintArmNNHeader()
64{
65 PrintTabs();
66 m_OutputStream << R"("ArmNN": {)" << std::endl;
67 IncrementNumberOfTabs();
68}
69
Derek Lamberti6b4dfc22019-08-07 17:01:57 +010070std::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
77void JsonPrinter::PrintLabel(const std::string& label, size_t id)
telsoa01c577f2c2018-08-31 09:22:23 +010078{
79 PrintTabs();
Derek Lamberti6b4dfc22019-08-07 17:01:57 +010080 m_OutputStream << R"(")" << MakeKey(label, id) << R"(": {)" << std::endl;
telsoa01c577f2c2018-08-31 09:22:23 +010081 IncrementNumberOfTabs();
82}
83
84void 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 Lamberti6b4dfc22019-08-07 17:01:57 +010092void 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
telsoa01c577f2c2018-08-31 09:22:23 +0100119void 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
144void JsonPrinter::PrintTabs()
145{
146 unsigned int numTabs = m_NumTabs;
147 while (numTabs-- > 0)
148 {
149 m_OutputStream << "\t";
150 }
151}
152
153void JsonPrinter::PrintSeparator()
154{
155 m_OutputStream << ",";
156}
157
158void JsonPrinter::PrintNewLine()
159{
160 m_OutputStream << std::endl;
161}
162
163void JsonPrinter::PrintFooter()
164{
165 DecrementNumberOfTabs();
166 PrintTabs();
167 m_OutputStream << "}";
168}
169
170void JsonPrinter::DecrementNumberOfTabs()
171{
172 if (m_NumTabs == 0)
173 {
174 return;
175 }
176 --m_NumTabs;
177}
178
179void JsonPrinter::IncrementNumberOfTabs()
180{
181 ++m_NumTabs;
182}
183
184} // namespace armnn