duckticker/src/bitcoinity_payload.hpp
King_DuckZ b3b77d5cad Add timestamp to TickerPrice
It's almost always 0, it's only filled when bitcoinity
replies with a "trade" data, other jsons don't give any
time stamps
2022-05-14 22:49:01 +02:00

97 lines
2.8 KiB
C++

/* Copyright 2022, Michele Santullo
* This file is part of duckticker.
*
* Wrenpp is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Wrenpp is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with duckticker. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <simdjson.h>
#include <string_view>
#include <cstdint>
namespace duck {
namespace detail {
template <typename As, typename Key, typename... Keys>
inline std::optional<As> get_from_node(simdjson::ondemand::object& node, Key&& key, Keys&&... keys) {
auto item_result = node[key];
if (item_result.error())
return {};
else {
if constexpr (sizeof...(Keys) > 0) {
simdjson::ondemand::object item = item_result;
return get_from_node<As>(item, std::forward<Keys>(keys)...);
}
else {
return static_cast<As>(item_result);
}
}
}
} //namespace detail
class BitcoinityPayload {
public:
static constexpr std::string_view JsonKeyPayload = "payload";
BitcoinityPayload(
simdjson::padded_string&& body_p,
simdjson::ondemand::parser&& parser_p
);
BitcoinityPayload(BitcoinityPayload&& other);
BitcoinityPayload& operator= (BitcoinityPayload&& other);
template <typename As, typename... Keys>
std::optional<As> get (Keys&&... keys) const {
return detail::get_from_node<As>(m_payload_node, std::forward<Keys>(keys)...);
}
template <typename... Keys>
std::optional<std::string_view> get_string (Keys&&... keys) const {
return this->get<std::string_view>(std::forward<Keys>(keys)...);
}
template <typename... Keys>
std::optional<double> get_double (Keys&&... keys) const {
return this->get<double>(std::forward<Keys>(keys)...);
}
template <typename... Keys>
std::string_view get_string_or_empty (Keys&&... keys) const {
auto retval = this->get_string(std::forward<Keys>(keys)...);
if (retval)
return *retval;
else
return {};
}
template <typename... Keys>
std::optional<std::uint64_t> get_uint64 (Keys&&... keys) const {
return this->get<std::uint64_t>(std::forward<Keys>(keys)...);
}
template <typename... Keys>
bool has_key (Keys&&... keys) const {
return !!this->get<simdjson::ondemand::object>(std::forward<Keys>(keys)...);
}
private:
simdjson::padded_string m_body;
mutable simdjson::ondemand::parser m_parser;
mutable simdjson::ondemand::document m_document;
mutable simdjson::ondemand::object m_payload_node;
};
} //namespace duck