Skip to content

Commit

Permalink
Started to replace all instaces of Cereal with "nlohmann::json". Cere…
Browse files Browse the repository at this point in the history
…al has some limitations when it comes to backwards compatibility that I want to avoid.
  • Loading branch information
sago007 committed May 11, 2024
1 parent b15a741 commit 26c50dc
Showing 1 changed file with 14 additions and 26 deletions.
40 changes: 14 additions & 26 deletions source/code/highscore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,21 @@ Source information and contacts persons can be found at

#include "highscore.h"
#include "os.hpp"
#include "cereal/cereal.hpp"
#include "cereal/types/vector.hpp"
#include "cereal/archives/json.hpp"
#include "sago/SagoMisc.hpp"
#include <algorithm>
#include "common.h"
#include <fmt/core.h>
#include "nlohmann/json.hpp"

namespace cereal {
using json = nlohmann::json;

template<class Archive>
void save(Archive& archive, record const& m) {
archive( cereal::make_nvp("Name", m.name), cereal::make_nvp("Score", m.score) );
}

template<class Archive>
void load(Archive& archive, record& m) {
archive( cereal::make_nvp("Name", m.name), cereal::make_nvp("Score", m.score) );
void to_json(json& j, const record& p) {
j = json{ {"Name", p.name}, {"Score", p.score}};
}

void from_json(const json& j, record& p) {
p.name = j.at("Name").get<std::string>();
p.score = j.at("Score").get<int>();
}

/*
Expand All @@ -59,21 +54,17 @@ Highscore::Highscore(const std::string& type, double speed) : filename(type+".js
}
std::string readFileContent = sago::GetFileContent(filename.c_str());
if (readFileContent.length() > 0) {
json j = json::parse(readFileContent);
try {
std::stringstream ss(readFileContent);
{
cereal::JSONInputArchive archive(ss);
archive(cereal::make_nvp("highscore", table));
}
}
catch (cereal::Exception& e) {
j.at("highscore").get_to(table);
} catch (json::exception& e) {
std::cerr << "Failed to read highscore " << filename << " due to formatting errors. Resetting the file. Reason: " <<
e.what() << "\n";
table.clear();
}
}
if (table.size() < top) {
for (int i = 0; i<top; i++) {
for (size_t i = table.size(); i<top; i++) {
record r;
r.name = "Poul Sander";
r.score = 2000 - i*100;
Expand All @@ -87,12 +78,9 @@ Highscore::Highscore(const std::string& type, double speed) : filename(type+".js


void Highscore::writeFile() {
std::stringstream ss;
{
cereal::JSONOutputArchive archive(ss);
archive(cereal::make_nvp("highscore", table));
}
sago::WriteFileContent(filename.c_str(), ss.str());
json j = json{ { "highscore", table } };
std::string s = j.dump(4);
sago::WriteFileContent(filename.c_str(), s);
}

bool Highscore::isHighScore(int newScore) {
Expand Down

0 comments on commit 26c50dc

Please sign in to comment.