replace jsoncpp with nlohmann_json

This commit is contained in:
Martin Brodbeck 2018-10-18 12:06:36 +02:00
parent 1aa56f24a3
commit 6b251a4a32
6 changed files with 54 additions and 52 deletions

3
.gitmodules vendored Normal file
View File

@ -0,0 +1,3 @@
[submodule "3rdparty/nlohmann_json"]
path = 3rdparty/nlohmann_json
url = https://github.com/nlohmann/json.git

4
3rdparty/CMakeLists.txt vendored Normal file
View File

@ -0,0 +1,4 @@
if(NOT KIMA2_USE_EXTERNAL_JSON)
set(JSON_BuildTests OFF CACHE INTERNAL "")
add_subdirectory(nlohmann_json)
endif()

1
3rdparty/nlohmann_json vendored Submodule

@ -0,0 +1 @@
Subproject commit b553a8a93cd72d45c942b9eb9e320fc0d44f5b74

View File

@ -21,6 +21,13 @@ set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
option(KIMA2_USE_EXTERNAL_JSON "Use an external JSON library" OFF)
if(KIMA2_USE_EXTERNAL_JSON)
find_package(nlohmann_json REQUIRED)
endif()
add_subdirectory(3rdparty)
add_subdirectory(src) add_subdirectory(src)
if(NOT CMAKE_BUILD_TYPE OR CMAKE_BUILD_TYPE MATCHES Debug) if(NOT CMAKE_BUILD_TYPE OR CMAKE_BUILD_TYPE MATCHES Debug)

View File

@ -2,13 +2,12 @@ set(Boost_USE_STATIC_LIBS ON)
find_package(Boost 1.62 COMPONENTS date_time REQUIRED) find_package(Boost 1.62 COMPONENTS date_time REQUIRED)
find_package(SQLite3 REQUIRED) find_package(SQLite3 REQUIRED)
if (MINGW) if (MINGW)
find_package(XLNT REQUIRED STATIC) find_package(XLNT REQUIRED STATIC)
find_package(JSONCPP REQUIRED)
else (MINGW) else (MINGW)
find_package(PkgConfig REQUIRED) find_package(PkgConfig REQUIRED)
pkg_check_modules(XLNT REQUIRED xlnt>=1.3) pkg_check_modules(XLNT REQUIRED xlnt>=1.3)
pkg_check_modules(JSONCPP REQUIRED jsoncpp)
endif (MINGW) endif (MINGW)
set(CORE_SOURCES set(CORE_SOURCES
@ -24,11 +23,12 @@ set(CORE_SOURCES
) )
add_library(core STATIC ${CORE_SOURCES}) add_library(core STATIC ${CORE_SOURCES})
if (WIN32) if (WIN32)
target_link_libraries(core printer Boost::boost Boost::date_time sqlite3 ${XLNT_LIBRARY} ${JSONCPP_LIBRARY}) target_link_libraries(core PRIVATE printer Boost::boost Boost::date_time sqlite3 nlohmann_json::nlohmann_json ${XLNT_LIBRARY})
target_link_libraries(core bcrypt) target_link_libraries(core PRIVATE bcrypt)
else() else()
target_link_libraries(core printer Boost::boost Boost::date_time sqlite3 ${XLNT_LIBRARIES} ${JSONCPP_LIBRARIES}) target_link_libraries(core PRIVATE printer Boost::boost Boost::date_time sqlite3 nlohmann_json::nlohmann_json ${XLNT_LIBRARIES})
endif() endif()
target_include_directories(core PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) target_include_directories(core PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})

View File

