summaryrefslogtreecommitdiff
path: root/Source/Core
diff options
context:
space:
mode:
authorAdmiral H. Curtiss <pikachu025@gmail.com>2024-03-22 02:45:58 +0100
committerGitHub <noreply@github.com>2024-03-22 02:45:58 +0100
commitf535b61d5d4e14d3930eafba2ae94f488619090d (patch)
treed47486ba58ddf6d0656eb24823b2507d02c7c1d8 /Source/Core
parentb69122f3cfa932a04137a206e33537924bb78f4d (diff)
parent146504d6357513fded0eaf4edc77e0c2a1ee1640 (diff)
Merge pull request #12652 from iwubcode/json_util_update
Common: add even more json utility functions
Diffstat (limited to 'Source/Core')
-rw-r--r--Source/Core/Common/JsonUtil.cpp26
-rw-r--r--Source/Core/Common/JsonUtil.h24
2 files changed, 42 insertions, 8 deletions
diff --git a/Source/Core/Common/JsonUtil.cpp b/Source/Core/Common/JsonUtil.cpp
index d16f37f4f3..3e842c5c18 100644
--- a/Source/Core/Common/JsonUtil.cpp
+++ b/Source/Core/Common/JsonUtil.cpp
@@ -14,7 +14,27 @@ picojson::object ToJsonObject(const Common::Vec3& vec)
void FromJson(const picojson::object& obj, Common::Vec3& vec)
{
- vec.x = ReadNumericOrDefault<float>(obj, "x");
- vec.y = ReadNumericOrDefault<float>(obj, "y");
- vec.z = ReadNumericOrDefault<float>(obj, "z");
+ vec.x = ReadNumericFromJson<float>(obj, "x").value_or(0.0f);
+ vec.y = ReadNumericFromJson<float>(obj, "y").value_or(0.0f);
+ vec.z = ReadNumericFromJson<float>(obj, "z").value_or(0.0f);
+}
+
+std::optional<std::string> ReadStringFromJson(const picojson::object& obj, const std::string& key)
+{
+ const auto it = obj.find(key);
+ if (it == obj.end())
+ return std::nullopt;
+ if (!it->second.is<std::string>())
+ return std::nullopt;
+ return it->second.to_str();
+}
+
+std::optional<bool> ReadBoolFromJson(const picojson::object& obj, const std::string& key)
+{
+ const auto it = obj.find(key);
+ if (it == obj.end())
+ return std::nullopt;
+ if (!it->second.is<bool>())
+ return std::nullopt;
+ return it->second.get<bool>();
}
diff --git a/Source/Core/Common/JsonUtil.h b/Source/Core/Common/JsonUtil.h
index cee52432ff..eec836d507 100644
--- a/Source/Core/Common/JsonUtil.h
+++ b/Source/Core/Common/JsonUtil.h
@@ -3,7 +3,9 @@
#pragma once
+#include <optional>
#include <string>
+#include <type_traits>
#include <picojson.h>
@@ -17,28 +19,40 @@
template <typename Range>
picojson::array ToJsonArray(const Range& data)
{
+ using RangeUnderlyingType = typename Range::value_type;
picojson::array result;
result.reserve(std::size(data));
for (const auto& value : data)
{
- result.emplace_back(static_cast<double>(value));
+ if constexpr (std::is_integral_v<RangeUnderlyingType> ||
+ std::is_floating_point_v<RangeUnderlyingType>)
+ {
+ result.emplace_back(static_cast<double>(value));
+ }
+ else
+ {
+ result.emplace_back(value);
+ }
}
return result;
}
template <typename Type>
-Type ReadNumericOrDefault(const picojson::object& obj, const std::string& key,
- Type default_value = Type{})
+std::optional<Type> ReadNumericFromJson(const picojson::object& obj, const std::string& key)
{
const auto it = obj.find(key);
if (it == obj.end())
- return default_value;
+ return std::nullopt;
if (!it->second.is<double>())
- return default_value;
+ return std::nullopt;
return MathUtil::SaturatingCast<Type>(it->second.get<double>());
}
+std::optional<std::string> ReadStringFromJson(const picojson::object& obj, const std::string& key);
+
+std::optional<bool> ReadBoolFromJson(const picojson::object& obj, const std::string& key);
+
picojson::object ToJsonObject(const Common::Vec3& vec);
void FromJson(const picojson::object& obj, Common::Vec3& vec);