serialization.cpp
Go to the documentation of this file.
3 
4 namespace foxglove {
5 
6 void to_json(nlohmann::json& j, const Channel& c) {
7  j = {
8  {"id", c.id},
9  {"topic", c.topic},
10  {"encoding", c.encoding},
11  {"schemaName", c.schemaName},
12  {"schema", c.schema},
13  };
14 
15  if (c.schemaEncoding.has_value()) {
16  j["schemaEncoding"] = c.schemaEncoding.value();
17  }
18 }
19 void from_json(const nlohmann::json& j, Channel& c) {
20  const auto schemaEncoding =
21  j.find("schemaEncoding") == j.end()
22  ? std::optional<std::string>(std::nullopt)
23  : std::optional<std::string>(j["schemaEncoding"].get<std::string>());
24 
25  ChannelWithoutId channelWithoutId{j["topic"].get<std::string>(), j["encoding"].get<std::string>(),
26  j["schemaName"].get<std::string>(),
27  j["schema"].get<std::string>(), schemaEncoding};
28  c = Channel(j["id"].get<ChannelId>(), channelWithoutId);
29 }
30 
32  const auto paramType = p.getType();
33  if (paramType == ParameterType::PARAMETER_BOOL) {
34  j = p.getValue<bool>();
35  } else if (paramType == ParameterType::PARAMETER_INTEGER) {
36  j = p.getValue<int64_t>();
37  } else if (paramType == ParameterType::PARAMETER_DOUBLE) {
38  j = p.getValue<double>();
39  } else if (paramType == ParameterType::PARAMETER_STRING) {
40  j = p.getValue<std::string>();
41  } else if (paramType == ParameterType::PARAMETER_BYTE_ARRAY) {
42  const auto& paramValue = p.getValue<std::vector<unsigned char>>();
43  const std::string_view strValue(reinterpret_cast<const char*>(paramValue.data()),
44  paramValue.size());
45  j = base64Encode(strValue);
46  } else if (paramType == ParameterType::PARAMETER_STRUCT) {
47  j = p.getValue<std::unordered_map<std::string, ParameterValue>>();
48  } else if (paramType == ParameterType::PARAMETER_ARRAY) {
49  j = p.getValue<std::vector<ParameterValue>>();
50  } else if (paramType == ParameterType::PARAMETER_NOT_SET) {
51  // empty value.
52  }
53 }
54 
56  const auto jsonType = j.type();
57 
58  if (jsonType == nlohmann::detail::value_t::string) {
59  p = ParameterValue(j.get<std::string>());
60  } else if (jsonType == nlohmann::detail::value_t::boolean) {
61  p = ParameterValue(j.get<bool>());
62  } else if (jsonType == nlohmann::detail::value_t::number_integer) {
63  p = ParameterValue(j.get<int64_t>());
64  } else if (jsonType == nlohmann::detail::value_t::number_unsigned) {
65  p = ParameterValue(j.get<int64_t>());
66  } else if (jsonType == nlohmann::detail::value_t::number_float) {
67  p = ParameterValue(j.get<double>());
68  } else if (jsonType == nlohmann::detail::value_t::object) {
69  p = ParameterValue(j.get<std::unordered_map<std::string, ParameterValue>>());
70  } else if (jsonType == nlohmann::detail::value_t::array) {
71  p = ParameterValue(j.get<std::vector<ParameterValue>>());
72  }
73 }
74 
75 void to_json(nlohmann::json& j, const Parameter& p) {
76  to_json(j["value"], p.getValue());
77  j["name"] = p.getName();
79  j["type"] = "byte_array";
80  }
81 }
82 
83 void from_json(const nlohmann::json& j, Parameter& p) {
84  const auto name = j["name"].get<std::string>();
85 
86  if (j.find("value") == j.end()) {
87  p = Parameter(name); // Value is not set (undefined).
88  return;
89  }
90 
91  ParameterValue pValue;
92  from_json(j["value"], pValue);
93 
94  if (j.find("type") != j.end() && j["type"] == "byte_array" &&
96  p = Parameter(name, base64Decode(pValue.getValue<std::string>()));
97  } else {
98  p = Parameter(name, pValue);
99  }
100 }
101 
102 void to_json(nlohmann::json& j, const Service& service) {
103  j = {
104  {"id", service.id},
105  {"name", service.name},
106  {"type", service.type},
107  {"requestSchema", service.requestSchema},
108  {"responseSchema", service.responseSchema},
109  };
110 }
111 
112 void from_json(const nlohmann::json& j, Service& p) {
113  p.id = j["id"].get<ServiceId>();
114  p.name = j["name"].get<std::string>();
115  p.type = j["type"].get<std::string>();
116  p.requestSchema = j["requestSchema"].get<std::string>();
117  p.responseSchema = j["responseSchema"].get<std::string>();
118 }
119 
120 void ServiceResponse::read(const uint8_t* data, size_t dataLength) {
121  size_t offset = 0;
122  this->serviceId = ReadUint32LE(data + offset);
123  offset += 4;
124  this->callId = ReadUint32LE(data + offset);
125  offset += 4;
126  const size_t encondingLength = static_cast<size_t>(ReadUint32LE(data + offset));
127  offset += 4;
128  this->encoding = std::string(reinterpret_cast<const char*>(data + offset), encondingLength);
129  offset += encondingLength;
130  const auto payloadLength = dataLength - offset;
131  this->data.resize(payloadLength);
132  std::memcpy(this->data.data(), data + offset, payloadLength);
133 }
134 
135 void ServiceResponse::write(uint8_t* data) const {
136  size_t offset = 0;
137  foxglove::WriteUint32LE(data + offset, this->serviceId);
138  offset += 4;
139  foxglove::WriteUint32LE(data + offset, this->callId);
140  offset += 4;
141  foxglove::WriteUint32LE(data + offset, static_cast<uint32_t>(this->encoding.size()));
142  offset += 4;
143  std::memcpy(data + offset, this->encoding.data(), this->encoding.size());
144  offset += this->encoding.size();
145  std::memcpy(data + offset, this->data.data(), this->data.size());
146 }
147 
148 } // namespace foxglove
const ParameterValue & getValue() const
Definition: parameter.hpp:68
std::optional< std::string > schemaEncoding
Definition: common.hpp:54
ParameterType getType() const
Definition: parameter.hpp:40
uint32_t ServiceId
Definition: common.hpp:28
void write(uint8_t *data) const
void read(const uint8_t *data, size_t size)
void to_json(nlohmann::json &j, const Channel &c)
nlohmann::json json
std::string responseSchema
Definition: common.hpp:116
std::vector< uint8_t > data
Definition: common.hpp:132
void from_json(const nlohmann::json &j, Channel &c)
ChannelId id
Definition: common.hpp:63
ServiceId id
Definition: common.hpp:120
const std::string & getName() const
Definition: parameter.hpp:60
std::string schemaName
Definition: common.hpp:52
uint32_t ReadUint32LE(const uint8_t *buf)
std::string base64Encode(const std::string_view &input)
Definition: base64.cpp:10
std::string requestSchema
Definition: common.hpp:115
const T & getValue() const
Definition: parameter.hpp:45
ParameterType getType() const
Definition: parameter.hpp:64
std::vector< unsigned char > base64Decode(const std::string &input)
Definition: base64.cpp:47
void WriteUint32LE(uint8_t *buf, uint32_t val)


foxglove_bridge
Author(s): Foxglove
autogenerated on Mon Jul 3 2023 02:12:22