This commit is contained in:
Nayla Hanegan 2024-05-12 02:17:59 -04:00
commit 98c174edc4
520 changed files with 74815 additions and 58942 deletions

View file

@ -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);