Debian Codegraph @

tests/framework/util/json_writer.cpp

raw
1 /*
2 * Copyright (c) 2023 The Khronos Group Inc.
3 * Copyright (c) 2023 Valve Corporation
4 * Copyright (c) 2023 LunarG, Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and/or associated documentation files (the "Materials"), to
8 * deal in the Materials without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Materials, and to permit persons to whom the Materials are
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice(s) and this permission notice shall be included in
14 * all copies or substantial portions of the Materials.
15 *
16 * THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19 *
20 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
21 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
22 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE MATERIALS OR THE
23 * USE OR OTHER DEALINGS IN THE MATERIALS.
24 *
25 * Author: Charles Giessen <[email protected]>
26 */
27
28 #include "json_writer.h"
29
30 #include "wide_char_handling.h"
31
32 void JsonWriter::StartObject() {
33 CommaAndNewLine();
34 Indent();
35 output += "{";
36 stack.push(false);
37 }
38 void JsonWriter::StartKeyedObject(std::string const& key) {
39 CommaAndNewLine();
40 Indent();
41 output += "\"" + key + "\": {";
42 stack.push(false);
43 }
44 void JsonWriter::EndObject() {
45 stack.pop();
46 output += "\n";
47 Indent();
48 output += "}";
49 }
50 void JsonWriter::StartKeyedArray(std::string const& key) {
51 CommaAndNewLine();
52 Indent();
53 output += "\"" + key + "\": [";
54 stack.push(false);
55 }
56 void JsonWriter::StartArray() {
57 CommaAndNewLine();
58 Indent();
59 output += "[";
60 stack.push(false);
61 }
62 void JsonWriter::EndArray() {
63 stack.pop();
64 output += "\n";
65 Indent();
66 output += "]";
67 }
68
69 void JsonWriter::AddKeyedString(std::string const& key, std::string const& value) {
70 CommaAndNewLine();
71 Indent();
72 output += "\"" + key + "\": \"" + escape(value) + "\"";
73 }
74 void JsonWriter::AddString(std::string const& value) {
75 CommaAndNewLine();
76 Indent();
77 output += "\"" + escape(value) + "\"";
78 }
79 #if defined(_WIN32)
80 void JsonWriter::AddKeyedString(std::string const& key, std::wstring const& value) {
81 CommaAndNewLine();
82 Indent();
83 output += "\"" + key + "\": \"" + escape(narrow(value)) + "\"";
84 }
85 void JsonWriter::AddString(std::wstring const& value) {
86 CommaAndNewLine();
87 Indent();
88 output += "\"" + escape(narrow(value)) + "\"";
89 }
90 #endif
91
92 void JsonWriter::AddKeyedBool(std::string const& key, bool value) {
93 CommaAndNewLine();
94 Indent();
95 output += "\"" + key + "\": " + (value ? "true" : "false");
96 }
97 void JsonWriter::AddBool(bool value) {
98 CommaAndNewLine();
99 Indent();
100 output += std::string(value ? "true" : "false");
101 }
102
103 void JsonWriter::AddKeyedNumber(std::string const& key, double number) {
104 CommaAndNewLine();
105 Indent();
106 output += "\"" + key + "\": " + std::to_string(number);
107 }
108 void JsonWriter::AddNumber(double number) {
109 CommaAndNewLine();
110 Indent();
111 output += std::to_string(number);
112 }
113
114 void JsonWriter::AddKeyedInteger(std::string const& key, int64_t number) {
115 CommaAndNewLine();
116 Indent();
117 output += "\"" + key + "\": " + std::to_string(number);
118 }
119 void JsonWriter::AddInteger(int64_t number) {
120 CommaAndNewLine();
121 Indent();
122 output += std::to_string(number);
123 }
124
125 // Json doesn't allow `\` in strings, it must be escaped. Thus we have to convert '\\' to '\\\\' in strings
126 std::string JsonWriter::escape(std::string const& in_path) {
127 std::string out;
128 for (auto& c : in_path) {
129 if (c == '\\')
130 out += "\\\\";
131 else
132 out += c;
133 }
134 return out;
135 }
136 std::string JsonWriter::escape(std::filesystem::path const& in_path) { return escape(narrow(in_path.native())); }
137
138 void JsonWriter::CommaAndNewLine() {
139 if (stack.size() > 0) {
140 if (stack.top() == false) {
141 stack.top() = true;
142 } else {
143 output += ",";
144 }
145 output += "\n";
146 }
147 }
148 void JsonWriter::Indent() {
149 for (uint32_t i = 0; i < stack.size(); i++) {
150 output += '\t';
151 }
152 }