From 8158c85de7415f56aba840f1a1da0f7fe902f657 Mon Sep 17 00:00:00 2001 From: tastytea Date: Mon, 29 Jun 2020 06:10:15 +0200 Subject: [PATCH] Add JSON converter. --- .drone.yml | 4 ++-- src/CMakeLists.txt | 4 +++- src/json.cpp | 33 +++++++++++++++++++++++++++++++++ src/json.hpp | 34 ++++++++++++++++++++++++++++++++++ src/main.cpp | 4 ++++ 5 files changed, 76 insertions(+), 3 deletions(-) create mode 100644 src/json.cpp create mode 100644 src/json.hpp diff --git a/.drone.yml b/.drone.yml index 3b56ae1..ca1db1f 100644 --- a/.drone.yml +++ b/.drone.yml @@ -25,8 +25,8 @@ steps: - rm /etc/apt/apt.conf.d/docker-clean - alias apt-get='rm -f /var/cache/apt/archives/lock && apt-get' - apt-get update -q - - apt-get install -qq catch libcgicc-dev - apt-get install -qq build-essential cmake clang pkg-config + - apt-get install -qq catch libcgicc-dev nlohmann-json-dev - rm -rf build && mkdir -p build && cd build - cmake -G "Unix Makefiles" -DWITH_TESTS=YES .. - make VERBOSE=1 @@ -54,8 +54,8 @@ steps: - rm /etc/apt/apt.conf.d/docker-clean - alias apt-get='rm -f /var/cache/apt/archives/lock && apt-get' - apt-get update -q - - apt-get install -qq catch libcgicc-dev - apt-get install -qq build-essential cmake clang pkg-config + - apt-get install -qq catch libcgicc-dev nlohmann-json-dev - rm -rf build && mkdir -p build && cd build - cmake -G "Unix Makefiles" -DWITH_TESTS=YES .. - make VERBOSE=1 diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 796d121..d12f065 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -2,6 +2,8 @@ include(GNUInstallDirs) find_package(PkgConfig REQUIRED) pkg_check_modules(cgicc REQUIRED IMPORTED_TARGET cgicc) +find_package(nlohmann_json REQUIRED CONFIG) + add_executable(${PROJECT_NAME} main.cpp) file(GLOB_RECURSE sources_src *.cpp) @@ -12,7 +14,7 @@ unset(sources_src) unset(headers_src) target_link_libraries(${PROJECT_NAME} - PRIVATE PkgConfig::cgicc) + PRIVATE PkgConfig::cgicc nlohmann_json::nlohmann_json) install(TARGETS ${PROJECT_NAME} RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}") diff --git a/src/json.cpp b/src/json.cpp new file mode 100644 index 0000000..cc9f081 --- /dev/null +++ b/src/json.cpp @@ -0,0 +1,33 @@ +/* This file is part of FediBlock-backend. + * Copyright © 2020 tastytea + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, version 3. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +#include "json.hpp" + +#include + +namespace FediBlock +{ + +string to_json(const entry_type &entry) +{ + nlohmann::json json{{"instance", entry.instance}, + {"tags", entry.tags}, + {"receipts", entry.receipts}, + {"description", entry.description}}; + return json.dump(4); +} + +} // namespace FediBlock diff --git a/src/json.hpp b/src/json.hpp new file mode 100644 index 0000000..a9f7abb --- /dev/null +++ b/src/json.hpp @@ -0,0 +1,34 @@ +/* This file is part of FediBlock-backend. + * Copyright © 2020 tastytea + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, version 3. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +#ifndef FEDIBLOCK_BACKEND_JSON_HPP +#define FEDIBLOCK_BACKEND_JSON_HPP + +#include "cgi.hpp" + +#include + +namespace FediBlock +{ + +using std::string; + +// Convert our object into a JSON string. +[[nodiscard]] string to_json(const entry_type &entry); + +} // namespace FediBlock + +#endif // FEDIBLOCK_BACKEND_JSON_HPP diff --git a/src/main.cpp b/src/main.cpp index 0e3f079..6263aa3 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -15,6 +15,7 @@ */ #include "cgi.hpp" +#include "json.hpp" #include @@ -42,6 +43,9 @@ void print_debug() } cout << "\r\n"; cout << " Description: " << entry.description << "\r\n"; + + cout << "\r\n\r\nJSON:\r\n"; + cout << to_json(entry) << "\r\n"; } int main()