Skip to content

Commit

Permalink
detect and repair invalid json
Browse files Browse the repository at this point in the history
  • Loading branch information
smk762 committed Sep 13, 2022
1 parent 494befc commit 41cd270
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 65 deletions.
11 changes: 3 additions & 8 deletions src/app/app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -295,19 +295,14 @@ namespace atomic_dex

if (fs::exists(wallet_custom_cfg_path))
{
nlohmann::json custom_config_json_data;
QFile file;
file.setFileName(std_path_to_qstring(wallet_custom_cfg_path));
file.open(QIODevice::ReadOnly | QIODevice::Text);

//! Read Contents
custom_config_json_data = nlohmann::json::parse(QString(file.readAll()).toStdString());
file.close();
nlohmann::json custom_config_json_data = utils::read_json_file(wallet_custom_cfg_path);

//! Modify
for (auto&& [key, value]: custom_config_json_data.items()) { value["active"] = false; }

//! Write
QFile file;
file.setFileName(std_path_to_qstring(wallet_custom_cfg_path));
file.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate);
file.write(QString::fromStdString(custom_config_json_data.dump()).toUtf8());
file.close();
Expand Down
15 changes: 13 additions & 2 deletions src/core/atomicdex/managers/qt.wallet.manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -300,12 +300,23 @@ namespace atomic_dex
const std::string wallet_cfg_file = std::string(atomic_dex::get_raw_version()) + "-coins"s + "."s + wallet_name.toStdString() + ".json"s;
const fs::path wallet_cfg_path = utils::get_atomic_dex_config_folder() / wallet_cfg_file;

bool valid_json = false;

if (not fs::exists(wallet_cfg_path))
if (fs::exists(wallet_cfg_path))
{
QFile ifs;
ifs.setFileName(std_path_to_qstring(wallet_cfg_path));
ifs.open(QIODevice::ReadOnly | QIODevice::Text);
std::string json_data = QString(ifs.readAll()).toUtf8().constData();
valid_json = nlohmann::json::accept(json_data);
ifs.close();
}

if (!valid_json)
{
const auto cfg_path = ag::core::assets_real_path() / "config";
std::string filename = std::string(atomic_dex::get_raw_version()) + "-coins.json";
fs::copy(cfg_path / filename, wallet_cfg_path);
fs::copy(cfg_path / filename, wallet_cfg_path, fs::copy_options::overwrite_existing);
}

const fs::path seed_path = utils::get_atomic_dex_config_folder() / (wallet_name.toStdString() + ".seed"s);
Expand Down
70 changes: 15 additions & 55 deletions src/core/atomicdex/services/mm2/mm2.service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,23 +148,9 @@ namespace
std::string custom_tokens_filename = "custom-tokens." + wallet_name + ".json";
fs::path custom_tokens_filepath = cfg_path / custom_tokens_filename;

QFile ifs;
ifs.setFileName(atomic_dex::std_path_to_qstring((cfg_path / filename)));
ifs.open(QIODevice::ReadOnly | QIODevice::Text);
nlohmann::json config_json_data = atomic_dex::utils::read_json_file(cfg_path / filename);
nlohmann::json custom_cfg_data = atomic_dex::utils::read_json_file(custom_tokens_filepath);

nlohmann::json config_json_data;
nlohmann::json custom_cfg_data;

if (fs::exists(custom_tokens_filepath.c_str()))
{
QFile ifs_custom;
ifs_custom.setFileName(atomic_dex::std_path_to_qstring(custom_tokens_filepath));
ifs_custom.open(QIODevice::ReadOnly | QIODevice::Text);
custom_cfg_data = nlohmann::json::parse(QString(ifs_custom.readAll()).toStdString());
ifs_custom.close();
}

config_json_data = nlohmann::json::parse(QString(ifs.readAll()).toStdString());
{
std::shared_lock lock(registry_mtx);
for (auto&& ticker: tickers)
Expand All @@ -189,8 +175,6 @@ namespace
}
}

ifs.close();

