Skip to content

Commit

Permalink
Using nlohmann::json instead of RapidJson
Browse files Browse the repository at this point in the history
  • Loading branch information
khabinov committed Dec 11, 2017
1 parent d0d4481 commit a98123c
Show file tree
Hide file tree
Showing 10 changed files with 131 additions and 467 deletions.
6 changes: 0 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ target_link_libraries(satorivideo
CONAN_PKG::Loguru
CONAN_PKG::Openssl
CONAN_PKG::PrometheusCpp
CONAN_PKG::Rapidjson
)
target_include_directories(satorivideo PUBLIC include)
target_compile_definitions(satorivideo PRIVATE CONAN_PACKAGE_VERSION="${CONAN_PACKAGE_VERSION}")
Expand Down Expand Up @@ -158,7 +157,6 @@ target_link_libraries(satori_video_publisher
CONAN_PKG::Json
CONAN_PKG::Loguru
CONAN_PKG::Openssl
CONAN_PKG::Rapidjson
CONAN_PKG::PrometheusCpp
)

Expand All @@ -177,7 +175,6 @@ target_link_libraries(satori_video_recorder
CONAN_PKG::Loguru
CONAN_PKG::Openssl
CONAN_PKG::PrometheusCpp
CONAN_PKG::Rapidjson
)

add_executable(satori_video_player src/clitools/player.cpp)
Expand All @@ -195,7 +192,6 @@ target_link_libraries(satori_video_player
CONAN_PKG::Loguru
CONAN_PKG::Openssl
CONAN_PKG::PrometheusCpp
CONAN_PKG::Rapidjson
CONAN_PKG::SDL
)

Expand All @@ -213,7 +209,6 @@ target_link_libraries(test_configure_bot
CONAN_PKG::Json
CONAN_PKG::Loguru
CONAN_PKG::Openssl
CONAN_PKG::Rapidjson
)


Expand Down Expand Up @@ -255,7 +250,6 @@ function(add_video_test TEST_NAME TEST_FILE)
CONAN_PKG::Loguru
CONAN_PKG::Openssl
CONAN_PKG::PrometheusCpp
CONAN_PKG::Rapidjson
)
add_test(${TEST_NAME} ${CMAKE_BINARY_DIR}/test/${TEST_NAME})
endfunction()
Expand Down
3 changes: 1 addition & 2 deletions conanfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,10 @@ class SatorivideoConan(ConanFile):
"Loguru/1.5.0@satorivideo/master", \
"Openssl/1.1.0g@satorivideo/master", \
"PrometheusCpp/2017.12.06@satorivideo/master", \
"Rapidjson/1.1.0@satorivideo/master", \
"SDL/2.0.5@satorivideo/master"

license = "proprietary"
version = '0.10.21'
version = '0.10.22'
settings = "os", "compiler", "build_type", "arch"
default_options = "with_opencv=True", \
"with_gperftools=True", \
Expand Down
44 changes: 16 additions & 28 deletions src/bot_environment.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
#include "bot_environment.h"

#include <rapidjson/document.h>
#include <rapidjson/error/en.h>
#include <rapidjson/filereadstream.h>
#include <rapidjson/stringbuffer.h>
#include <rapidjson/writer.h>
#include <algorithm>
#include <boost/asio.hpp>
#include <boost/program_options.hpp>
#include <fstream>
#include <gsl/gsl>
#include <json.hpp>

#include "bot_instance.h"
#include "cbor_json.h"
Expand Down Expand Up @@ -109,40 +105,32 @@ struct file_cbor_dump_observer : streams::observer<cbor_item_t*> {

} // namespace

