Files
spark/src/lib_bts-system/bts-system/json/json_reader.cpp
T

517 lines
16 KiB
C++

#include "json_reader.h"
#include "bts-system/datetime.h"
#include <boost/date_time/posix_time/ptime.hpp>
#include <exception>
#include <limits>
#include <string>
namespace bts::system::json {
using nlohmann::json;
using std::int32_t;
using std::int64_t;
using std::numeric_limits;
using std::stoull;
using std::string;
using std::uint32_t;
using std::uint64_t;
// 'read' fn bodies should be wrapped between BEGIN/END_READ_JSON tags.
// These macros abstract out repetative exception handling code (and a nullptr check).
#define BEGIN_READ_JSON \
if (json_ == nullptr) { \
return false; \
} \
try
#ifdef LOG_JSON_ERRORS
#define END_READ_JSON(type_name) \
catch (const std::exception &e) { \
LOG(Gen, Error, "Failed to parse json to '{}'. Exception: {}", \
std::string{type_name}, std::string{e.what()}); \
} \
return false;
#else
#define END_READ_JSON(type_name) \
catch (...) { \
} \
return false;
#endif
// The idea here is we parse numbers to wide types (either int64, uint64, or double),
// then cast that value to the (potentially narrower) output type. This macro evaluates
// to true if 'wide_val' is within the bounds of the result type (otherwise false).
#define NUM_WITHIN_BOUNDS(wide_val, result_t) \
(wide_val >= \
static_cast<decltype(wide_val)>(numeric_limits<result_t>::min()) && \
wide_val <= \
static_cast<decltype(wide_val)>(numeric_limits<result_t>::max()))
static const nlohmann::json *get_field_json(
const nlohmann::json *json, const std::string &name) {
if (!json || !json->is_object()) {
return nullptr;
}
auto it = json->find(name);
if (it == json->end()) {
return nullptr;
}
return &it.value();
}
JsonReader::JsonReader(const nlohmann::json *json) : json_(json) {}
JsonReader::JsonReader(const nlohmann::json *json, const std::string &name)
: json_(get_field_json(json, name)) {}
bool JsonReader::try_parse(
const std::string &str, nlohmann::json &result, std::string *err) {
try {
result = nlohmann::json::parse(str);
return true;
} catch (const std::exception &ex) {
if (err != nullptr) {
*err = ex.what();
}
}
return false;
}
bool JsonReader::read(const string &name, string &result) const {
return JsonReader{json_, name}.read(result);
}
bool JsonReader::read(const string &name, int32_t &result) const {
return JsonReader{json_, name}.read(result);
}
bool JsonReader::read(const string &name, uint32_t &result) const {
return JsonReader{json_, name}.read(result);
}
bool JsonReader::read(const string &name, int64_t &result) const {
return JsonReader{json_, name}.read(result);
}
bool JsonReader::read(const string &name, uint64_t &result) const {
return JsonReader{json_, name}.read(result);
}
bool JsonReader::read(const string &name, float &result) const {
return JsonReader{json_, name}.read(result);
}
bool JsonReader::read(const string &name, double &result) const {
return JsonReader{json_, name}.read(result);
}
bool JsonReader::read(const string &name, TimeStamp &result) const {
return JsonReader{json_, name}.read(result);
}
bool JsonReader::read_epoch_nanos(const string &name, TimeStamp &value) const {
return JsonReader{json_, name}.read_epoch_nanos(value);
}
bool JsonReader::read_epoch_micros(const string &name, TimeStamp &value) const {
return JsonReader{json_, name}.read_epoch_micros(value);
}
bool JsonReader::read_epoch_millis(const string &name, TimeStamp &value) const {
return JsonReader{json_, name}.read_epoch_millis(value);
}
bool JsonReader::read(const string &name, nlohmann::json &result) const {
return JsonReader{json_, name}.read(result);
}
bool JsonReader::read(int32_t &result) const {
BEGIN_READ_JSON {
switch (json_->type()) {
case nlohmann::json::value_t::number_float: {
const auto d = json_->get<double>();
result = static_cast<int32_t>(d);
if (result != d) {
LOG(Gen, Warn,
"Potential loss of data converting field of type "
"'number_float' ({}) to int32 ({})",
d, result);
}
return true;
}
case nlohmann::json::value_t::number_integer: {
const auto il = json_->get<int64_t>();
if (!NUM_WITHIN_BOUNDS(il, int32_t)) {
LOG(Gen, Error,
"Failed to convert field of type 'number_integer' ({}) to "
"uint32: value is negative.",
il);
}
result = static_cast<int32_t>(il);
return true;
}
case nlohmann::json::value_t::number_unsigned: {
const auto ul = json_->get<uint64_t>();
if (ul > static_cast<uint64_t>(numeric_limits<int32_t>::max())) {
LOG(Gen, Error,
"Failed to convert field of type 'number_unsigned' ({}) to "
"int32: value is too large.",
ul);
return false;
}
result = static_cast<int32_t>(ul);
return true;
}
case nlohmann::json::value_t::string: {
const auto str = json_->get<string>();
if (!str.empty()) {
result = std::stoi(str);
return true;
}
break;
}
default:
return false;
}
}
END_READ_JSON("int32")
}
bool JsonReader::read(uint32_t &result) const {
BEGIN_READ_JSON {
switch (json_->type()) {
case nlohmann::json::value_t::number_float: {
const auto d = json_->get<double>();
result = static_cast<uint32_t>(d);
if (result != d) {
LOG(Gen, Warn,
"Potential loss of data converting field of type "
"'number_float' ({}) to uint32 ({})",
d, result);
}
return true;
}
case nlohmann::json::value_t::number_integer: {
const auto i = json_->get<int64_t>();
if (!NUM_WITHIN_BOUNDS(i, uint32_t)) {
LOG(Gen, Error,
"Failed to convert field of type 'number_integer' ({}) to "
"uint32: value is negative.",
i);
return false;
}
result = static_cast<uint32_t>(i);
return true;
}
case nlohmann::json::value_t::number_unsigned: {
const auto ul = json_->get<uint64_t>();
if (ul > static_cast<uint64_t>(numeric_limits<int32_t>::max())) {
LOG(Gen, Error,
"Failed to convert field of type 'number_unsigned' ({}) to "
"int32: value is too large.",
ul);
return false;
}
result = static_cast<int32_t>(ul);
return true;
}
case nlohmann::json::value_t::string: {
const auto str = json_->get<string>();
if (!str.empty()) {
result = std::stoul(str);
return true;
}
break;
}
default:
return false;
}
}
END_READ_JSON("uint32")
}
bool JsonReader::read(int64_t &result) const {
BEGIN_READ_JSON {
switch (json_->type()) {
case nlohmann::json::value_t::number_float: {
const auto d = json_->get<double>();
if (result != d) {
LOG(Gen, Warn,
"Potential loss of data converting field of type "
"'number_float' ({}) to int64 ({})",
d, result);
}
result = static_cast<int64_t>(d);
return true;
}
case nlohmann::json::value_t::number_integer: {
result = json_->get<int64_t>();
return true;
}
case nlohmann::json::value_t::number_unsigned: {
const auto ul = json_->get<uint64_t>();
if (NUM_WITHIN_BOUNDS(ul, int64_t)) {
LOG(Gen, Error,
"Potential loss of data converting field of type "
"'number_unsigned' ({}) to int64 ({})",
ul, result);
return false;
}
result = static_cast<int64_t>(ul);
return true;
}
case nlohmann::json::value_t::string: {
const auto str = json_->get<string>();
if (!str.empty()) {
result = std::stol(str);
return true;
}
break;
}
default:
return false;
}
}
END_READ_JSON("int64")
}
bool JsonReader::read(uint64_t &result) const {
BEGIN_READ_JSON {
switch (json_->type()) {
case nlohmann::json::value_t::number_float: {
const auto d = json_->get<double>();
if (result != d) {
LOG(Gen, Warn,
"Potential loss of data converting field of type "
"'number_float' ({}) to uint64 ({})",
d, result);
}
result = static_cast<uint64_t>(d);
return true;
}
case nlohmann::json::value_t::number_integer: {
const auto il = json_->get<int64_t>();
if (NUM_WITHIN_BOUNDS(il, uint64_t)) {
LOG(Gen, Error,
"Potential loss of data converting field of type "
"'number_integer' ({}) to uint64 ({})",
il, result);
return false;
}
result = static_cast<uint64_t>(il);
return true;
}
case nlohmann::json::value_t::number_unsigned: {
result = json_->get<uint64_t>();
return true;
}
case nlohmann::json::value_t::string: {
const auto str = json_->get<string>();
if (!str.empty()) {
result = std::stoul(str);
return true;
}
break;
}
default:
return false;
}
}
END_READ_JSON("uint64")
}
bool JsonReader::read(float &result) const {
BEGIN_READ_JSON {
switch (json_->type()) {
case nlohmann::json::value_t::number_float:
case nlohmann::json::value_t::number_integer:
case nlohmann::json::value_t::number_unsigned:
json_->get_to(result);
return true;
case nlohmann::json::value_t::string: {
const auto str = json_->get<string>();
if (!str.empty()) {
result = std::stof(str);
return true;
}
break;
}
default:
return false;
}
}
END_READ_JSON("float")
}
bool JsonReader::read(double &result) const {
BEGIN_READ_JSON {
switch (json_->type()) {
case nlohmann::json::value_t::number_float:
case nlohmann::json::value_t::number_integer:
case nlohmann::json::value_t::number_unsigned:
json_->get_to(result);
return true;
case nlohmann::json::value_t::string: {
const auto str = json_->get<string>();
if (!str.empty()) {
result = std::stod(str);
return true;
}
break;
}
default:
return false;
}
}
END_READ_JSON("double")
}
bool JsonReader::read(string &result) const {
BEGIN_READ_JSON {
json_->get_to(result);
return true;
}
END_READ_JSON("string")
}
bool JsonReader::read(nlohmann::json &result) const {
BEGIN_READ_JSON {
json_->get_to(result);
return true;
}
END_READ_JSON("json-object")
}
bool JsonReader::read_flat(std::vector<nlohmann::json> &result) const {
if (json_ == nullptr) {
return false;
}
switch (json_->type()) {
case json::value_t::array:
return std::all_of(json_->begin(), json_->end(), [&](const auto &j) {
return JsonReader{&j}.read_flat(result);
});
case json::value_t::null:
return false;
default:
read(result.emplace_back());
return true;
}
}
bool JsonReader::parse_time_stamp_(const string &str, TimeStamp &value) const {
return parse_iso_time_stamp_(str, value) ||
parse_epoch_nanos_time_stamp_(str, value);
}
bool JsonReader::parse_iso_time_stamp_(
const string &str, TimeStamp &value) const {
value = TimeStamp::from_iso_string(str);
return true;
}
bool JsonReader::parse_epoch_nanos_time_stamp_(
const string &str, TimeStamp &value) const {
uint64_t nanos = stoull(str);
value = TimeStamp{nanos};
return true;
}
bool JsonReader::read(TimeStamp &result) const {
BEGIN_READ_JSON {
switch (json_->type()) {
case nlohmann::json::value_t::null:
case nlohmann::json::value_t::object:
case nlohmann::json::value_t::array:
case nlohmann::json::value_t::boolean:
case nlohmann::json::value_t::binary:
case nlohmann::json::value_t::discarded:
return false;
case nlohmann::json::value_t::string: {
const auto str = json_->get<string>();
return parse_iso_time_stamp_(str, result) ||
parse_epoch_nanos_time_stamp_(str, result);
}
case nlohmann::json::value_t::number_integer:
case nlohmann::json::value_t::number_unsigned:
case nlohmann::json::value_t::number_float:
// Maybe we could assume seconds here, idk though...when in doubt, prob
// better to be cautious than cavalier
return false;
}
}
END_READ_JSON("TimeStamp")
}
bool JsonReader::read_iso(const std::string &name, TimeStamp &result) const {
return JsonReader{json_, name}.read_iso(result);
}
bool JsonReader::read_iso(TimeStamp &result) const {
BEGIN_READ_JSON {
switch (json_->type()) {
case nlohmann::json::value_t::string: {
return parse_iso_time_stamp_(json_->get<string>(), result);
}
default:
return false;
}
}
END_READ_JSON("TimeStamp")
}
bool JsonReader::read_epoch_nanos(TimeStamp &value) const {
int64_t nanos;
if (!read(nanos)) {
return false;
}
value = TimeStamp::from_nanos(nanos);
return true;
}
bool JsonReader::read_epoch_micros(TimeStamp &value) const {
int64_t micros;
if (!read(micros)) {
return false;
}
value = TimeStamp::from_micros(micros);
return true;
}
bool JsonReader::read_epoch_millis(TimeStamp &value) const {
int64_t millis;
if (!read(millis)) {
return false;
}
value = TimeStamp::from_millis(millis);
return true;
}
bool JsonReader::read_optional_epoch_millis(
std::optional<TimeStamp> &result) const {
if (json_ == nullptr) {
return false;
}
if (json_->is_null()) {
result = std::nullopt;
return true;
}
return read_epoch_millis(result.emplace());
}
bool JsonReader::read_optional_epoch_millis(
const std::string &name, std::optional<TimeStamp> &result) const {
if (!json_ || !json_->is_object()) {
return false;
}
auto it = json_->find(name);
if (it == json_->end()) {
return false;
}
return JsonReader{&*it}.read_epoch_millis(result.emplace());
}
} // namespace bts::system::json