Add JSON converter.

This commit is contained in:
tastytea 2020-06-29 06:10:15 +02:00
parent cd43a70ca0
commit 8158c85de7
Signed by: tastytea
GPG Key ID: CFC39497F1B26E07
5 changed files with 76 additions and 3 deletions

View File

@ -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

View File

@ -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}")

33
src/json.cpp Normal file
View File

@ -0,0 +1,33 @@
/* This file is part of FediBlock-backend.
* Copyright © 2020 tastytea <tastytea@tastytea.de>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#include "json.hpp"
#include <nlohmann/json.hpp>
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

34
src/json.hpp Normal file
View File

@ -0,0 +1,34 @@
/* This file is part of FediBlock-backend.
* Copyright © 2020 tastytea <tastytea@tastytea.de>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#ifndef FEDIBLOCK_BACKEND_JSON_HPP
#define FEDIBLOCK_BACKEND_JSON_HPP
#include "cgi.hpp"
#include <string>
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

View File

@ -15,6 +15,7 @@
*/
#include "cgi.hpp"
#include "json.hpp"
#include <iostream>
@ -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()