Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pymem json #828

Merged
merged 4 commits into from
Aug 18, 2024
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
15 changes: 2 additions & 13 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,22 +53,11 @@ jobs:
if: runner.os == 'Linux'
run: |
sudo apt update
sudo apt install libeigen3-dev qtdeclarative5-dev qt5-qmake libqglviewer-dev-qt5 libsuitesparse-dev lcov libspdlog-dev

- name: Install cereal on Ubuntu
if: runner.os == 'Linux'
run: |
cd /tmp
wget https://github.com/USCiLab/cereal/archive/refs/tags/v1.3.2.tar.gz
tar xaf v1.3.2.tar.gz
cd cereal-1.3.2
mkdir build; cd build
cmake -DJUST_INSTALL_CEREAL:=On ..
sudo make install
sudo apt install libeigen3-dev qtdeclarative5-dev qt5-qmake libqglviewer-dev-qt5 libsuitesparse-dev lcov libspdlog-dev nlohmann-json3-dev

- name: Install dependencies on macOS
if: runner.os == 'macOS'
run: brew install cmake eigen coreutils
run: brew install cmake eigen coreutils nlohmann-json

- name: Configure ccache for macOS
if: runner.os == 'macOS'
Expand Down
8 changes: 4 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -421,16 +421,16 @@ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${g2o_CXX_FLAGS}")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${g2o_C_FLAGS}")

find_package(Eigen3 3.3 REQUIRED)
find_package(cereal)
find_package(nlohmann_json 3.2.0)

# Generate config.h
set(G2O_OPENGL_FOUND ${OPENGL_FOUND})
set(G2O_HAVE_CHOLMOD ${CHOLMOD_FOUND})
set(G2O_HAVE_CSPARSE ${G2O_USE_CSPARSE})
if (TARGET cereal::cereal)
set(G2O_HAVE_CEREAL 1)
if (TARGET nlohmann_json::nlohmann_json)
set(G2O_HAVE_JSON 1)
else()
set(G2O_HAVE_CEREAL 0)
set(G2O_HAVE_JSON 0)
endif()
set(G2O_SHARED_LIBS ${BUILD_SHARED_LIBS})
set(G2O_LGPL_SHARED_LIBS ${BUILD_LGPL_SHARED_LIBS})
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,9 @@ following packages.

- spdlog <https://github.com/gabime/spdlog>
- suitesparse <http://faculty.cse.tamu.edu/davis/suitesparse.html>
- cereal <https://uscilab.github.io/cereal/index.html>
- Qt5 <http://qt-project.org>
- libQGLViewer <http://www.libqglviewer.com>
- JSON for Modern C++ <https://github.com/nlohmann/json>

On Ubuntu / Debian these dependencies are resolved by installing the
following packages.
Expand Down
2 changes: 1 addition & 1 deletion config.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#cmakedefine G2O_SHARED_LIBS 1
#cmakedefine G2O_LGPL_SHARED_LIBS 1

#cmakedefine G2O_HAVE_CEREAL 1
#cmakedefine G2O_HAVE_JSON 1

// available sparse matrix libraries
#cmakedefine G2O_HAVE_CHOLMOD 1
Expand Down
5 changes: 2 additions & 3 deletions g2o/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ io/io_format.cpp
io/io_binary.cpp io/io_binary.h
io/io_json.cpp io/io_json.h
io/io_g2o.cpp io/io_g2o.h
io/io_xml.cpp io/io_xml.h
)

