Goby3 3.2.3
2025.05.13
Loading...
Searching...
No Matches
json.h
Go to the documentation of this file.
1// Copyright 2021:
2// GobySoft, LLC (2013-)
3// Community contributors (see AUTHORS file)
4// File authors:
5// Toby Schneider <toby@gobysoft.org>
6//
7//
8// This file is part of the Goby Underwater Autonomy Project Libraries
9// ("The Goby Libraries").
10//
11// The Goby Libraries are free software: you can redistribute them and/or modify
12// them under the terms of the GNU Lesser General Public License as published by
13// the Free Software Foundation, either version 2.1 of the License, or
14// (at your option) any later version.
15//
16// The Goby Libraries are distributed in the hope that they will be useful,
17// but WITHOUT ANY WARRANTY; without even the implied warranty of
18// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19// GNU Lesser General Public License for more details.
20//
21// You should have received a copy of the GNU Lesser General Public License
22// along with Goby. If not, see <http://www.gnu.org/licenses/>.
23
24#ifndef GOBY_MIDDLEWARE_MARSHALLING_JSON_H
25#define GOBY_MIDDLEWARE_MARSHALLING_JSON_H
26
27#include <boost/type_index.hpp>
28
30
31#include "interface.h"
32
33namespace goby
34{
35namespace middleware
36{
39{
40 static std::vector<char> serialize(const nlohmann::json& msg)
41 {
42 auto bson = nlohmann::json::to_bson(msg);
43 std::vector<char> bytes(bson.begin(), bson.end());
44 return bytes;
45 }
46
47 static std::string type_name(const nlohmann::json& d = nlohmann::json())
48 {
49 return "nlohmann::json";
50 }
51
52 template <typename CharIterator>
53 static std::shared_ptr<nlohmann::json> parse(CharIterator bytes_begin, CharIterator bytes_end,
54 CharIterator& actual_end,
55 const std::string& type = type_name())
56 {
57 actual_end = bytes_end;
58 return std::make_shared<nlohmann::json>(nlohmann::json::from_bson(bytes_begin, bytes_end));
59 }
60};
61
62template <typename T, class Enable = void> constexpr const char* json_type_name()
63{
64 return T::goby_json_type;
65}
66
68template <typename T> struct SerializerParserHelper<T, MarshallingScheme::JSON>
69{
70 static std::vector<char> serialize(const T& msg)
71 {
72 nlohmann::json j = msg;
74 }
75
76 static std::string type_name(const T& t = T()) { return json_type_name<T>(); }
77
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())
81 {
83 bytes_begin, bytes_end, actual_end, type);
84
85 return std::make_shared<T>(j->template get<T>());
86 }
87};
88
89template <typename T,
90 typename std::enable_if<std::is_same<T, nlohmann::json>::value>::type* = nullptr>
91constexpr int scheme()
92{
94}
95
96template <typename T, typename std::enable_if<T::goby_json_type != nullptr>::type* = nullptr>
97constexpr int scheme()
98{
100}
101
102} // namespace middleware
103} // namespace goby
104
105#endif
nlohmann::json json
constexpr int scheme()
Placeholder to provide an interface for the scheme() function family.
Definition cstr.h:65
constexpr const char * json_type_name()
Definition json.h:62
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.
Definition interface.h:46
static std::vector< char > serialize(const T &msg)
Definition json.h:70
static std::shared_ptr< T > parse(CharIterator bytes_begin, CharIterator bytes_end, CharIterator &actual_end, const std::string &type=type_name())
Definition json.h:79
static std::vector< char > serialize(const nlohmann::json &msg)
Definition json.h:40
static std::shared_ptr< nlohmann::json > parse(CharIterator bytes_begin, CharIterator bytes_end, CharIterator &actual_end, const std::string &type=type_name())
Definition json.h:53
static std::string type_name(const nlohmann::json &d=nlohmann::json())
Definition json.h:47
Class for parsing and serializing a given marshalling scheme. Must be specialized for a particular sc...
Definition interface.h:98
static std::string type_name()
The marshalling scheme specific string name for this type.
Definition interface.h:107
static std::vector< char > serialize(const DataType &)
Given data, produce a vector of bytes.
Definition interface.h:100
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.
Definition interface.h:129