Newer
Older
using Structure = std::vector<std::tuple<uint, std::string, uint>>;
Structure PacketStructureJson::readStructureFromSource(std::string source) {
Structure s_empty;
time_t now = time(nullptr);
std::cerr << "[" << std::put_time(localtime(&now), "%Y-%m-%d %H:%M:%S") <<"]\t[PacketStructure Json]\t" << "Could not open file: "
<< source << std::endl;
std::cerr << "\t Returning empty structure" << std::endl;
return s_empty;
try {
file >> data;
} catch (nlohmann::detail::parse_error &ex) {
time_t now = time(nullptr);
std::cerr << "[" << std::put_time(localtime(&now), "%Y-%m-%d %H:%M:%S") <<"]\t[PacketStructure Json]\t" << ex.what() << "\n\t\tReturning empty structure" << std::endl;
return s_empty;
}
std::optional<Structure> s_tmp = convertToTupleVector(data, count);
if (s_tmp.has_value())
return s_tmp.value();
time_t now = time(nullptr);
std::cerr << "[" << std::put_time(localtime(&now), "%Y-%m-%d %H:%M:%S") <<"]\t[PacketStructure Json]\t" << "Returning empty structure" << std::endl;
std::optional<Structure> PacketStructureJson::convertToTupleVector(
std::optional<Structure> out = Structure();
if (it.value().is_object()) { // array found
if (it.value().find("size") == it.value().end() // error handling
|| it.value().find("fields") == it.value().end()
time_t now = time(nullptr);
std::cerr << "[" << std::put_time(localtime(&now), "%Y-%m-%d %H:%M:%S") <<"]\t[PacketStructure Json]\t" << "Error in file: "
<< it.value().dump() << std::endl;
return std::nullopt;
for (uint i = 0; i < it.value()["size"]; i++) { // convert to tuple vector the json array
std::optional<Structure> subArray = convertToTupleVector(
it.value()["fields"], count).value();
if (!subArray.has_value())
return std::nullopt;
std::for_each(subArray.value().begin(), subArray.value().end(),
std::get<1>(line).append("_#").append(
std::to_string(i));
out.value().insert(out.value().end(), subArray.value().begin(),
subArray.value().end());
} else { // single field found
if (!it.value().is_number_integer()) { // error handling
time_t now = time(nullptr);
std::cerr << "[" << std::put_time(localtime(&now), "%Y-%m-%d %H:%M:%S") <<"]\t[PacketStructure Json]\t" << "Not an int: "
<< it.value().dump() << std::endl;
return std::nullopt;
std::transform(fieldName.begin(), fieldName.end(),
fieldName.begin(), ::tolower);