target_include_directories(core PUBLIC
Expand All @@ -58,8 +57,8 @@ set_target_properties(core PROPERTIES
SOVERSION ${G2O_LIB_SOVERSION})
target_link_libraries(core PUBLIC stuff Eigen3::Eigen)
target_link_libraries(core PUBLIC g2o_ceres_ad)
if (TARGET cereal::cereal)
target_link_libraries(core PUBLIC cereal::cereal)
if (TARGET nlohmann_json::nlohmann_json)
target_link_libraries(core PRIVATE nlohmann_json::nlohmann_json)
endif()
target_compile_features(core PUBLIC cxx_std_17)

Expand Down
3 changes: 0 additions & 3 deletions g2o/core/abstract_graph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
#include "g2o/core/io/io_binary.h"
#include "g2o/core/io/io_format.h"
#include "g2o/core/io/io_json.h"
#include "g2o/core/io/io_xml.h"
#include "g2o/stuff/logger.h"
#include "io/io_g2o.h"

Expand All @@ -54,8 +53,6 @@ std::unique_ptr<g2o::IoInterface> allocate(g2o::io::Format format) {
return std::make_unique<g2o::IoBinary>();
case g2o::io::Format::kJson:
return std::make_unique<g2o::IoJson>();
case g2o::io::Format::kXML:
return std::make_unique<g2o::IoXml>();
}
G2O_CRITICAL("Failed to create graph IO interface for format {}",
g2o::io::to_string(format));
Expand Down
31 changes: 20 additions & 11 deletions g2o/core/io/io_binary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,26 +30,35 @@

#include "g2o/config.h"
#include "g2o/core/abstract_graph.h"

#ifdef G2O_HAVE_CEREAL
#include <cereal/archives/portable_binary.hpp>
#include <cereal/cereal.hpp>

#include "io_wrapper_cereal.h" // IWYU pragma: keep
#else
#include "g2o/stuff/logger.h"
#endif // HAVE CEREAL

#ifdef G2O_HAVE_JSON
#include "io_wrapper_json.h"
#endif // HAVE_JSON

namespace g2o {

#ifdef G2O_HAVE_CEREAL
#ifdef G2O_HAVE_JSON

std::optional<AbstractGraph> IoBinary::load(std::istream& input) {
return io::load<cereal::PortableBinaryInputArchive>(input, "BINARY");
try {
return json::fromJson(nlohmann::json::from_cbor(input));
} catch (const std::exception& e) {
G2O_ERROR("Exception while loading: {}", e.what());

Check warning on line 47 in g2o/core/io/io_binary.cpp

View check run for this annotation

Codecov / codecov/patch

g2o/core/io/io_binary.cpp#L46-L47

Added lines #L46 - L47 were not covered by tests
}
return std::nullopt;

Check warning on line 49 in g2o/core/io/io_binary.cpp

View check run for this annotation

Codecov / codecov/patch

g2o/core/io/io_binary.cpp#L49

Added line #L49 was not covered by tests
}

bool IoBinary::save(std::ostream& output, const AbstractGraph& graph) {
return io::save<cereal::PortableBinaryOutputArchive>(output, graph, "BINARY");
try {
std::vector<std::uint8_t> binary =
nlohmann::json::to_cbor(json::toJson(graph));
output.write(reinterpret_cast<char*>(binary.data()), binary.size());
return output.good();
} catch (const std::exception& e) {
G2O_ERROR("Exception while saving: {}", e.what());

Check warning on line 59 in g2o/core/io/io_binary.cpp

View check run for this annotation

Codecov / codecov/patch

g2o/core/io/io_binary.cpp#L59

Added line #L59 was not covered by tests
}
return false;

Check warning on line 61 in g2o/core/io/io_binary.cpp

View check run for this annotation

Codecov / codecov/patch

g2o/core/io/io_binary.cpp#L61

Added line #L61 was not covered by tests
}

#else
Expand Down
4 changes: 0 additions & 4 deletions g2o/core/io/io_format.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,13 @@ std::string_view to_string(g2o::io::Format format) {
return "Binary";
case g2o::io::Format::kJson:
return "JSON";
case g2o::io::Format::kXML:
return "XML";
}
return "";
}

std::optional<Format> formatForFileExtension(std::string_view extension) {
if (extension == "g2o" || extension == "G2O") return Format::kG2O;
if (extension == "json" || extension == "JSON") return Format::kJson;
if (extension == "xml" || extension == "XML") return Format::kXML;
if (extension == "bin" || extension == "BIN") return Format::kBinary;
return std::nullopt;
}
Expand All @@ -66,7 +63,6 @@ std::vector<FileFilter> getFileFilter(bool open) {
std::vector<FileFilter> result;
result.emplace_back("g2o Ascii files (*.g2o)", Format::kG2O);
result.emplace_back("g2o Json files (*.json)", Format::kJson);
result.emplace_back("g2o XML files (*.xml)", Format::kXML);
result.emplace_back("g2o BIN files (*.bin)", Format::kBinary);
if (open) {
result.emplace_back("All Files (*)", Format::kUndefined);
Expand Down
1 change: 0 additions & 1 deletion g2o/core/io/io_format.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ enum class G2O_CORE_API Format {
kG2O = 0,
kBinary = 1,
kJson = 2,
kXML = 3
};

G2O_CORE_API std::string_view to_string(g2o::io::Format format);
Expand Down
32 changes: 22 additions & 10 deletions g2o/core/io/io_json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,30 +26,42 @@

#include "io_json.h"

#include <exception>
#include <optional>

#include "g2o/config.h"
#include "g2o/core/abstract_graph.h"
#include "g2o/stuff/logger.h"

#ifdef G2O_HAVE_CEREAL
#include <cereal/archives/json.hpp>
#include <cereal/cereal.hpp>
#ifdef G2O_HAVE_JSON
#include <nlohmann/json.hpp>

#include "io_wrapper_cereal.h" // IWYU pragma: keep
#else
#include "g2o/stuff/logger.h"
#endif // HAVE CEREAL
#include "io_wrapper_json.h"
#endif // HAVE_JSON

namespace g2o {

#ifdef G2O_HAVE_CEREAL
#ifdef G2O_HAVE_JSON

std::optional<AbstractGraph> IoJson::load(std::istream& input) {
return io::load<cereal::JSONInputArchive>(input, "JSON");
try {
nlohmann::json json;
input >> json;
return json::fromJson(json);
} catch (const std::exception& e) {
G2O_ERROR("Exception while saving: {}", e.what());

Check warning on line 52 in g2o/core/io/io_json.cpp

View check run for this annotation

Codecov / codecov/patch

g2o/core/io/io_json.cpp#L52

Added line #L52 was not covered by tests
}
return std::nullopt;

Check warning on line 54 in g2o/core/io/io_json.cpp

View check run for this annotation

Codecov / codecov/patch

g2o/core/io/io_json.cpp#L54

Added line #L54 was not covered by tests
}

bool IoJson::save(std::ostream& output, const AbstractGraph& graph) {
return io::save<cereal::JSONOutputArchive>(output, graph, "JSON");
try {
output << json::toJson(graph);
} catch (const std::exception& e) {
G2O_ERROR("Exception while saving: {}", e.what());
return false;

Check warning on line 62 in g2o/core/io/io_json.cpp

View check run for this annotation

Codecov / codecov/patch

g2o/core/io/io_json.cpp#L60-L62

Added lines #L60 - L62 were not covered by tests
}
return true;
}

#else
Expand Down
Loading
Loading