|
39 | 39 | #include <array> |
40 | 40 | #include <utility> |
41 | 41 | #include <variant> |
| 42 | + |
| 43 | +#include <nlohmann/json.hpp> |
| 44 | + |
42 | 45 | #include "MessageDefinition.h" |
43 | 46 | #include "utils.h" |
44 | 47 | #include "picosha2/picosha2.h" |
@@ -284,6 +287,38 @@ namespace mav { |
284 | 287 | return *this; |
285 | 288 | } |
286 | 289 |
|
| 290 | + Message& operator()(const nlohmann::json &j) { |
| 291 | + for (auto it = j.begin(); it != j.end(); ++it) { |
| 292 | + const std::string& key = it.key(); |
| 293 | + const auto& value = it.value(); |
| 294 | + |
| 295 | + if (value.is_string()) { |
| 296 | + setFromNativeTypeVariant(key, value.get<std::string>()); |
| 297 | + } |
| 298 | + else if (value.is_number_integer()) { |
| 299 | + setFromNativeTypeVariant(key, value.get<int>()); |
| 300 | + } |
| 301 | + else if (value.is_number_unsigned()) { |
| 302 | + setFromNativeTypeVariant(key, value.get<unsigned int>()); |
| 303 | + } |
| 304 | + else if (value.is_number_float()) { |
| 305 | + setFromNativeTypeVariant(key, value.get<float>()); |
| 306 | + } |
| 307 | + else if (value.is_boolean()) { |
| 308 | + setFromNativeTypeVariant(key, value.get<bool>()); |
| 309 | + } |
| 310 | + else if (value.is_array()) { |
| 311 | + // NOTE: choose actual expected vector element type |
| 312 | + setFromNativeTypeVariant(key, value.get<std::vector<float>>()); |
| 313 | + } |
| 314 | + else { |
| 315 | + std::cerr << "Unsupported JSON type: " << key << std::endl; |
| 316 | + } |
| 317 | + } |
| 318 | + return *this; |
| 319 | + } |
| 320 | + |
| 321 | + |
287 | 322 | template <typename T> |
288 | 323 | Message& set(const std::string &field_key, const T &v, int array_index = 0) { |
289 | 324 | auto field = _message_definition->fieldForName(field_key); |
|
0 commit comments