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:
iwubcode
2024-03-19 22:33:48 -05:00
parent 69694494ce
commit 146504d635
2 changed files with 35 additions and 1 deletions

View File

@ -18,3 +18,23 @@ void FromJson(const picojson::object& obj, Common::Vec3& vec)
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>();
}