Debian Codegraph @

tests/framework/util/json_writer.h

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 #pragma once
29
30 #include <cstdint>
31 #include <stack>
32 #include <string>
33 #include <filesystem>
34
35 #include "wide_char_handling.h"
36
37 // Utility class to simplify the writing of JSON manifest files
38
39 struct JsonWriter {
40 std::string output;
41
42 // the bool represents whether an object has been written & a comma is needed
43 std::stack<bool> stack;
44
45 void StartObject();
46 void StartKeyedObject(std::string const& key);
47 void EndObject();
48 void StartKeyedArray(std::string const& key);
49 void StartArray();
50 void EndArray();
51
52 void AddKeyedString(std::string const& key, std::string const& value);
53 void AddString(std::string const& value);
54 #if defined(_WIN32)
55 void AddKeyedString(std::string const& key, std::wstring const& value);
56 void AddString(std::wstring const& value);
57 #endif
58
59 void AddKeyedBool(std::string const& key, bool value);
60 void AddBool(bool value);
61
62 void AddKeyedNumber(std::string const& key, double number);
63 void AddNumber(double number);
64
65 void AddKeyedInteger(std::string const& key, int64_t number);
66 void AddInteger(int64_t number);
67
68 // Json doesn't allow `\` in strings, it must be escaped. Thus we have to convert '\\' to '\\\\' in strings
69 static std::string escape(std::string const& in_path);
70 static std::string escape(std::filesystem::path const& in_path);
71
72 private:
73 void CommaAndNewLine();
74 void Indent();
75 };