Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 0 additions & 27 deletions CMake/FindJsonCppCustom.cmake

This file was deleted.

20 changes: 3 additions & 17 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -58,21 +58,8 @@ if (CURL_FOUND)
target_compile_definitions(${PROJECT_NAME} PUBLIC -DHAVE_CURL)
endif()

## jsoncpp
if (NOT TARGET JsonCpp::JsonCpp)
find_package(jsoncpp)
if (NOT TARGET JsonCpp::JsonCpp)
message(STATUS "Using alternative findjsoncpp")
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/CMake")
find_package(JsonCppCustom)
if (NOT TARGET JsonCpp::JsonCpp)
message(FATAL_ERROR "Jsoncpp is not available")
endif()
endif()
endif()
install(FILES CMake/FindJsonCppCustom.cmake
DESTINATION lib/cmake/tgbot-cpp
)
## nlohmann_json
find_package(nlohmann_json 3.2.0 REQUIRED)

## boost
set(Boost_USE_MULTITHREADED ON)
Expand All @@ -91,7 +78,7 @@ set(LIB_LIST
ZLIB::ZLIB
OpenSSL::SSL
Boost::system
JsonCpp::JsonCpp
nlohmann_json::nlohmann_json
)

if (CURL_FOUND)
Expand All @@ -105,7 +92,6 @@ endif()
target_include_directories(${PROJECT_NAME} PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>)
target_include_directories(${PROJECT_NAME} PUBLIC ${jsoncpp_INCLUDE_DIRS} ${JSONCPP_PKG_INCLUDE_DIRS})
target_link_libraries(${PROJECT_NAME} ${LIB_LIST})
include(GNUInstallDirs)
install(TARGETS ${PROJECT_NAME}
Expand Down
3 changes: 1 addition & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ FROM debian:latest
MAINTAINER Oleg Morozenkov <m@oleg.rocks>

RUN apt-get -qq update && \
apt-get -qq install -y g++ make binutils cmake libssl-dev libboost-system-dev libcurl4-openssl-dev zlib1g-dev libjsoncpp-dev && \
apt-get -qq install -y g++ make binutils cmake libssl-dev libboost-system-dev libcurl4-openssl-dev zlib1g-dev nlohmann-json3-dev && \
rm -rf /var/lib/apt/lists/*

WORKDIR /usr/src/tgbot-cpp
COPY include include
COPY src src
COPY CMakeLists.txt ./
COPY CMake CMake

RUN cmake . && \
make -j$(nproc) && \
Expand Down
3 changes: 1 addition & 2 deletions Dockerfile_test
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ RUN apt-get -qq update && \
python2.7-dev \
wget \
zlib1g-dev \
libjsoncpp-dev \
nlohmann-json3-dev \
pkg-config && \
rm -rf /var/lib/apt/lists/*

Expand All @@ -26,7 +26,6 @@ COPY include include
COPY src src
COPY test test
COPY CMakeLists.txt ./
COPY CMake CMake

RUN cmake -DENABLE_TESTS=ON . && \
make -j$(nproc) && \
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ Dependencies:
- Boost
- OpenSSL
- ZLib
- JsonCpp
- nlohmann/json
- Libcurl (optional unless you want to use curl-based http client `CurlHttpClient`).


Expand All @@ -69,7 +69,7 @@ Dependencies:
You can install dependencies on Debian-based distibutives with these commands:

```sh
sudo apt install g++ make binutils cmake libboost-system-dev libssl-dev zlib1g-dev libcurl4-openssl-dev libjsoncpp-dev
sudo apt install g++ make binutils cmake libboost-system-dev libssl-dev zlib1g-dev libcurl4-openssl-dev nlohmann-json3-dev
```

Optionally, install the dependencies for testing and documenting
Expand All @@ -95,7 +95,7 @@ Alternatively, you can use Docker to build and run your bot. Set the base image
You can install dependencies with these commands:

```sh
brew install gcc cmake boost openssl zlib curl jsoncpp
brew install gcc cmake boost openssl zlib curl nlohmann-json
```

You can compile and install the library like Linux instructions.
Expand Down
1 change: 1 addition & 0 deletions _codeql_detected_source_root
49 changes: 24 additions & 25 deletions include/tgbot/TgTypeParser.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef TGBOT_TGTYPEPARSER_H
#define TGBOT_TGTYPEPARSER_H

#include <json/json.h>
#include <nlohmann/json.hpp>

#include <memory>
#include <sstream>
Expand Down Expand Up @@ -256,15 +256,15 @@ constexpr bool is_matrix_v = is_matrix<T>::value;

// Parse function for shared_ptr<T>
template <typename T>
std::shared_ptr<T> parse(const Json::Value &data) = delete;
std::shared_ptr<T> parse(const nlohmann::json &data) = delete;

#define DECLARE_PARSER_FROM_JSON(TYPE) \
template <> \
TYPE::Ptr parse(const Json::Value &data)
TYPE::Ptr parse(const nlohmann::json &data)

// Grab array of T from JSON array.
template <typename T>
std::vector<std::shared_ptr<T>> parseArray(const Json::Value &data) {
std::vector<std::shared_ptr<T>> parseArray(const nlohmann::json &data) {
std::vector<std::shared_ptr<T>> result;
for (const auto &item : data) {
result.emplace_back(parse<T>(item));
Expand All @@ -274,17 +274,17 @@ std::vector<std::shared_ptr<T>> parseArray(const Json::Value &data) {

// Parse array from a key.
template <typename T>
std::vector<std::shared_ptr<T>> parseArray(const Json::Value &data,
std::vector<std::shared_ptr<T>> parseArray(const nlohmann::json &data,
const std::string &key) {
if (!data.isMember(key)) {
if (!data.contains(key)) {
return {};
}
return parseArray<T>(data[key]);
}

// Parse 2D array of T from JSON.
template <typename T>
Matrix<std::shared_ptr<T>> parseMatrix(const Json::Value &data) {
Matrix<std::shared_ptr<T>> parseMatrix(const nlohmann::json &data) {
Matrix<std::shared_ptr<T>> result;
for (const auto &item : data) {
result.emplace_back(parseArray<T>(item));
Expand All @@ -293,78 +293,77 @@ Matrix<std::shared_ptr<T>> parseMatrix(const Json::Value &data) {
}

template <typename T>
Matrix<std::shared_ptr<T>> parseMatrix(const Json::Value &data,
Matrix<std::shared_ptr<T>> parseMatrix(const nlohmann::json &data,
const std::string &key) {
if (!data.isMember(key)) {
if (!data.contains(key)) {
return {};
}
return parseMatrix<T>(data[key]);
}

// Parse an array of primitive types.
template <typename T>
std::vector<T> parsePrimitiveArray(const Json::Value &data,
std::vector<T> parsePrimitiveArray(const nlohmann::json &data,
const std::string &key) {
if (!data.isMember(key)) {
if (!data.contains(key)) {
return {};
}
std::vector<T> result;
for (const auto &item : data[key]) {
result.emplace_back(item.as<T>());
result.emplace_back(item.get<T>());
}
return result;
}

// Put function for objects to JSON
template <typename T>
Json::Value put(const T &value) = delete;
nlohmann::json put(const T &value) = delete;

#define DECLARE_PARSER_TO_JSON(TYPE) \
template <> \
Json::Value put(const TYPE::Ptr &object)
nlohmann::json put(const TYPE::Ptr &object)

// Helper to put base class shared_ptr to derived T.
template <typename T, typename V,
std::enable_if_t<detail::is_shared_ptr_v<V> &&
!std::is_same_v<typename T::Ptr, V> &&
std::is_base_of_v<typename V::element_type, T>,
bool> = true>
Json::Value put(const V &data) {
nlohmann::json put(const V &data) {
return put(std::static_pointer_cast<T>(data));
}

// Put vector to JSON.
template <typename T, std::enable_if_t<!detail::is_vector_v<T>, bool> = true>
Json::Value put(const std::vector<T> &vector) {
Json::Value dataArray(Json::arrayValue);
nlohmann::json put(const std::vector<T> &vector) {
nlohmann::json dataArray = nlohmann::json::array();
for (const auto &item : vector) {
if constexpr (detail::is_primitive_v<T>) {
dataArray.append(item);
dataArray.push_back(item);
} else if constexpr (std::is_same_v<std::string_view, T>) {
dataArray.append(std::string(item));
dataArray.push_back(std::string(item));
} else {
// Recursively call put for non-primitives
dataArray.append(put(item));
dataArray.push_back(put(item));
}
}
return dataArray;
}

// Put 2D array (Matrix) to JSON.
template <typename T>
Json::Value put(const Matrix<T> &matrix) {
Json::Value dataMatrix(Json::arrayValue);
nlohmann::json put(const Matrix<T> &matrix) {
nlohmann::json dataMatrix = nlohmann::json::array();
for (const auto &row : matrix) {
dataMatrix.append(put(row)); // Recursively call put for each row
dataMatrix.push_back(put(row)); // Recursively call put for each row
}
return dataMatrix;
}

// Serialize object to JSON string.
template <typename T>
std::string putJSON(const T &object) {
Json::StreamWriterBuilder writer;
return Json::writeString(writer, put(object));
return put(object).dump();
}

#define IMPLEMENT_PARSERS(type) \
Expand Down
6 changes: 2 additions & 4 deletions include/tgbot/net/TgWebhookServer.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef TGBOT_TGHTTPSERVER_H
#define TGBOT_TGHTTPSERVER_H

#include <json/json.h>
#include <nlohmann/json.hpp>

#include <string>
#include <unordered_map>
Expand Down Expand Up @@ -38,9 +38,7 @@ class TgWebhookServer : public HttpServer<Protocol> {
const std::string &data,
const std::unordered_map<std::string, std::string> &headers) {
if (headers.at("_method") == "POST" && headers.at("_path") == _path) {
Json::Value update;
Json::Reader reader;
reader.parse(data, update, false);
nlohmann::json update = nlohmann::json::parse(data);
_eventHandler->handleUpdate(parse<Update>(update));
}
return HttpParser::generateResponse("", "text/plain", 200, "OK", false);
Expand Down
7 changes: 2 additions & 5 deletions samples/common_defs.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,11 @@ set(Boost_USE_MULTITHREADED ON)
find_package(Threads REQUIRED)
find_package(OpenSSL REQUIRED)
find_package(Boost COMPONENTS system REQUIRED)
find_package(jsoncpp QUIET)
if (NOT TARGET JsonCpp::JsonCPP)
include(/usr/local/lib/cmake/tgbot-cpp/FindJsonCppCustom.cmake)
endif()
find_package(nlohmann_json 3.2.0 REQUIRED)
find_package(CURL)
include_directories(/usr/local/include ${OPENSSL_INCLUDE_DIR} ${Boost_INCLUDE_DIR})
if (CURL_FOUND)
include_directories(${CURL_INCLUDE_DIRS})
add_definitions(-DHAVE_CURL)
endif()
set(LINK_LIB_LIST /usr/local/lib/libTgBot.a ${CMAKE_THREAD_LIBS_INIT} ${OPENSSL_LIBRARIES} ${Boost_LIBRARIES} ${CURL_LIBRARIES} JsonCpp::JsonCpp)
set(LINK_LIB_LIST /usr/local/lib/libTgBot.a ${CMAKE_THREAD_LIBS_INIT} ${OPENSSL_LIBRARIES} ${Boost_LIBRARIES} ${CURL_LIBRARIES} nlohmann_json::nlohmann_json)
2 changes: 1 addition & 1 deletion samples/echobot-submodule/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ project(echobot-submodule)
include(../common_defs.cmake)
add_subdirectory(tgbot-cpp)
add_executable(echobot-submodule src/main.cpp)
target_link_libraries(echobot-submodule TgBot ${CMAKE_THREAD_LIBS_INIT} ${OPENSSL_LIBRARIES} ${Boost_LIBRARIES} ${CURL_LIBRARIES} JsonCpp::JsonCpp)
target_link_libraries(echobot-submodule TgBot ${CMAKE_THREAD_LIBS_INIT} ${OPENSSL_LIBRARIES} ${Boost_LIBRARIES} ${CURL_LIBRARIES} nlohmann_json::nlohmann_json)
Loading