/
uzer_007
/
reindexer
Обзор
Документация
Войти
/
uzer_007
/
reindexer
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
cpp_src/tools/jsontools.h
104 строки
4 KB
madschemas
Update to version v5.12.0
20 мар 2026, 08:17
20 мар 2026, 08:17
2eef2e6
Код
Авторство
О чём код?
#pragma once #include <string> #include <string_view> #include <span> #include "gason/gason.h" #include "tools/stringstools.h" namespace reindexer { class WrSerializer; constexpr int kJsonShiftWidth = 4; void jsonValueToString(gason::JsonValue o, WrSerializer& ser, int shift = kJsonShiftWidth, int indent = 0, bool escapeStrings = true); void prettyPrintJSON(std::span<char> json, WrSerializer& ser, int shift = kJsonShiftWidth); void prettyPrintJSON(std::string_view json, WrSerializer& ser, int shift = kJsonShiftWidth); std::string stringifyJson(const gason::JsonNode& elem, bool escapeStrings = true); /// @brief Reading values of certain type from json node to a container. /// @param node - json node. /// @returns container with node values of type T. template <typename T, template <typename, auto...> class ContainerT, auto... Args> ContainerT<T, Args...> readNodeValues(const gason::JsonNode& node) { ContainerT<T, Args...> values; if (node.empty()) { return values; } const auto tag{node.value.getTag()}; if (tag == gason::JsonTag::OBJECT) { throw Error{errParseJson, "Cannot read json object value"}; } if (tag == gason::JsonTag::ARRAY) { for (const auto& item : node) { values.emplace_back(item.As<T>()); } } else { values.emplace_back(node.As<T>()); } return values; } namespace details { /** * @brief Safely reads JSON value from parent json node, in case of errors, value left unmodified. * @tparam T type of value to read * @tparam Args for numeric types can contain typenames for value range limits of type \p T. List is deducible from arguments list. * @tparam required value importance mode. True - item with \p valueName is required and in case of absence function will * return an Error(errNotFound). * @param parent parent JSON node \p valueName item belongs to * @param valueName json value name * @param value a reference to variable to write correct value to * @param args a number of optional parameters. For numeric types can contain value range limits [min, max], respectivelly. If * ommited then these parameters will be defaulted to <tt>T minv = std::numeric_limits<T>::min(), T maxv = * std::numeric_limits<T>::max()</tt> * @returns * - Error{errOK} - \p valueName read successfuly, \p value updated; * - Error{...} - in case of read errors (inspect Error.what() for details), \p value left unchanged. */ template <bool required, typename T, typename JsonT, typename... Args> Error tryReadJsonValue(std::string* errLog, const gason::JsonNode& parent, std::string_view valueName, T& value, Args&&... args) { Error result; if (!parent[valueName].empty()) { try { value = parent[valueName].As<JsonT>(value, std::forward<Args>(args)...); } catch (const gason::Exception& ex) { result = Error(errParseJson, "{}", ex.what()); } } else if constexpr (required) { result = Error(errParseJson, "Required paramenter '{}' is not found.\n", valueName); } if (errLog && !result.ok()) { ensureEndsWith(*errLog, "\n") += result.what(); } return result; } } // namespace details /// @brief Safely reads optional JSON value template <typename T, typename... Args> Error tryReadOptionalJsonValue(std::string* errLog, const gason::JsonNode& parent, std::string_view valueName, T& value, Args&&... args) { return details::tryReadJsonValue<false, T, T, Args...>(errLog, parent, valueName, value, std::forward<Args>(args)...); } template <typename T, typename... Args> Error tryReadOptionalJsonValue(std::string* errLog, const gason::JsonNode& parent, std::string_view valueName, std::atomic<T>& value, Args&&... args) { return details::tryReadJsonValue<false, std::atomic<T>, T, Args...>(errLog, parent, valueName, value, std::forward<Args>(args)...); } /// @brief Safely reads required JSON value template <typename T, typename... Args> Error tryReadRequiredJsonValue(std::string* errLog, const gason::JsonNode& parent, std::string_view valueName, T& value, Args&&... args) { return details::tryReadJsonValue<true, T, T, Args...>(errLog, parent, valueName, value, std::forward<Args>(args)...); } template <typename T, typename... Args> Error tryReadRequiredJsonValue(std::string* errLog, const gason::JsonNode& parent, std::string_view valueName, std::atomic<T>& value, Args&&... args) { return details::tryReadJsonValue<true, std::atomic<T>, T, Args...>(errLog, parent, valueName, value, std::forward<Args>(args)...); } } // namespace reindexer