ArmNN
 20.02
JsonPrinter.cpp
Go to the documentation of this file.
1 //
2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
6 #include "JsonPrinter.hpp"
7 
8 #include <iomanip>
9 #include <iostream>
10 #include <sstream>
11 
12 namespace armnn
13 {
14 
15 void JsonPrinter::PrintJsonChildObject(const JsonChildObject& object, size_t& id)
16 {
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  }
23 
24  PrintLabel(object.m_Label, id);
25  PrintType(object.m_Type);
26 
27  if (!object.m_Measurements.empty() || !object.m_Children.empty())
28  {
30  PrintNewLine();
31  }
32 
33  if (object.GetType() == JsonObjectType::Measurement)
34  {
35  PrintMeasurementsList(object.m_Measurements);
37  PrintNewLine();
38  PrintUnit(object.m_Unit);
39  }
40  if (!object.m_Children.empty())
41  {
42  for (unsigned int childIndex = 0; childIndex < object.m_Children.size(); ++childIndex)
43  {
44  PrintJsonChildObject(object.m_Children[childIndex], id);
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  {
49  PrintNewLine();
50  }
51  }
52  }
53  PrintNewLine();
54  PrintFooter();
55 }
56 
58 {
59  m_OutputStream << "{" << std::endl;
60  IncrementNumberOfTabs();
61 }
62 
64 {
65  PrintTabs();
66  m_OutputStream << R"("ArmNN": {)" << std::endl;
67  IncrementNumberOfTabs();
68 }
69 
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)
78 {
79  PrintTabs();
80  m_OutputStream << R"(")" << MakeKey(label, id) << R"(": {)" << std::endl;
81  IncrementNumberOfTabs();
82 }
83 
85 {
86  PrintTabs();
87  m_OutputStream << R"("unit": ")";
88  m_OutputStream << armnn::Measurement::ToString(unit);
89  m_OutputStream << R"(")";
90 }
91 
93 {
94  auto ToString = [](armnn::JsonObjectType type)
95  {
96  switch (type)
97  {
99  {
100  return "Measurement";
101  }
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 
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 
154 {
155  m_OutputStream << ",";
156 }
157 
159 {
160  m_OutputStream << std::endl;
161 }
162 
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
static const char * ToString(Unit unit)
Definition: Instrument.hpp:23
Copyright (c) 2020 ARM Limited.
void PrintMeasurementsList(const std::vector< double > &measurementsVector)
void PrintType(armnn::JsonObjectType type)
Definition: JsonPrinter.cpp:92
void PrintJsonChildObject(const JsonChildObject &object, size_t &id)
Definition: JsonPrinter.cpp:15
void PrintLabel(const std::string &label, size_t id)
Definition: JsonPrinter.cpp:77
JsonObjectType
Definition: JsonPrinter.hpp:18
void PrintUnit(armnn::Measurement::Unit unit)
Definition: JsonPrinter.cpp:84