diff options
author | Aki <please@ignore.pl> | 2022-05-20 23:21:08 +0200 |
---|---|---|
committer | Aki <please@ignore.pl> | 2022-05-20 23:21:08 +0200 |
commit | f233014e960a412d3c5d4ba0015f09aa620fe567 (patch) | |
tree | d0c0790e01f36966ed935f783f499e801d8e67a0 | |
parent | 650d81b8d185ae54e456b5159c3b94e0df94e252 (diff) | |
download | derelict-f233014e960a412d3c5d4ba0015f09aa620fe567.zip derelict-f233014e960a412d3c5d4ba0015f09aa620fe567.tar.gz derelict-f233014e960a412d3c5d4ba0015f09aa620fe567.tar.bz2 |
Simplified JSON reading
-rw-r--r-- | DumpSource.cpp | 41 | ||||
-rw-r--r-- | 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 <ctime> +#include <sstream> #include <string> #include <utility> #include <vector> @@ -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<long double>(), - info["victim"]["position"]["y"].get<long double>(), - info["victim"]["position"]["z"].get<long double>(), - }; - auto id = info["solar_system_id"].get<long int>(); - auto location = dump["locations"][std::to_string(id)]; - km.location.system = location["name"].get<std::string>().data(); - km.location.constellation = location["constellation"].get<std::string>().data(); - km.location.region = location["region"].get<std::string>().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<std::string>()); + 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); +} @@ -1,5 +1,7 @@ #pragma once +#include <ctime> + #include "Location.h" #include "LongVector3.h" #include "Owner.h" @@ -12,4 +14,5 @@ struct Killmail Owner owner; Ship ship; LongVector3 position; + std::tm time; }; |