blob: 97cd5f2b4b3aaf93542d82b93513aac38e11a5ef [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>
10
11namespace armnn
12{
13
14void JsonPrinter::PrintJsonChildObject(const JsonChildObject& object)
15{
16 PrintLabel(object.m_Label);
17 PrintMeasurementsList(object.m_Measurements);
18 PrintSeparator();
19 PrintNewLine();
20 PrintUnit(object.m_Unit);
21
22 if (!object.m_Children.empty())
23 {
24 PrintSeparator();
25 PrintNewLine();
26 for (unsigned int childIndex = 0; childIndex < object.m_Children.size(); ++childIndex)
27 {
28 PrintJsonChildObject(object.m_Children[childIndex]);
29 // Only print separator and new line if current child is not the last element.
30 if (&object.m_Children[childIndex] != &object.m_Children.back())
31 {
32 PrintSeparator();
33 PrintNewLine();
34 }
35 }
36 }
37 PrintNewLine();
38 PrintFooter();
39}
40
41void JsonPrinter::PrintHeader()
42{
43 m_OutputStream << "{" << std::endl;
44 IncrementNumberOfTabs();
45}
46
47void JsonPrinter::PrintArmNNHeader()
48{
49 PrintTabs();
50 m_OutputStream << R"("ArmNN": {)" << std::endl;
51 IncrementNumberOfTabs();
52}
53
54void JsonPrinter::PrintLabel(const std::string& label)
55{
56 PrintTabs();
57 m_OutputStream << R"(")" << label << R"(": {)" << std::endl;
58 IncrementNumberOfTabs();
59}
60
61void JsonPrinter::PrintUnit(armnn::Measurement::Unit unit)
62{
63 PrintTabs();
64 m_OutputStream << R"("unit": ")";
65 m_OutputStream << armnn::Measurement::ToString(unit);
66 m_OutputStream << R"(")";
67}
68
69void JsonPrinter::PrintMeasurementsList(const std::vector<double>& measurementsVector)
70{
71 if (measurementsVector.empty())
72 {
73 return;
74 }
75
76 PrintTabs();
77 m_OutputStream << R"("raw": [)" << std::endl;
78 IncrementNumberOfTabs();
79 PrintTabs();
80 auto iter = measurementsVector.begin();
81 m_OutputStream << *iter;
82 for (iter = std::next(iter); iter != measurementsVector.end(); ++iter)
83 {
84 m_OutputStream << "," << std::endl;
85 PrintTabs();
86 m_OutputStream << *iter;
87 }
88 m_OutputStream << std::endl;
89 DecrementNumberOfTabs();
90 PrintTabs();
91 m_OutputStream << "]";
92}
93
94void JsonPrinter::PrintTabs()
95{
96 unsigned int numTabs = m_NumTabs;
97 while (numTabs-- > 0)
98 {
99 m_OutputStream << "\t";
100 }
101}
102
103void JsonPrinter::PrintSeparator()
104{
105 m_OutputStream << ",";
106}
107
108void JsonPrinter::PrintNewLine()
109{
110 m_OutputStream << std::endl;
111}
112
113void JsonPrinter::PrintFooter()
114{
115 DecrementNumberOfTabs();
116 PrintTabs();
117 m_OutputStream << "}";
118}
119
120void JsonPrinter::DecrementNumberOfTabs()
121{
122 if (m_NumTabs == 0)
123 {
124 return;
125 }
126 --m_NumTabs;
127}
128
129void JsonPrinter::IncrementNumberOfTabs()
130{
131 ++m_NumTabs;
132}
133
134} // namespace armnn