@ -1,54 +1,48 @@
#include "jsonutil.h" #include "jsonutil.h"
#include "database.h" #include "database.h"
#include <json/json.h> #include <nlohmann/json.hpp>
#include <fstream> #include <fstream>
namespace fs = std::filesystem; namespace fs = std::filesystem;
using json = nlohmann::json;
void JsonUtil::exportSellers(const fs::path& filePath, Marketplace* market) void JsonUtil::exportSellers(const std::filesystem::path& filePath, Marketplace* market)
{ {
Json::Value root; json root;
std::ofstream file(filePath); std::ofstream file(filePath);
Json::StreamWriterBuilder builder;
builder["commentStyle"] = "None";
builder["indentation"] = " ";
std::unique_ptr<Json::StreamWriter> writer(builder.newStreamWriter());
for (const auto& seller : market->getSellers()) { for (const auto& seller : market->getSellers()) {
Json::Value newEntry; json newEntry;
newEntry["uuid"] = seller->getUuidAsString(); newEntry["uuid"] = seller->getUuidAsString();
newEntry["seller_no"] = seller->getSellerNo(); newEntry["seller_no"] = seller->getSellerNo();
newEntry["last_name"] = seller->getLastName(); newEntry["last_name"] = seller->getLastName();
newEntry["first_name"] = seller->getFirstName(); newEntry["first_name"] = seller->getFirstName();
newEntry["num_offered_articles"] = seller->numArticlesOffered(); newEntry["num_offered_articles"] = seller->numArticlesOffered();
root["sellers"].append(newEntry); root["sellers"].push_back(newEntry);
} }
writer->write(root, &file); file << root.dump(4) << std::endl;
} }
void JsonUtil::importSellers(const fs::path& filePath, Marketplace* market) void JsonUtil::importSellers(const std::filesystem::path& filePath, Marketplace* market)
{ {
for (auto& seller : market->getSellers()) { for (auto& seller : market->getSellers()) {
seller->setState(Seller::State::DELETE); seller->setState(Seller::State::DELETE);
} }
market->storeToDb(true); market->storeToDb(true);
Json::Value jsonValues;
std::ifstream file(filePath); std::ifstream file(filePath);
file >> jsonValues; json jsonValues = json::parse(file);
for (auto val : jsonValues["sellers"]) { for (auto val : jsonValues["sellers"]) {
auto seller = std::make_unique<Seller>(); auto seller = std::make_unique<Seller>();
seller->setUuidFromString(val["uuid"].asString()); seller->setUuidFromString(val["uuid"]);
seller->setSellerNo(val["seller_no"].asInt()); seller->setSellerNo(val["seller_no"]);
seller->setLastName(val["last_name"].asString()); seller->setLastName(val["last_name"]);
seller->setFirstName(val["first_name"].asString()); seller->setFirstName(val["first_name"]);
seller->setNumArticlesOffered(val["num_offered_articles"].asInt()); seller->setNumArticlesOffered(val["num_offered_articles"]);
market->getSellers().push_back(std::move(seller)); market->getSellers().push_back(std::move(seller));
} }
@ -69,29 +63,23 @@ void JsonUtil::importSellers(const fs::path& filePath, Marketplace* market)
market->storeToDb(); market->storeToDb();
} }
void JsonUtil::exportSales(const fs::path& filePath, Marketplace* market, int cashPointNo) void JsonUtil::exportSales(const std::filesystem::path& filePath, Marketplace* market, int cashPointNo)
{ {
Json::Value root; json root;
std::ofstream file(filePath); std::ofstream file(filePath);
Json::StreamWriterBuilder builder;
builder["commentStyle"] = "None";
builder["indentation"] = " ";
std::unique_ptr<Json::StreamWriter> writer(builder.newStreamWriter());
root["source_no"] = cashPointNo; root["source_no"] = cashPointNo;
for (const auto& sale : market->getSales()) { for (const auto& sale : market->getSales()) {
if (sale->getSourceNo() != cashPointNo) if (sale->getSourceNo() != cashPointNo)
continue; continue;
Json::Value newSale; json newSale;
newSale["uuid"] = sale->getUuidAsString(); newSale["uuid"] = sale->getUuidAsString();
newSale["timestamp"] = sale->getTimestamp(); newSale["timestamp"] = sale->getTimestamp();
for (const auto& article : sale->getArticles()) { for (const auto& article : sale->getArticles()) {
Json::Value newArticle; json newArticle;
newArticle["uuid"] = article->getUuidAsString(); newArticle["uuid"] = article->getUuidAsString();
newArticle["seller_uuid"] = article->getSeller()->getUuidAsString(); newArticle["seller_uuid"] = article->getSeller()->getUuidAsString();
newArticle["desc"] = article->getDescription(); newArticle["desc"] = article->getDescription();
@ -99,43 +87,42 @@ void JsonUtil::exportSales(const fs::path& filePath, Marketplace* market, int ca
// newArticle["source_no"] = article->getSourceNo(); // newArticle["source_no"] = article->getSourceNo();
newArticle["article_no"] = article->getArticleNo(); newArticle["article_no"] = article->getArticleNo();
newSale["articles"].append(newArticle); newSale["articles"].push_back(newArticle);
} }
root["sales"].append(newSale); root["sales"].push_back(newSale);
} }
writer->write(root, &file); file << root.dump(4) << std::endl;
} }
void JsonUtil::importSales(const fs::path& filePath, Marketplace* market, int cashPointNo) void JsonUtil::importSales(const std::filesystem::path& filePath, Marketplace* market, int cashPointNo)
{ {
Json::Value jsonValues;
std::ifstream file(filePath); std::ifstream file(filePath);
file >> jsonValues; json jsonValues = json::parse(file);
int source_no = jsonValues["source_no"].asInt(); int source_no = jsonValues["source_no"];
if (source_no == cashPointNo) { if (source_no == cashPointNo) {
throw std::runtime_error("Die Kassen-Nr. der zu imporierenden Daten wird von dieser Kasse " throw std::runtime_error("Die Kassen-Nr. der zu imporierenden Daten wird von dieser Kasse "
"hier bereits verwendet."); "hier bereits verwendet.");
} }
market->setSalesToDelete(jsonValues["source_no"].asInt()); market->setSalesToDelete(jsonValues["source_no"]);
market->storeToDb(); market->storeToDb();
for (const auto& valSale : jsonValues["sales"]) { for (const auto& valSale : jsonValues["sales"]) {
auto sale = std::make_unique<Sale>(); auto sale = std::make_unique<Sale>();
sale->setUuidFromString(valSale["uuid"].asString()); sale->setUuidFromString(valSale["uuid"]);
sale->setSourceNo(jsonValues["source_no"].asInt()); sale->setSourceNo(jsonValues["source_no"]);
sale->setTimestamp(valSale["timestamp"].asString()); sale->setTimestamp(valSale["timestamp"]);
for (const auto& valArticle : valSale["articles"]) { for (const auto& valArticle : valSale["articles"]) {
auto article = std::make_unique<Article>(); auto article = std::make_unique<Article>();
article->setUuidFromString(valArticle["uuid"].asString()); article->setUuidFromString(valArticle["uuid"]);
article->setSourceNo(jsonValues["source_no"].asInt()); article->setSourceNo(jsonValues["source_no"]);
article->setArticleNo(valArticle["article_no"].asInt()); article->setArticleNo(valArticle["article_no"]);
article->setDescription(valArticle["desc"].asString()); article->setDescription(valArticle["desc"]);
article->setPrice(valArticle["price"].asInt()); article->setPrice(valArticle["price"]);
auto seller = market->findSellerWithUuid(valArticle["seller_uuid"].asString()); auto seller = market->findSellerWithUuid(valArticle["seller_uuid"]);
if (seller == nullptr) { if (seller == nullptr) {
throw std::runtime_error( throw std::runtime_error(
"Die zu importierenden Daten verweisen auf einen nicht vorhandenen Verkäufer. " "Die zu importierenden Daten verweisen auf einen nicht vorhandenen Verkäufer. "