Skip to content

Commit

Permalink
Provide MetadataIO interface to convert metadata to a string in memor…
Browse files Browse the repository at this point in the history
…y, alongside file IO versions (ros2#894)

Signed-off-by: Emerson Knapp <emerson.b.knapp@gmail.com>
  • Loading branch information
emersonknapp authored Oct 27, 2021
1 parent 23943c8 commit df9695c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
6 changes: 6 additions & 0 deletions rosbag2_storage/include/rosbag2_storage/metadata_io.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ class MetadataIo
ROSBAG2_STORAGE_PUBLIC
virtual bool metadata_file_exists(const std::string & uri);

ROSBAG2_STORAGE_PUBLIC
virtual std::string serialize_metadata(const BagMetadata & metadata);

ROSBAG2_STORAGE_PUBLIC
virtual BagMetadata deserialize_metadata(const std::string & serialized_metadata);

private:
std::string get_metadata_file_name(const std::string & uri);
};
Expand Down
14 changes: 14 additions & 0 deletions rosbag2_storage/src/rosbag2_storage/metadata_io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -284,4 +284,18 @@ bool MetadataIo::metadata_file_exists(const std::string & uri)
return rcpputils::fs::exists(rcpputils::fs::path(get_metadata_file_name(uri)));
}

std::string MetadataIo::serialize_metadata(const BagMetadata & metadata)
{
auto node = YAML::convert<BagMetadata>().encode(metadata);
std::stringstream out;
out << node;
return out.str();
}

BagMetadata MetadataIo::deserialize_metadata(const std::string & serialized_metadata)
{
YAML::Node yaml = YAML::Load(serialized_metadata);
return yaml.as<BagMetadata>();
}

} // namespace rosbag2_storage

0 comments on commit df9695c

Please sign in to comment.