Files
Adminive/backend/include/adminive/adapters/nlohmann_json.hpp
T
2026-08-07 00:01:16 +08:00

113 lines
3.5 KiB
C++

#pragma once
#include "adminive/adapter.hpp"
#include "nlohmann/json.hpp"
#include <cstdint>
#include <string>
#include <string_view>
#include <utility>
#include <vector>
namespace adminive {
template <>
struct Json_Adapter<nlohmann::json> {
using Json = nlohmann::json;
static Json object() {
return Json::object();
}
static Json array() {
return Json::array();
}
static Json null() {
return nullptr;
}
static Json parse(std::string_view value) {
return Json::parse(value.begin(), value.end());
}
static std::string dump(const Json& value, int indent) {
return value.dump(indent);
}
static bool is_null(const Json& value) noexcept {
return value.is_null();
}
static bool is_object(const Json& value) noexcept {
return value.is_object();
}
static bool is_array(const Json& value) noexcept {
return value.is_array();
}
static bool is_string(const Json& value) noexcept {
return value.is_string();
}
static bool is_signed_integer(const Json& value) noexcept {
return value.is_number_integer() && !value.is_number_unsigned();
}
static bool is_unsigned_integer(const Json& value) noexcept {
return value.is_number_unsigned();
}
static bool is_floating_point(const Json& value) noexcept {
return value.is_number_float();
}
static bool is_number(const Json& value) noexcept {
return value.is_number();
}
static bool is_boolean(const Json& value) noexcept {
return value.is_boolean();
}
static std::int64_t signed_integer(const Json& value) {
return value.get<std::int64_t>();
}
static std::uint64_t unsigned_integer(const Json& value) {
return value.get<std::uint64_t>();
}
static long double number(const Json& value) {
return value.get<long double>();
}
static bool contains(const Json& value, std::string_view name) {
return value.contains(std::string(name));
}
static std::size_t size(const Json& value) noexcept {
return value.size();
}
static std::vector<std::string> keys(const Json& value) {
std::vector<std::string> result;
result.reserve(value.size());
for(auto iterator = value.begin(); iterator != value.end(); ++iterator) {
result.push_back(iterator.key());
}
return result;
}
static Json& at(Json& value, std::string_view name) {
return value.at(std::string(name));
}
static const Json& at(const Json& value, std::string_view name) {
return value.at(std::string(name));
}
static Json& at(Json& value, std::size_t index) {
return value.at(index);
}
static const Json& at(const Json& value, std::size_t index) {
return value.at(index);
}
static void set(Json& value, std::string_view name, Json item) {
value[std::string(name)] = std::move(item);
}
static void append(Json& value, Json item) {
value.push_back(std::move(item));
}
static void erase(Json& value, std::string_view name) {
value.erase(std::string(name));
}
template <class T>
static Json make(T&& value) {
if constexpr(std::same_as<std::remove_cvref_t<T>, std::string_view>) {
return Json(std::string(value));
} else {
return Json(std::forward<T>(value));
}
}
template <class T>
static T get(const Json& value) {
return value.get<T>();
}
};
}