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 }
bool parseMessageImpl(double &timestamp)
iterator end() noexcept
returns an iterator to one past the last element
Definition: json.hpp:19689
array (ordered collection of values)
span_CONFIG_SIZE_TYPE size_t
Definition: span.hpp:561
a class to store JSON values
Definition: json.hpp:3326
size_t size() const
bool parseMessage(const MessageRef msg, double &timestamp) override
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
nlohmann::json _json
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
bool parseMessage(const MessageRef msg, double &timestamp) override
object (unordered set of name/value pairs)
iterator find(KeyT &&key)
find an element in a JSON object
Definition: json.hpp:19600
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
bool parseMessage(const MessageRef msg, double &timestamp) override
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
bool parseMessage(const MessageRef msg, double &timestamp) override
const uint8_t * data() const
Definition: core.h:1131
std::string _stamp_fieldname
PlotData & getSeries(const std::string &key)
reference value() const
return the value of an iterator
Definition: json.hpp:12094
std::basic_string< Char > format(const text_style &ts, const S &format_str, const Args &... args)
Definition: color.h:583


plotjuggler
Author(s): Davide Faconti
autogenerated on Mon Jun 19 2023 03:01:38