Goby3  3.1.4
2024.02.22
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 
33 namespace goby
34 {
35 namespace middleware
36 {
38 template <> struct SerializerParserHelper<nlohmann::json, MarshallingScheme::JSON>
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 
62 template <typename T, class Enable = void> constexpr const char* json_type_name()
63 {
64  return T::goby_json_type;
65 }
66 
68 template <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 
89 template <typename T,
90  typename std::enable_if<std::is_same<T, nlohmann::json>::value>::type* = nullptr>
91 constexpr int scheme()
92 {
94 }
95 
97 constexpr int scheme()
98 {
100 }
101 
102 } // namespace middleware
103 } // namespace goby
104 
105 #endif
goby::middleware::MarshallingScheme
Enumeration and helper functions for marshalling scheme identification.
Definition: interface.h:45
goby::middleware::SerializerParserHelper< T, MarshallingScheme::JSON >::type_name
static std::string type_name(const T &t=T())
Definition: json.h:76
goby::middleware::SerializerParserHelper::parse
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
interface.h
goby::middleware::SerializerParserHelper< T, MarshallingScheme::JSON >::parse
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
goby
The global namespace for the Goby project.
Definition: acomms_constants.h:33
goby::middleware::SerializerParserHelper
Class for parsing and serializing a given marshalling scheme. Must be specialized for a particular sc...
Definition: interface.h:97
goby::middleware::json_type_name
constexpr const char * json_type_name()
Definition: json.h:62
goby::middleware::SerializerParserHelper< nlohmann::json, MarshallingScheme::JSON >::serialize
static std::vector< char > serialize(const nlohmann::json &msg)
Definition: json.h:40
goby::middleware::SerializerParserHelper< nlohmann::json, MarshallingScheme::JSON >::type_name
static std::string type_name(const nlohmann::json &d=nlohmann::json())
Definition: json.h:47
goby::middleware::SerializerParserHelper::serialize
static std::vector< char > serialize(const DataType &)
Given data, produce a vector of bytes.
Definition: interface.h:100
basic_json
namespace for Niels Lohmann
Definition: json.hpp:3393
goby::middleware::SerializerParserHelper::type_name
static std::string type_name()
The marshalling scheme specific string name for this type.
Definition: interface.h:107
jwt::json::type
type
Generic JSON types used in JWTs.
Definition: jwt.h:2071
goby::msg
extern ::google::protobuf::internal::ExtensionIdentifier< ::google::protobuf::MessageOptions, ::google::protobuf::internal::MessageTypeTraits< ::goby::GobyMessageOptions >, 11, false > msg
Definition: option_extensions.pb.h:1327
json.hpp
goby::middleware::SerializerParserHelper< T, MarshallingScheme::JSON >::serialize
static std::vector< char > serialize(const T &msg)
Definition: json.h:70
goby::middleware::MarshallingScheme::JSON
@ JSON
Definition: interface.h:59
json
basic_json<> json
default specialization
Definition: json.hpp:3404
goby::middleware::scheme
constexpr int scheme()
Placeholder to provide an interface for the scheme() function family.
Definition: cstr.h:65
goby::middleware::SerializerParserHelper< nlohmann::json, MarshallingScheme::JSON >::parse
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