mirror of
https://github.com/dolphin-emu/dolphin.git
synced 2025-03-12 14:46:49 +01:00
Common: add json utility functions 'ReadStringFromJson' and 'ReadBoolFromJson' to match 'ReadNumericFromJson' functionality for other data types, similarly support other data types for 'ToJsonArray'
This commit is contained in:
parent
69694494ce
commit
146504d635
@ -18,3 +18,23 @@ void FromJson(const picojson::object& obj, Common::Vec3& vec)
|
|||||||
vec.y = ReadNumericFromJson<float>(obj, "y").value_or(0.0f);
|
vec.y = ReadNumericFromJson<float>(obj, "y").value_or(0.0f);
|
||||||
vec.z = ReadNumericFromJson<float>(obj, "z").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>();
|
||||||
|
}
|
||||||
|
@ -5,6 +5,7 @@
|
|||||||
|
|
||||||
#include <optional>
|
#include <optional>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <type_traits>
|
||||||
|
|
||||||
#include <picojson.h>
|
#include <picojson.h>
|
||||||
|
|
||||||
@ -18,12 +19,21 @@
|
|||||||
template <typename Range>
|
template <typename Range>
|
||||||
picojson::array ToJsonArray(const Range& data)
|
picojson::array ToJsonArray(const Range& data)
|
||||||
{
|
{
|
||||||
|
using RangeUnderlyingType = typename Range::value_type;
|
||||||
picojson::array result;
|
picojson::array result;
|
||||||
result.reserve(std::size(data));
|
result.reserve(std::size(data));
|
||||||
|
|
||||||
for (const auto& value : 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;
|
return result;
|
||||||
@ -40,5 +50,9 @@ std::optional<Type> ReadNumericFromJson(const picojson::object& obj, const std::
|
|||||||
return MathUtil::SaturatingCast<Type>(it->second.get<double>());
|
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);
|
picojson::object ToJsonObject(const Common::Vec3& vec);
|
||||||
void FromJson(const picojson::object& obj, Common::Vec3& vec);
|
void FromJson(const picojson::object& obj, Common::Vec3& vec);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user