mirror of
https://github.com/dolphin-emu/dolphin.git
synced 2025-07-23 22:29:39 -06:00
Merge pull request #12818 from iwubcode/json_file_operations
Common: add Json helper utilities for loading or saving to a file
This commit is contained in:
@ -87,6 +87,7 @@ add_library(common
|
||||
JitRegister.cpp
|
||||
JitRegister.h
|
||||
JsonUtil.h
|
||||
JsonUtil.cpp
|
||||
Lazy.h
|
||||
LinearDiskCache.h
|
||||
Logging/ConsoleListener.h
|
||||
|
@ -3,6 +3,10 @@
|
||||
|
||||
#include "Common/JsonUtil.h"
|
||||
|
||||
#include <fstream>
|
||||
|
||||
#include "Common/FileUtil.h"
|
||||
|
||||
picojson::object ToJsonObject(const Common::Vec3& vec)
|
||||
{
|
||||
picojson::object obj;
|
||||
@ -38,3 +42,27 @@ std::optional<bool> ReadBoolFromJson(const picojson::object& obj, const std::str
|
||||
return std::nullopt;
|
||||
return it->second.get<bool>();
|
||||
}
|
||||
|
||||
bool JsonToFile(const std::string& filename, const picojson::value& root, bool prettify)
|
||||
{
|
||||
std::ofstream json_stream;
|
||||
File::OpenFStream(json_stream, filename, std::ios_base::out);
|
||||
if (!json_stream.is_open())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
json_stream << root.serialize(prettify);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool JsonFromFile(const std::string& filename, picojson::value* root, std::string* error)
|
||||
{
|
||||
std::string json_data;
|
||||
if (!File::ReadFileToString(filename, json_data))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
*error = picojson::parse(*root, json_data);
|
||||
return error->empty();
|
||||
}
|
||||
|
@ -9,7 +9,6 @@
|
||||
|
||||
#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,
|
||||
@ -47,7 +46,7 @@ std::optional<Type> ReadNumericFromJson(const picojson::object& obj, const std::
|
||||
return std::nullopt;
|
||||
if (!it->second.is<double>())
|
||||
return std::nullopt;
|
||||
return MathUtil::SaturatingCast<Type>(it->second.get<double>());
|
||||
return static_cast<Type>(it->second.get<double>());
|
||||
}
|
||||
|
||||
std::optional<std::string> ReadStringFromJson(const picojson::object& obj, const std::string& key);
|
||||
@ -56,3 +55,6 @@ std::optional<bool> ReadBoolFromJson(const picojson::object& obj, const std::str
|
||||
|
||||
picojson::object ToJsonObject(const Common::Vec3& vec);
|
||||
void FromJson(const picojson::object& obj, Common::Vec3& vec);
|
||||
|
||||
bool JsonToFile(const std::string& filename, const picojson::value& root, bool prettify = false);
|
||||
bool JsonFromFile(const std::string& filename, picojson::value* root, std::string* error);
|
||||
|
Reference in New Issue
Block a user