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 
11 bool NlohmannParser::parseMessageImpl(double& timestamp)
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  {
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 
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 
81 bool MessagePack_Parser::parseMessage(const MessageRef msg, double& timestamp)
82 {
83  _json = nlohmann::json::from_msgpack(msg.data(), msg.data() + msg.size());
84  return parseMessageImpl(timestamp);
85 }
86 
87 bool JSON_Parser::parseMessage(const MessageRef msg, double& timestamp)
88 {
89  _json = nlohmann::json::parse(msg.data(), msg.data() + msg.size());
90  return parseMessageImpl(timestamp);
91 }
92 
93 bool CBOR_Parser::parseMessage(const MessageRef msg, double& timestamp)
94 {
95  _json = nlohmann::json::from_cbor(msg.data(), msg.data() + msg.size());
96  return parseMessageImpl(timestamp);
97 }
98 
99 bool BSON_Parser::parseMessage(const MessageRef msg, double& timestamp)
100 {
101  _json = nlohmann::json::from_bson(msg.data(), msg.data() + msg.size());
102  return parseMessageImpl(timestamp);
103 }
nlohmann::basic_json::from_bson
static JSON_HEDLEY_WARN_UNUSED_RESULT basic_json from_bson(InputType &&i, const bool strict=true, const bool allow_exceptions=true)
create a JSON value from an input in BSON format
Definition: json.hpp:21275
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
nlohmann::detail::value_t::object
@ object
object (unordered set of name/value pairs)
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
nlohmann::basic_json::parse
static JSON_HEDLEY_WARN_UNUSED_RESULT basic_json parse(InputType &&i, const parser_callback_t cb=nullptr, const bool allow_exceptions=true, const bool ignore_comments=false)
deserialize from a compatible input
Definition: json.hpp:20814
nlohmann::basic_json::from_msgpack
static JSON_HEDLEY_WARN_UNUSED_RESULT basic_json from_msgpack(InputType &&i, const bool strict=true, const bool allow_exceptions=true)
create a JSON value from an input in MessagePack format
Definition: json.hpp:21167
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
nlohmann::detail::void
j template void())
Definition: json.hpp:4061
nlohmann::detail::value_t::array
@ array
array (ordered collection of values)
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::basic_json::find
iterator find(KeyT &&key)
find an element in a JSON object
Definition: json.hpp:19600
nlohmann::basic_json
a class to store JSON values
Definition: json.hpp:3326
nlohmann_parsers.h
PJ::MessageRef
Definition: messageparser_base.h:28
nlohmann::basic_json::from_cbor
static JSON_HEDLEY_WARN_UNUSED_RESULT basic_json from_cbor(InputType &&i, const bool strict=true, const bool allow_exceptions=true, const cbor_tag_handler_t tag_handler=cbor_tag_handler_t::error)
create a JSON value from an input in CBOR format
Definition: json.hpp:21108
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
nlohmann::detail::iter_impl::value
reference value() const
return the value of an iterator
Definition: json.hpp:12094
nlohmann::basic_json::end
iterator end() noexcept
returns an iterator to one past the last element
Definition: json.hpp:19689


plotjuggler
Author(s): Davide Faconti
autogenerated on Sun Aug 11 2024 02:24:23