Fixposition SDK 0.0.0-heads/main-0-g97f6014
Collection of c++ libraries and apps for use with Fixposition products on Linux
Loading...
Searching...
No Matches
parser.hpp
Go to the documentation of this file.
1/**
2 * \verbatim
3 * ___ ___
4 * \ \ / /
5 * \ \/ / Copyright (c) Fixposition AG (www.fixposition.com) and contributors
6 * / /\ \ License: see the LICENSE file
7 * /__/ \__\
8 * \endverbatim
9 *
10 * @file
11 * @brief Fixposition SDK: to_json() helpers for some fpsdk::common::parser
12 */
13#ifndef __FPSDK_COMMON_TO_JSON_PARSER_HPP__
14#define __FPSDK_COMMON_TO_JSON_PARSER_HPP__
15
16/* LIBC/STL */
17
18/* EXTERNAL */
19#include <nlohmann/json.hpp>
20
21/* PACKAGE */
22#include "../parser/types.hpp"
23
24#ifndef _DOXYGEN_ // not documenting these
25/* ****************************************************************************************************************** */
26namespace fpsdk::common::parser {
27
28inline void to_json(nlohmann::json& j, const ParserMsg& m)
29{
30 j = nlohmann::json::object({
31 { "_proto", ProtocolStr(m.proto_) },
32 { "_name", m.name_ },
33 { "_seq", m.seq_ },
34 });
35
36 if (!m.info_.empty()) {
37 j["_info"] = m.info_;
38 } else {
39 j["_info"] = nullptr;
40 }
41
42 if (std::all_of(m.data_.data(), m.data_.data() + m.data_.size(),
43 [](const uint8_t c) { return std::isprint(c) || std::isspace(c); })) {
44 j["_data"] = string::BufToStr(m.data_);
45 } else {
46 j["_data_b64"] = string::Base64Enc(m.data_);
47 }
48}
49
50} // namespace fpsdk::common::parser
51/* ****************************************************************************************************************** */
52#endif // !_DOXYGEN_
53#endif // __FPSDK_COMMON_TO_JSON_PARSER_HPP__
const char * ProtocolStr(const Protocol proto)
Stringify Protocol.
std::string BufToStr(const std::vector< uint8_t > &buf)
Convert buffer to string.
std::string Base64Enc(const std::vector< uint8_t > &buf)
Encode to base64.
Fixposition SDK: Parser.
Message frame output by the Parser.
Definition types.hpp:98