Merge pull request #12585 from iwubcode/json_util_vec3
Common: add json utility functions for Vec3 serialization
This commit is contained in:
commit
0157166940
|
@ -0,0 +1,20 @@
|
|||
// Copyright 2024 Dolphin Emulator Project
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
#include "Common/JsonUtil.h"
|
||||
|
||||
picojson::object ToJsonObject(const Common::Vec3& vec)
|
||||
{
|
||||
picojson::object obj;
|
||||
obj.emplace("x", vec.x);
|
||||
obj.emplace("y", vec.y);
|
||||
obj.emplace("z", vec.z);
|
||||
return obj;
|
||||
}
|
||||
|
||||
void FromJson(const picojson::object& obj, Common::Vec3& vec)
|
||||
{
|
||||
vec.x = ReadNumericOrDefault<float>(obj, "x");
|
||||
vec.y = ReadNumericOrDefault<float>(obj, "y");
|
||||
vec.z = ReadNumericOrDefault<float>(obj, "z");
|
||||
}
|
|
@ -3,10 +3,13 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include <span>
|
||||
#include <string>
|
||||
|
||||
#include <picojson.h>
|
||||
|
||||
#include "Common/MathUtil.h"
|
||||
#include "Common/Matrix.h"
|
||||
|
||||
// Ideally this would use a concept like, 'template <std::ranges::range Range>' to constrain it,
|
||||
// but unfortunately we'd need to require clang 15 for that, since the ranges library isn't
|
||||
// fully implemented until then, but this should suffice.
|
||||
|
@ -24,3 +27,18 @@ picojson::array ToJsonArray(const Range& data)
|
|||
|
||||
return result;
|
||||
}
|
||||
|
||||
template <typename Type>
|
||||
Type ReadNumericOrDefault(const picojson::object& obj, const std::string& key,
|
||||
Type default_value = Type{})
|
||||
{
|
||||
const auto it = obj.find(key);
|
||||
if (it == obj.end())
|
||||
return default_value;
|
||||
if (!it->second.is<double>())
|
||||
return default_value;
|
||||
return MathUtil::SaturatingCast<Type>(it->second.get<double>());
|
||||
}
|
||||
|
||||
picojson::object ToJsonObject(const Common::Vec3& vec);
|
||||
void FromJson(const picojson::object& obj, Common::Vec3& vec);
|
||||
|
|
Loading…
Reference in New Issue