From f233014e960a412d3c5d4ba0015f09aa620fe567 Mon Sep 17 00:00:00 2001 From: Aki Date: Fri, 20 May 2022 23:21:08 +0200 Subject: Simplified JSON reading --- DumpSource.cpp | 41 +++++++++++++++++++++++++++-------------- Killmail.h | 3 +++ 2 files changed, 30 insertions(+), 14 deletions(-) diff --git a/DumpSource.cpp b/DumpSource.cpp index 1fdf7a8..f20ce0f 100644 --- a/DumpSource.cpp +++ b/DumpSource.cpp @@ -1,5 +1,7 @@ #include "DumpSource.h" +#include +#include #include #include #include @@ -11,6 +13,14 @@ using json = nlohmann::json; +using tm = std::tm; + + +void from_json(const json& j, tm& d); +void from_json(const json& j, Killmail& km); + + +NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(LongVector3, x, y, z) DumpSource::DumpSource(const char* filename) : @@ -20,20 +30,7 @@ DumpSource::DumpSource(const char* filename) : throw "File does not exist"; char* text = LoadFileText(filename); auto dump = json::parse(text); - for (const auto& info : dump["killmails"]) { - Killmail km; - km.position = { - info["victim"]["position"]["x"].get(), - info["victim"]["position"]["y"].get(), - info["victim"]["position"]["z"].get(), - }; - auto id = info["solar_system_id"].get(); - auto location = dump["locations"][std::to_string(id)]; - km.location.system = location["name"].get().data(); - km.location.constellation = location["constellation"].get().data(); - km.location.region = location["region"].get().data(); - m_killmails.push_back(std::move(km)); - } + dump.at("killmails").get_to(m_killmails); UnloadFileText(text); } @@ -43,3 +40,19 @@ DumpSource::killmails() const { return m_killmails; } + + +void +from_json(const json& j, tm& d) +{ + std::istringstream str(j.get()); + str >> std::get_time(&d, "%Y-%m-%dT%H:%M:%SZ"); +} + + +void +from_json(const json& j, Killmail& km) +{ + j.at("victim").at("position").get_to(km.position); + j.at("killmail_time").get_to(km.time); +} diff --git a/Killmail.h b/Killmail.h index 6550dc9..23db3c6 100644 --- a/Killmail.h +++ b/Killmail.h @@ -1,5 +1,7 @@ #pragma once +#include + #include "Location.h" #include "LongVector3.h" #include "Owner.h" @@ -12,4 +14,5 @@ struct Killmail Owner owner; Ship ship; LongVector3 position; + std::tm time; }; -- cgit v1.1