cbor_item_t* read_config_from_file(const std::string& config_file) {
FILE* fp = fopen(config_file.c_str(), "r");
if (fp == nullptr) {
std::cerr << "Can't read config file " << config_file << ": " << strerror(errno)
<< std::endl;
exit(1);
}
auto file_closer = gsl::finally([&fp]() { fclose(fp); });
cbor_item_t* read_config_from_file(const std::string& config_file_name) {
nlohmann::json config;

char readBuffer[65536];
rapidjson::FileReadStream is(fp, readBuffer, sizeof(readBuffer));
rapidjson::Document d;
rapidjson::ParseResult ok = d.ParseStream(is);

if (!ok) {
std::cerr << "Config parse error at offset " << ok.Offset() << ": "
<< GetParseError_En(ok.Code()) << std::endl;
try {
std::ifstream config_file(config_file_name);
config = nlohmann::json::parse(config_file);
} catch (const std::exception& e) {
std::cerr << "Can't parse config file " << config_file_name << ": " << e.what()
<< std::endl;
exit(1);
}

return cbor_move(rapidjson_to_cbor(d));
return cbor_move(json_to_cbor(config));
}

cbor_item_t* read_config_from_arg(const std::string& arg) {
rapidjson::Document d;
rapidjson::ParseResult ok = d.Parse(arg.c_str());
nlohmann::json config;

if (!ok) {
std::cerr << "Config parse error at offset " << ok.Offset() << ": "
<< GetParseError_En(ok.Code()) << std::endl;
try {
config = nlohmann::json::parse(arg);
} catch (const std::exception& e) {
std::cerr << "Can't parse config: " << e.what() << "\nArg: " << arg << std::endl;
exit(1);
}

return cbor_move(rapidjson_to_cbor(d));
return cbor_move(json_to_cbor(config));
}

bot_environment& bot_environment::instance() {
Expand Down
157 changes: 0 additions & 157 deletions src/cbor_json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,163 +8,6 @@
namespace satori {
namespace video {

cbor_item_t* rapidjson_to_cbor(const rapidjson::Value& d) {
if (d.IsString()) {
return cbor_build_string(d.GetString());
}
if (d.IsInt()) {
int i = d.GetInt();
return i >= 0 ? cbor_build_uint32(i)
: cbor_build_negint32(static_cast<uint32_t>(-1 - i));
}
if (d.IsInt64()) {
int64_t i = d.GetInt64();
return i >= 0 ? cbor_build_uint64(i)
: cbor_build_negint64(static_cast<uint64_t>(-1 - i));
}
if (d.IsDouble()) {
return cbor_build_float8(d.GetDouble());
}
if (d.IsArray()) {
cbor_item_t* message = cbor_new_definite_array(d.Size());
for (auto& m : d.GetArray()) {
cbor_item_t* item = rapidjson_to_cbor(m);
CHECK_EQ(1, cbor_refcount(item));
CHECK(cbor_array_push(message, cbor_move(item)));
}
return message;
}
if (d.IsObject()) {
cbor_item_t* message = cbor_new_definite_map(d.MemberCount());
for (auto& m : d.GetObject()) {
cbor_item_t* key = rapidjson_to_cbor(m.name);
cbor_item_t* value = rapidjson_to_cbor(m.value);
CHECK_EQ(1, cbor_refcount(key));
CHECK_EQ(1, cbor_refcount(value));
CHECK(cbor_map_add(message, {cbor_move(key), cbor_move(value)}));
}
return message;
}
if (d.IsBool()) {
return cbor_build_bool(d.GetBool());
}
if (d.IsFalse()) {
return cbor_build_bool(false);
}
if (d.IsUint()) {
return cbor_build_uint32(d.GetUint());
}
if (d.IsUint64()) {
return cbor_build_uint64(d.GetUint64());
}
if (d.IsNull()) {
return cbor_new_null();
}

ABORT() << "Unsupported message field";
return cbor_build_bool(false);
}

rapidjson::Value cbor_to_rapidjson(const cbor_item_t* item,
rapidjson::Document& document) {
switch (cbor_typeof(item)) {
case CBOR_TYPE_UINT: {
return rapidjson::Value(cbor_get_int(item));
}

case CBOR_TYPE_NEGINT: {
return rapidjson::Value(-1 - static_cast<int64_t>(cbor_get_int(item)));
}

case CBOR_TYPE_BYTESTRING: {
ABORT() << "CBOR byte strings are not supported";
rapidjson::Value null_value;
return null_value;
}

case CBOR_TYPE_STRING: {
if (cbor_string_is_definite(item)) {
rapidjson::Value result;
result.SetString(reinterpret_cast<char*>(cbor_string_handle(item)),
static_cast<int>(cbor_string_length(item)),
document.GetAllocator());
return result;
}

if (cbor_string_is_indefinite(item)) {
const size_t chunk_count = cbor_string_chunk_count(item);
cbor_item_t** chunk_handle = cbor_string_chunks_handle(item);

std::ostringstream result_stream;
for (size_t i = 0; i < chunk_count; i++) {
cbor_item_t* chunk = chunk_handle[i];
CHECK(cbor_string_is_definite(chunk));

result_stream << std::string{reinterpret_cast<char*>(cbor_string_handle(chunk)),
cbor_string_length(chunk)};
}
const std::string result = result_stream.str();

return rapidjson::Value(result.data(), static_cast<int>(result.size()),
document.GetAllocator());
}

ABORT() << "Unreachable statement for string";
rapidjson::Value null_value;
return null_value;
}

case CBOR_TYPE_ARRAY: {
rapidjson::Value array;
array = rapidjson::Value(rapidjson::kArrayType);
for (size_t i = 0; i < cbor_array_size(item); i++) {
array.PushBack(cbor_to_rapidjson(cbor_array_handle(item)[i], document),
document.GetAllocator());
}
return array;
}

case CBOR_TYPE_MAP: {
rapidjson::Value map;
map = rapidjson::Value(rapidjson::kObjectType);
for (size_t i = 0; i < cbor_map_size(item); i++) {
map.AddMember(cbor_to_rapidjson(cbor_map_handle(item)[i].key, document),
cbor_to_rapidjson(cbor_map_handle(item)[i].value, document),
document.GetAllocator());
}
return map;
}

case CBOR_TYPE_TAG: {
ABORT() << "CBOR tags are not supported";
rapidjson::Value null_value;
return null_value;
}

case CBOR_TYPE_FLOAT_CTRL: {
if (cbor_is_float(item)) {
return rapidjson::Value(cbor_float_get_float(item));
}
if (cbor_is_null(item)) {
rapidjson::Value null_value;
return null_value;
}
if (cbor_is_bool(item)) {
return rapidjson::Value(cbor_ctrl_is_bool(item));
}
ABORT() << "not supported float or control";
rapidjson::Value null_value;
return null_value;
}

default: {
ABORT() << "Not supported";
rapidjson::Value null_value;
return null_value;
}
}
}

cbor_item_t* json_to_cbor(const nlohmann::json& document) {
if (document.is_string()) {
return cbor_build_string(document.get<std::string>().c_str());
Expand Down
6 changes: 0 additions & 6 deletions src/cbor_json.h
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
#pragma once

#include <cbor.h>
#include <rapidjson/document.h>
#include <json.hpp>

namespace satori {
namespace video {

cbor_item_t* rapidjson_to_cbor(const rapidjson::Value& d);

rapidjson::Value cbor_to_rapidjson(const cbor_item_t* item,
rapidjson::Document& document);

cbor_item_t* json_to_cbor(const nlohmann::json& document);

nlohmann::json cbor_to_json(const cbor_item_t* item);
Expand Down
25 changes: 14 additions & 11 deletions src/replay_source.cpp
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
#include <rapidjson/document.h>
#include <cstring>
#include "video_streams.h"

#include <fstream>
#include <functional>
#include <gsl/gsl>
#include <iostream>
#include <memory>
#include <json.hpp>
#include <string>
#include <thread>

#include "cbor_json.h"
#include "cbor_tools.h"
#include "logging.h"
#include "streams/asio_streams.h"
#include "video_streams.h"

namespace satori {
namespace video {
Expand All @@ -35,10 +32,14 @@ struct read_json_impl {
return;
}
LOG(4) << "line=" << line;
rapidjson::Document data;
data.Parse<0>(line.c_str()).HasParseError();
CHECK(data.IsObject());
observer.on_next(cbor_move(rapidjson_to_cbor(data)));
nlohmann::json data;
try {
data = nlohmann::json::parse(line);
} catch (const std::exception &e) {
ABORT() << "Unable to parse line: " << e.what() << " " << line;
}
CHECK(data.is_object());
observer.on_next(cbor_move(json_to_cbor(data)));
}

const std::string _filename;
Expand All @@ -59,9 +60,11 @@ static streams::publisher<cbor_item_t *> get_messages(cbor_item_t *&&doc) {
auto decref = gsl::finally([&doc]() { cbor_decref(&doc); });

cbor_item_t *messages = cbor::map(doc).get("messages");
CHECK(cbor_isa_array(messages));
std::vector<cbor_item_t *> result;
auto array_handle = cbor_array_handle(messages);
for (size_t i = 0; i < cbor_array_size(messages); ++i) {
result.push_back(cbor_array_get(messages, i));
result.push_back(cbor_incref(array_handle[i]));
}
return streams::publishers::of(std::move(result));
}
Expand Down
Loading

0 comments on commit a98123c

Please sign in to comment.