24#ifndef GOBY_MIDDLEWARE_MARSHALLING_JSON_H
25#define GOBY_MIDDLEWARE_MARSHALLING_JSON_H
27#include <boost/type_index.hpp>
42 auto bson = nlohmann::json::to_bson(
msg);
43 std::vector<char> bytes(bson.begin(), bson.end());
47 static std::string
type_name(
const nlohmann::json& d = nlohmann::json())
49 return "nlohmann::json";
52 template <
typename CharIterator>
53 static std::shared_ptr<nlohmann::json>
parse(CharIterator bytes_begin, CharIterator bytes_end,
54 CharIterator& actual_end,
57 actual_end = bytes_end;
58 return std::make_shared<nlohmann::json>(nlohmann::json::from_bson(bytes_begin, bytes_end));
62template <
typename T,
class Enable =
void>
constexpr const char*
json_type_name()
64 return T::goby_json_type;
72 nlohmann::json j =
msg;
76 static std::string
type_name(
const T& t = T()) {
return json_type_name<T>(); }
78 template <
typename CharIterator>
79 static std::shared_ptr<T>
parse(CharIterator bytes_begin, CharIterator bytes_end,
80 CharIterator& actual_end,
const std::string& type =
type_name())
83 bytes_begin, bytes_end, actual_end, type);
85 return std::make_shared<T>(j->template get<T>());
90 typename std::enable_if<std::is_same<T, nlohmann::json>::value>::type* =
nullptr>
96template <typename T, typename std::enable_if<T::goby_json_type != nullptr>::type* =
nullptr>
constexpr int scheme()
Placeholder to provide an interface for the scheme() function family.
constexpr const char * json_type_name()
The global namespace for the Goby project.
extern ::PROTOBUF_NAMESPACE_ID::internal::ExtensionIdentifier< ::PROTOBUF_NAMESPACE_ID::MessageOptions, ::PROTOBUF_NAMESPACE_ID::internal::MessageTypeTraits< ::goby::GobyMessageOptions >, 11, false > msg
Enumeration and helper functions for marshalling scheme identification.
static std::string type_name(const T &t=T())
static std::vector< char > serialize(const T &msg)
static std::shared_ptr< T > parse(CharIterator bytes_begin, CharIterator bytes_end, CharIterator &actual_end, const std::string &type=type_name())
static std::vector< char > serialize(const nlohmann::json &msg)
static std::shared_ptr< nlohmann::json > parse(CharIterator bytes_begin, CharIterator bytes_end, CharIterator &actual_end, const std::string &type=type_name())
static std::string type_name(const nlohmann::json &d=nlohmann::json())
Class for parsing and serializing a given marshalling scheme. Must be specialized for a particular sc...
static std::string type_name()
The marshalling scheme specific string name for this type.
static std::vector< char > serialize(const DataType &)
Given data, produce a vector of bytes.
static std::shared_ptr< DataType > parse(CharIterator bytes_begin, CharIterator bytes_end, CharIterator &actual_end, const std::string &type=type_name())
Given a beginning and end iterator to bytes, parse the data and return it.