mirror of
https://github.com/dolphin-emu/dolphin.git
synced 2025-02-15 16:59:18 +01:00
Common: update 'ReadNumericOrDefault' to 'ReadNumericFromJson' and have it return an optional, this provides the caller with more flexibility
This commit is contained in:
parent
85dee300b5
commit
69694494ce
@ -14,7 +14,7 @@ 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)
|
||||||
{
|
{
|
||||||
vec.x = ReadNumericOrDefault<float>(obj, "x");
|
vec.x = ReadNumericFromJson<float>(obj, "x").value_or(0.0f);
|
||||||
vec.y = ReadNumericOrDefault<float>(obj, "y");
|
vec.y = ReadNumericFromJson<float>(obj, "y").value_or(0.0f);
|
||||||
vec.z = ReadNumericOrDefault<float>(obj, "z");
|
vec.z = ReadNumericFromJson<float>(obj, "z").value_or(0.0f);
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <optional>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
#include <picojson.h>
|
#include <picojson.h>
|
||||||
@ -29,14 +30,13 @@ picojson::array ToJsonArray(const Range& data)
|
|||||||
}
|
}
|
||||||
|
|
||||||
template <typename Type>
|
template <typename Type>
|
||||||
Type ReadNumericOrDefault(const picojson::object& obj, const std::string& key,
|
std::optional<Type> ReadNumericFromJson(const picojson::object& obj, const std::string& key)
|
||||||
Type default_value = Type{})
|
|
||||||
{
|
{
|
||||||
const auto it = obj.find(key);
|
const auto it = obj.find(key);
|
||||||
if (it == obj.end())
|
if (it == obj.end())
|
||||||
return default_value;
|
return std::nullopt;
|
||||||
if (!it->second.is<double>())
|
if (!it->second.is<double>())
|
||||||
return default_value;
|
return std::nullopt;
|
||||||
return MathUtil::SaturatingCast<Type>(it->second.get<double>());
|
return MathUtil::SaturatingCast<Type>(it->second.get<double>());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user