//! Write contents
QFile ofs;
ofs.setFileName(atomic_dex::std_path_to_qstring((cfg_path / filename)));
Expand Down Expand Up @@ -231,10 +215,7 @@ namespace atomic_dex
{
try
{
QFile ifs;
ifs.setFileName(atomic_dex::std_path_to_qstring(path));
ifs.open(QIODevice::ReadOnly | QIODevice::Text);
nlohmann::json config_json_data = nlohmann::json::parse(QString(ifs.readAll()).toStdString());
nlohmann::json config_json_data = atomic_dex::utils::read_json_file(path);
auto res = config_json_data.get<std::unordered_map<std::string, atomic_dex::coin_config>>();
return res;
}
Expand Down Expand Up @@ -2005,19 +1986,9 @@ namespace atomic_dex
fs::path cfg_path = utils::get_atomic_dex_config_folder();
std::string filename = "custom-tokens." + m_current_wallet_name + ".json";
fs::path file_path = cfg_path / filename;
nlohmann::json config_json_data;

if (fs::exists(file_path))
{
SPDLOG_DEBUG("reading contents of custom tokens cfg");
QFile ifs;
ifs.setFileName(std_path_to_qstring(file_path));
ifs.open(QIODevice::Text | QIODevice::ReadOnly);

//! Read Contents
config_json_data = nlohmann::json::parse(QString(ifs.readAll()).toStdString());
ifs.close();
}
SPDLOG_DEBUG("reading contents of custom tokens cfg");
nlohmann::json config_json_data = atomic_dex::utils::read_json_file(file_path);

//! Modify contents
config_json_data[coin_cfg_json.begin().key()] = coin_cfg_json.at(coin_cfg_json.begin().key());
Expand All @@ -2031,15 +2002,13 @@ namespace atomic_dex
}
if (not raw_coin_cfg_json.empty() && not is_this_ticker_present_in_raw_cfg(raw_coin_cfg_json.at("coin").get<std::string>()))
{
const fs::path mm2_cfg_path{atomic_dex::utils::get_current_configs_path() / "coins.json"};
SPDLOG_DEBUG("Adding entry : {} to mm2 coins file {}", raw_coin_cfg_json.dump(4), mm2_cfg_path.string());
const fs::path coins_json_path{atomic_dex::utils::get_current_configs_path() / "coins.json"};
SPDLOG_DEBUG("Adding entry : {} to mm2 coins file {}", raw_coin_cfg_json.dump(4), coins_json_path.string());
QFile ifs;
ifs.setFileName(std_path_to_qstring(mm2_cfg_path));
ifs.setFileName(std_path_to_qstring(coins_json_path));
ifs.open(QIODevice::ReadOnly | QIODevice::Text);
nlohmann::json config_json_data;

//! Read Contents
config_json_data = nlohmann::json::parse(QString(ifs.readAll()).toStdString());
nlohmann::json config_json_data = atomic_dex::utils::read_json_file(coins_json_path);

//! Modify contents
config_json_data.push_back(raw_coin_cfg_json);
Expand All @@ -2049,7 +2018,7 @@ namespace atomic_dex

//! Write contents
QFile ofs;
ofs.setFileName(std_path_to_qstring(mm2_cfg_path));
ofs.setFileName(std_path_to_qstring(coins_json_path));
ofs.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate);
ofs.write(QString::fromStdString(config_json_data.dump()).toUtf8());
ofs.close();
Expand Down Expand Up @@ -2082,25 +2051,16 @@ namespace atomic_dex
SPDLOG_DEBUG("remove it from custom cfg: {}", ticker);
fs::path cfg_path = utils::get_atomic_dex_config_folder();
std::string filename = "custom-tokens." + m_current_wallet_name + ".json";
QFile ifs;
ifs.setFileName(std_path_to_qstring((cfg_path / filename)));
ifs.open(QIODevice::ReadOnly | QIODevice::Text);
nlohmann::json config_json_data;


//! Read Contents
config_json_data = nlohmann::json::parse(QString(ifs.readAll()).toStdString());

SPDLOG_DEBUG("reading contents of custom tokens cfg");
nlohmann::json config_json_data = atomic_dex::utils::read_json_file(cfg_path / filename);
{
std::unique_lock lock(m_coin_cfg_mutex);
this->m_coins_informations.erase(ticker);
}

config_json_data.erase(config_json_data.find(ticker));

//! Close
ifs.close();

//! Write contents
QFile ofs;
ofs.setFileName(std_path_to_qstring((cfg_path / filename)));
Expand All @@ -2112,9 +2072,9 @@ namespace atomic_dex
if (is_this_ticker_present_in_raw_cfg(ticker))
{
SPDLOG_DEBUG("remove it from mm2 cfg: {}", ticker);
fs::path mm2_cfg_path{atomic_dex::utils::get_current_configs_path() / "coins.json"};
fs::path coins_json_path{atomic_dex::utils::get_current_configs_path() / "coins.json"};
QFile ifs;
ifs.setFileName(std_path_to_qstring(mm2_cfg_path));
ifs.setFileName(std_path_to_qstring(coins_json_path));
ifs.open(QIODevice::ReadOnly | QIODevice::Text);
nlohmann::json config_json_data;

Expand All @@ -2130,7 +2090,7 @@ namespace atomic_dex

//! Write contents
QFile ofs;
ofs.setFileName(std_path_to_qstring(mm2_cfg_path));
ofs.setFileName(std_path_to_qstring(coins_json_path));
ofs.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate);
ofs.write(QString::fromStdString(config_json_data.dump()).toUtf8());
ofs.close();
Expand Down
26 changes: 26 additions & 0 deletions src/core/atomicdex/utilities/global.utilities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
//! Qt Headers
#include <QCryptographicHash>
#include <QString>
#include <QFile>


//! Project Headers
#include "atomicdex/utilities/global.utilities.hpp"
Expand Down Expand Up @@ -306,4 +308,28 @@ namespace atomic_dex::utils
}
return out;
}

nlohmann::json
read_json_file(fs::path filepath)
{
nlohmann::json valid_json_data;

if (fs::exists(filepath))
{
QFile ifs;
#if defined(_WIN32) || defined(WIN32)
ifs.setFileName(QString::fromStdWString(filepath.wstring()));
#else
ifs.setFileName(QString::fromStdString(filepath.string()));
#endif
ifs.open(QIODevice::ReadOnly | QIODevice::Text);
std::string json_str = QString(ifs.readAll()).toUtf8().constData();
if (nlohmann::json::accept(json_str))
{
valid_json_data = nlohmann::json::parse(json_str);
}
ifs.close();
}
return valid_json_data;
}
} // namespace atomic_dex::utils
2 changes: 2 additions & 0 deletions src/core/atomicdex/utilities/global.utilities.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ namespace atomic_dex::utils

fs::path get_atomic_dex_config_folder();

nlohmann::json read_json_file(fs::path filepath);

//std::string minimal_trade_amount_str();

//const t_float_50 minimal_trade_amount();
Expand Down

0 comments on commit 41cd270

Please sign in to comment.