diff --git a/include/matjson/std.hpp b/include/matjson/std.hpp index d95b16e..18662b8 100644 --- a/include/matjson/std.hpp +++ b/include/matjson/std.hpp @@ -10,6 +10,7 @@ #include #include #include +#include namespace matjson { // allow converting parsing JSON directly to STL containers for convenience @@ -226,4 +227,30 @@ namespace matjson { return Value(nullptr); } }; + + template + struct Serialize> { + static geode::Result> fromJson(Value const& value) + requires requires(Value const& value) { value.template as(); } + { + if (!value.isArray()) return geode::Err("not an array"); + if (value.size() != N) return geode::Err("array must have size " + std::to_string(N)); + std::array res{}; + for (size_t i = 0; i < N; ++i) { + GEODE_UNWRAP_INTO(res[i], value[i].template as()); + } + return geode::Ok(res); + } + + static matjson::Value toJson(std::array const& value) + requires requires(T const& value) { Value(value); } + { + std::vector res; + res.reserve(N); + std::transform(value.begin(), value.end(), std::back_inserter(res), [](T const& value) -> Value { + return Value(value); + }); + return res; + } + }; }