feat: json serializer of bound_symbol & magic_entry.

This commit is contained in:
2025-03-02 12:08:10 +08:00
parent 6ccf92fc25
commit 1b3fa4da55
6 changed files with 50 additions and 17 deletions

View File

@@ -28,11 +28,7 @@ void BoundSymbolList::write(const fs::path& path) const {
nlohmann::json data;
for (const auto& entity : m_entities) {
data.emplace_back(nlohmann::json{
{"symbol", entity.m_symbol_name},
{"rva", entity.m_rva },
{"is_function", entity.m_is_function}
});
data.emplace_back(entity);
}
ofs << data.dump(4);

View File

@@ -0,0 +1,19 @@
#include "data_format/type/bound_symbol.h"
#include <nlohmann/json.hpp>
namespace di {
void to_json(nlohmann::json& json, const BoundSymbol& symbol) {
json["symbol"] = symbol.m_symbol_name;
json["rva"] = symbol.m_rva;
json["is_function"] = symbol.m_is_function;
}
void from_json(const nlohmann::json& json, BoundSymbol& symbol) {
symbol.m_symbol_name = json["symbol"];
symbol.m_rva = json["rva"];
symbol.m_is_function = json["is_function"];
}
} // namespace di

View File

@@ -1,6 +1,6 @@
#pragma once
#include <boost/functional/hash.hpp>
#include <nlohmann/json_fwd.hpp>
namespace di {
@@ -15,6 +15,9 @@ struct BoundSymbol {
}
};
void to_json(nlohmann::json& json, const BoundSymbol& symbol);
void from_json(const nlohmann::json& json, BoundSymbol& symbol);
} // namespace di
namespace std {
@@ -23,9 +26,9 @@ template <>
struct hash<di::BoundSymbol> {
constexpr size_t operator()(const di::BoundSymbol& symbol) const {
size_t seed = 0;
boost::hash_combine(seed, symbol.m_symbol_name);
boost::hash_combine(seed, symbol.m_rva);
boost::hash_combine(seed, symbol.m_is_function);
hash_combine(seed, symbol.m_symbol_name);
hash_combine(seed, symbol.m_rva);
hash_combine(seed, symbol.m_is_function);
return seed;
}
};

View File

@@ -0,0 +1,15 @@
#include "data_format/type/magic_entry.h"
#include <nlohmann/json.hpp>
namespace di {
void to_json(nlohmann::json& json, const MagicEntry& entry) {
json["rva"] = entry.rva;
json["is_function"] = entry.is_function();
json["_unk2"] = entry._unk2();
json["is_verbose"] = entry.is_verbose();
json["_unk4"] = entry._unk4();
}
} // namespace di

View File

@@ -1,5 +1,7 @@
#pragma once
#include <nlohmann/json_fwd.hpp>
namespace di {
struct MagicEntry {
@@ -18,4 +20,9 @@ struct MagicEntry {
constexpr bool _unk4() const { return flags[3]; }
};
void to_json(nlohmann::json& json, const MagicEntry& entry);
// TODO
// void from_json(const nlohmann::json& json, MagicEntry& entry);
} // namespace di

View File

@@ -42,14 +42,7 @@ int main(int argc, char* argv[]) try {
nlohmann::json data;
blob.for_each([&data](hash_t hash, const MagicEntry& entry) {
data.emplace_back(nlohmann::json{
{"hash", hash },
{"rva", entry.rva },
{"is_function", entry.is_function()},
{"_unk2", entry._unk2() },
{"is_verbose", entry.is_verbose() },
{"_unk4", entry._unk4() }
});
data[hash] = entry;
});
std::ofstream ofs(args.m_output_path);