Skip to content

Commit

Permalink
Store optional container only if not empty
Browse files Browse the repository at this point in the history
Store vertex / edge data and edge param_ids only if not empty. Reading
the JSON treads those elements as optional.

This reduces the data size a bit and focuses on required information.
  • Loading branch information
RainerKuemmerle committed Aug 24, 2024
1 parent c8ca3e2 commit 7ba323e
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 5 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ 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(nlohmann_json 3.2.0)
find_package(nlohmann_json 3.3.0)

# Generate config.h
set(G2O_OPENGL_FOUND ${OPENGL_FOUND})
Expand Down
2 changes: 2 additions & 0 deletions g2o/core/io/io_json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "io_json.h"

#include <exception>
#include <iomanip>
#include <optional>

#include "g2o/config.h"
Expand Down Expand Up @@ -56,6 +57,7 @@ std::optional<AbstractGraph> IoJson::load(std::istream& input) {

bool IoJson::save(std::ostream& output, const AbstractGraph& graph) {
try {
output << std::setw(2);
output << json::toJson(graph);
} catch (const std::exception& e) {
G2O_ERROR("Exception while saving: {}", e.what());
Expand Down
71 changes: 67 additions & 4 deletions g2o/core/io/io_wrapper_json.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,77 @@
#include "g2o/core/abstract_graph.h"

namespace g2o {
namespace internal {
/**
* @brief Get the object from the JSON if key exists
*
* @tparam T type of the target value
* @param j a JSON
* @param key key for retrieving the value
* @param target Where to store the value
*/
template <typename T>
void get_to_if_exists(const nlohmann::json& j, const char* key, T& target) {
auto it = j.find(key);
if (it == j.end()) return;
it->get_to(target);
}

/**
* @brief Store a container into the JSON if not empty
*
* @tparam T type of the container
* @param j a JSON
* @param key key for storing the container
* @param value the container to store
*/
template <typename T>
void store_if_not_empty(nlohmann::json& j, const char* key, const T& value) {
if (value.empty()) return;
j[key] = value;
}
} // namespace internal

NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(AbstractGraph::AbstractParameter, tag, id,
value);
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(AbstractGraph::AbstractData, tag, data);
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(AbstractGraph::AbstractVertex, tag, id,
estimate, data);
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(AbstractGraph::AbstractEdge, tag, ids,
param_ids, measurement, information, data);

// VERTEX
inline void to_json(nlohmann::json& j,
const AbstractGraph::AbstractVertex& vertex) {
j = nlohmann::json{
{"tag", vertex.tag}, {"id", vertex.id}, {"estimate", vertex.estimate}};
internal::store_if_not_empty(j, "data", vertex.data);
}

inline void from_json(const nlohmann::json& j,
AbstractGraph::AbstractVertex& vertex) {
j.at("tag").get_to(vertex.tag);
j.at("id").get_to(vertex.id);
j.at("estimate").get_to(vertex.estimate);
internal::get_to_if_exists(j, "data", vertex.data);
}

// EDGE
inline void to_json(nlohmann::json& j,
const AbstractGraph::AbstractEdge& edge) {
j = nlohmann::json{{"tag", edge.tag},
{"ids", edge.ids},
{"measurement", edge.measurement},
{"information", edge.information}};
internal::store_if_not_empty(j, "data", edge.data);
internal::store_if_not_empty(j, "param_ids", edge.param_ids);
}

inline void from_json(const nlohmann::json& j,
AbstractGraph::AbstractEdge& edge) {
j.at("tag").get_to(edge.tag);
j.at("ids").get_to(edge.ids);
j.at("measurement").get_to(edge.measurement);
j.at("information").get_to(edge.information);
internal::get_to_if_exists(j, "data", edge.data);
internal::get_to_if_exists(j, "param_ids", edge.param_ids);
}

namespace json {

Expand Down

0 comments on commit 7ba323e

Please sign in to comment.