Debian Codegraph @

Source/Core/Common/JsonUtil.h

raw symbols SCIP
1 // Copyright 2024 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #pragma once
5
6 #include <optional>
7 #include <string>
8 #include <type_traits>
9
10 #include <picojson.h>
11
12 #include "Common/Matrix.h"
13
14 // Ideally this would use a concept like, 'template <std::ranges::range Range>' to constrain it,
15 // but unfortunately we'd need to require clang 15 for that, since the ranges library isn't
16 // fully implemented until then, but this should suffice.
17
18 template <typename Range>
19 picojson::array ToJsonArray(const Range& data)
20 {
21 using RangeUnderlyingType = typename Range::value_type;
22 picojson::array result;
23 result.reserve(std::size(data));
24
25 for (const auto& value : data)
26 {
27 if constexpr (std::is_integral_v<RangeUnderlyingType> ||
28 std::is_floating_point_v<RangeUnderlyingType>)
29 {
30 result.emplace_back(static_cast<double>(value));
31 }
32 else
33 {
34 result.emplace_back(value);
35 }
36 }
37
38 return result;
39 }
40
41 template <typename Type>
42 std::optional<Type> ReadNumericFromJson(const picojson::object& obj, const std::string& key)
43 {
44 const auto it = obj.find(key);
45 if (it == obj.end())
46 return std::nullopt;
47 if (!it->second.is<double>())
48 return std::nullopt;
49 return static_cast<Type>(it->second.get<double>());
50 }
51
52 std::optional<std::string> ReadStringFromJson(const picojson::object& obj, const std::string& key);
53
54 std::optional<bool> ReadBoolFromJson(const picojson::object& obj, const std::string& key);
55
56 picojson::object ToJsonObject(const Common::Vec3& vec);
57 void FromJson(const picojson::object& obj, Common::Vec3& vec);
58
59 bool JsonToFile(const std::string& filename, const picojson::value& root, bool prettify = false);
60 bool JsonFromFile(const std::string& filename, picojson::value* root, std::string* error);