Merge branch 'nlohmann'

This commit is contained in:
Martin Brodbeck 2018-10-19 10:01:46 +02:00
commit 6dbd2919bd
7 changed files with 58 additions and 58 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()

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)
@ -57,7 +64,7 @@ else(WIN32 AND NOT UNIX)
set(CPACK_SOURCE_GENERATOR "TBZ2") set(CPACK_SOURCE_GENERATOR "TBZ2")
set(CPACK_GENERATOR "RPM;DEB") set(CPACK_GENERATOR "RPM;DEB")
set(CPACK_DEBIAN_PACKAGE_ARCHITECTURE amd64) set(CPACK_DEBIAN_PACKAGE_ARCHITECTURE amd64)
set(CPACK_DEBIAN_PACKAGE_DEPENDS "libqt5printsupport5 (>= 5.4), libjsoncpp1, libusb-1.0-0") set(CPACK_DEBIAN_PACKAGE_DEPENDS "libqt5printsupport5 (>= 5.4), libusb-1.0-0")
set(CPACK_RPM_EXCLUDE_FROM_AUTO_FILELIST_ADDITION set(CPACK_RPM_EXCLUDE_FROM_AUTO_FILELIST_ADDITION
"/usr/share/applications" "/usr/share/applications"
"/usr/share/icons" "/usr/share/icons"
@ -90,7 +97,6 @@ if( MINGW )
${MINGW_PATH}/libicuuc62.dll ${MINGW_PATH}/libicuuc62.dll
${MINGW_PATH}/libicuin62.dll ${MINGW_PATH}/libicuin62.dll
${MINGW_PATH}/libicudt62.dll ${MINGW_PATH}/libicudt62.dll
${MINGW_PATH}/libjsoncpp-20.dll
${MINGW_PATH}/libpcre2-16-0.dll ${MINGW_PATH}/libpcre2-16-0.dll
${MINGW_PATH}/zlib1.dll ${MINGW_PATH}/zlib1.dll
${MINGW_PATH}/libharfbuzz-0.dll ${MINGW_PATH}/libharfbuzz-0.dll

View file

@ -19,10 +19,10 @@ Ubuntu, Windows) angeboten. Bitte die Hinweise dort beachten.
### Selbst compilieren ### Selbst compilieren
KIMA2 benötigt folgende Libraries: KIMA2 benötigt folgende Libraries:
* Qt5 * Qt5
* jsoncpp
* xlnt >= 1.3.0
* boost >= 1.62 * boost >= 1.62
* libusb-1.0 * libusb-1.0
* xlnt >= 1.3.0
* nlohmann-json (als 3rdparty submodule vorhanden)
Da Features aus C++17 verwendet werden sowie std::filesystem, sollte als Compiler mindestens Da Features aus C++17 verwendet werden sowie std::filesystem, sollte als Compiler mindestens
GCC 8 verwendet werden. GCC 8 verwendet werden.

View file

@ -1,12 +1,12 @@
# Maintainer: Martin Brodbeck <martin at brodbeck-online dot de> # Maintainer: Martin Brodbeck <martin at brodbeck-online dot de>
pkgname=kima2 pkgname=kima2
pkgver=0.9.0 pkgver=0.12.0
pkgrel=1 pkgrel=1
pkgdesc="A small cash point program for children's things markets (German only)" pkgdesc="A small cash point program for children's things markets (German only)"
arch=('i686' 'x86_64') arch=('i686' 'x86_64')
url="http://www.rustysoft.de/?01_kima2" url="http://www.rustysoft.de/?01_kima2"
license=('custom') license=('custom')
depends=('glibc' 'libusb' 'qt5-base' 'sqlite3' 'xlnt' 'jsoncpp') depends=('glibc' 'libusb' 'qt5-base' 'sqlite3' 'xlnt')
makedepends=('boost>=1.62') makedepends=('boost>=1.62')
source=($pkgname-$pkgver.tar.gz) source=($pkgname-$pkgver.tar.gz)
md5sums=('') md5sums=('')

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. "