nlohmann_parsers.cpp
Go to the documentation of this file.
1 /*
2  * This Source Code Form is subject to the terms of the Mozilla Public
3  * License, v. 2.0. If a copy of the MPL was not distributed with this
4  * file, You can obtain one at https://mozilla.org/MPL/2.0/.
5  */
6 
7 #include "nlohmann_parsers.h"
8 
10 
12 {
13  if (_use_message_stamp && _stamp_fieldname.empty() == false)
14  {
15  auto ts = _json.find(_stamp_fieldname);
16  if (ts != _json.end() && ts.value().is_number())
17  {
18  timestamp = ts.value().get<double>();
19  }
20  else
21  {
22  _use_message_stamp = false;
23  }
24  }
25 
26  std::function<void(const std::string&, const nlohmann::json&)> flatten;
27 
28  flatten = [&](const std::string& prefix, const nlohmann::json& value) {
29  if (value.empty())
30  {
31  return;
32  }
33 
34  switch (value.type())
35  {
36  case nlohmann::detail::value_t::array: {
37  // iterate array and use index as reference string
38  for (std::size_t i = 0; i < value.size(); ++i)
39  {
40  flatten(fmt::format("{}[{}]", prefix, i), value[i]);
41  }
42  break;
43  }
44 
45  case nlohmann::detail::value_t::object: {
46  // iterate object and use keys as reference string
47  for (const auto& element : value.items())
48  {
49  flatten(fmt::format("{}/{}", prefix, element.key()), element.value());
50  }
51  break;
52  }
53 
54  default: {
55  double numeric_value = 0;
56  if (value.is_boolean())
57  {
58  numeric_value = value.get<bool>();
59  }
60  else if (value.is_number())
61  {
62  numeric_value = value.get<double>();
63  }
64  else
65  {
66  return;
67  }
68 
69  auto plot_data = &(getSeries(prefix));
70  plot_data->pushBack({ timestamp, numeric_value });
71 
72  break;
73  }
74  } // end switch
75  };
76 
77  flatten(_topic_name, _json);
78  return true;
79 }
80 
82 {
83  _json = nlohmann::json::from_msgpack(msg.data(), msg.data() + msg.size());
85 }
86 
88 {
89  _json = nlohmann::json::parse(msg.data(), msg.data() + msg.size());
91 }
92 
94 {
95  _json = nlohmann::json::from_cbor(msg.data(), msg.data() + msg.size());
97 }
98 
100 {
101  _json = nlohmann::json::from_bson(msg.data(), msg.data() + msg.size());
102  return parseMessageImpl(timestamp);
103 }
PJ::MessageParser::_topic_name
std::string _topic_name
Definition: messageparser_base.h:118
MessagePack_Parser::parseMessage
bool parseMessage(const MessageRef msg, double &timestamp) override
Definition: nlohmann_parsers.cpp:81
nonstd::span_lite::size_t
span_CONFIG_SIZE_TYPE size_t
Definition: span.hpp:576
BSON_Parser::parseMessage
bool parseMessage(const MessageRef msg, double &timestamp) override
Definition: nlohmann_parsers.cpp:99
NlohmannParser::_json
nlohmann::json _json
Definition: nlohmann_parsers.h:31
JSON_Parser::parseMessage
bool parseMessage(const MessageRef msg, double &timestamp) override
Definition: nlohmann_parsers.cpp:87
format.h
mqtt_test_proto.msg
msg
Definition: mqtt_test_proto.py:43
start_test_publisher.timestamp
timestamp
Definition: start_test_publisher.py:32
PJ::MessageParser::getSeries
PlotData & getSeries(const std::string &key)
Definition: messageparser_base.h:120
NlohmannParser::parseMessageImpl
bool parseMessageImpl(double &timestamp)
Definition: nlohmann_parsers.cpp:11
format
auto format(const text_style &ts, const S &format_str, const Args &... args) -> std::basic_string< Char >
Definition: color.h:543
NlohmannParser::_use_message_stamp
bool _use_message_stamp
Definition: nlohmann_parsers.h:32
nlohmann_parsers.h
PJ::MessageRef
Definition: messageparser_base.h:28
CBOR_Parser::parseMessage
bool parseMessage(const MessageRef msg, double &timestamp) override
Definition: nlohmann_parsers.cpp:93
NlohmannParser::_stamp_fieldname
std::string _stamp_fieldname
Definition: nlohmann_parsers.h:33


plotjuggler
Author(s): Davide Faconti
autogenerated on Mon May 26 2025 02:22:37