|
| 1 | +#ifndef JSON_DESERIALISER_NLOHMANN_H |
| 2 | +#define JSON_DESERIALISER_NLOHMANN_H |
| 3 | + |
| 4 | +#include "type_deduction.nlohmann.h" |
| 5 | + |
| 6 | +#include <nlohmann/json.hpp> |
| 7 | +#include <cstring> |
| 8 | +#include <fstream> |
| 9 | +#include <type_traits> |
| 10 | +#include <string_view> |
| 11 | + |
| 12 | +namespace JsonDeserialise { |
| 13 | +struct NlohmannJsonLib { |
| 14 | + |
| 15 | + // Essential alias |
| 16 | + |
| 17 | + template <typename Any> |
| 18 | + using Deserialisable = NlohmannJsonLibPrivate::Deserialisable<Any>; |
| 19 | + |
| 20 | + template <typename Any> |
| 21 | + using DeserialisableType = typename Deserialisable<Any>::Type; |
| 22 | + |
| 23 | + template <auto member_offset> |
| 24 | + using Customised = NlohmannJsonLibPrivate::Customised<member_offset>; |
| 25 | + |
| 26 | + // Basic Types |
| 27 | + |
| 28 | + using String = std::string; |
| 29 | + using CString = String; |
| 30 | + using Json = nlohmann::json; |
| 31 | + using JsonArray = Json; |
| 32 | + using JsonObject = Json; |
| 33 | + using StringView = String; |
| 34 | + |
| 35 | + // Basic functions |
| 36 | + |
| 37 | + inline static bool is_null(const Json& json) { |
| 38 | + return json.is_null(); |
| 39 | + } |
| 40 | + inline static bool is_array(const Json& json) { |
| 41 | + return json.is_array(); |
| 42 | + } |
| 43 | + inline static bool is_object(const Json& json) { |
| 44 | + return json.is_object(); |
| 45 | + } |
| 46 | + inline static bool is_string(const Json& json) { |
| 47 | + return json.is_string(); |
| 48 | + } |
| 49 | + inline static bool is_number(const Json& json) { |
| 50 | + return json.is_number(); |
| 51 | + } |
| 52 | + inline static bool is_bool(const Json& json) { |
| 53 | + return json.is_boolean(); |
| 54 | + } |
| 55 | + |
| 56 | + // Get Methods may not be const ref, and return type could even be const ref, |
| 57 | + // which depends on the library's physical implementation. |
| 58 | + |
| 59 | + inline static const JsonArray& get_array(const Json& json) { |
| 60 | + return json; |
| 61 | + } |
| 62 | + inline static const JsonObject& get_object(const Json& json) { |
| 63 | + return json; |
| 64 | + } |
| 65 | + inline static String get_string(const Json& json) { |
| 66 | + return json.get<String>(); |
| 67 | + } |
| 68 | + inline static double get_double(const Json& json) { |
| 69 | + return json.get<double>(); |
| 70 | + } |
| 71 | + inline static int get_int(const Json& json) { |
| 72 | + return json.get<int>(); |
| 73 | + } |
| 74 | + inline static unsigned get_uint(const Json& json) { |
| 75 | + return json.get<unsigned>(); |
| 76 | + } |
| 77 | + inline static int get_int64(const Json& json) { |
| 78 | + return json.get<int64_t>(); |
| 79 | + } |
| 80 | + inline static unsigned get_uint64(const Json& json) { |
| 81 | + return json.get<uint64_t>(); |
| 82 | + } |
| 83 | + inline static bool get_bool(const Json& json) { |
| 84 | + return json.get<bool>(); |
| 85 | + } |
| 86 | + |
| 87 | + inline static bool exists(const JsonObject& object, const String& key) { |
| 88 | + return object.contains(key); |
| 89 | + } |
| 90 | + |
| 91 | + inline static void insert(JsonObject& object, const String& key, Json&& json) { |
| 92 | + object.emplace(key, std::move(json)); |
| 93 | + } |
| 94 | + |
| 95 | + inline static void append(JsonArray& array, Json&& json) { |
| 96 | + array.emplace_back(std::move(json)); |
| 97 | + } |
| 98 | + |
| 99 | + inline static Json uint2json(unsigned integer) { |
| 100 | + return integer; |
| 101 | + } |
| 102 | + |
| 103 | + // String Contravariance |
| 104 | + |
| 105 | + inline static int str2int(const String& str) { |
| 106 | + return std::stoi(str); |
| 107 | + } |
| 108 | + inline static unsigned str2uint(const String& str) { |
| 109 | + return std::stoul(str); |
| 110 | + } |
| 111 | + inline static int str2int64(const String& str) { |
| 112 | + return std::stoll(str); |
| 113 | + } |
| 114 | + inline static unsigned str2uint64(const String& str) { |
| 115 | + return std::stoull(str); |
| 116 | + } |
| 117 | + inline static unsigned str2double(const String& str) { |
| 118 | + return std::stod(str); |
| 119 | + } |
| 120 | + |
| 121 | + inline static String tolower(const String& str) { |
| 122 | + String result; |
| 123 | + std::transform(str.cbegin(), str.cend(), std::back_inserter(result), ::tolower); |
| 124 | + return result; |
| 125 | + } |
| 126 | + inline static bool empty_str(const String& str) { |
| 127 | + return str.empty(); |
| 128 | + } |
| 129 | + |
| 130 | + // String Convertors |
| 131 | + |
| 132 | + template <typename T> |
| 133 | + struct StringConvertor { |
| 134 | + static constexpr bool value = false; |
| 135 | + }; |
| 136 | + template <> |
| 137 | + struct StringConvertor<char*> { |
| 138 | + static constexpr bool value = true; |
| 139 | + static inline char* convert(const String& str) { |
| 140 | + std::string_view view{str}; |
| 141 | + const auto length = str.length(); |
| 142 | + char* des = new char[length + 1]; |
| 143 | + std::strncpy(des, view.data(), length); |
| 144 | + des[length] = '\0'; |
| 145 | + return des; |
| 146 | + } |
| 147 | + static inline String deconvert(const char* src) { |
| 148 | + return src; |
| 149 | + } |
| 150 | + }; |
| 151 | + template <> |
| 152 | + struct StringConvertor<const char*> : public StringConvertor<char*> {}; |
| 153 | + template <> |
| 154 | + struct StringConvertor<std::string> { |
| 155 | + static constexpr bool value = true; |
| 156 | + static inline const std::string& convert(const std::string& str) { |
| 157 | + return str; |
| 158 | + } |
| 159 | + static inline const std::string& deconvert(const std::string& src) { |
| 160 | + return src; |
| 161 | + } |
| 162 | + }; |
| 163 | + |
| 164 | + // Implementations |
| 165 | + |
| 166 | + static Json parse(const String& json) { |
| 167 | + return nlohmann::json::parse(json); |
| 168 | + } |
| 169 | + |
| 170 | + static Json parse_file(const String& filepath) { |
| 171 | + std::ifstream file(filepath); |
| 172 | + if (!file.is_open()) |
| 173 | + throw std::ios_base::failure("Failed to Open File!"); |
| 174 | + std::string data {std::istreambuf_iterator(file), std::istreambuf_iterator<char>()}; |
| 175 | + file.close(); |
| 176 | + return parse(data); |
| 177 | + } |
| 178 | + |
| 179 | + static String print_json(Json&& data, bool compress) { |
| 180 | +#ifdef _DEBUG |
| 181 | + if (!data.isObject() && !data.isArray() && !data.isNull()) |
| 182 | + throw std::ios_base::failure("Invalid root JSON!"); |
| 183 | +#endif |
| 184 | + return data.dump(compress ? -1 : 4); |
| 185 | + } |
| 186 | + |
| 187 | + static void write_json(Json&& json, const String& filepath, bool compress) { |
| 188 | + auto data = print_json(std::move(json), compress); |
| 189 | + std::ofstream file(filepath); |
| 190 | + if (!file.is_open()) |
| 191 | + throw std::ios_base::failure("Failed to Open File!"); |
| 192 | + file << data; |
| 193 | + file.close(); |
| 194 | + } |
| 195 | + |
| 196 | + template <size_t limit> |
| 197 | + static void char_array_write(char* des, String&& json) { |
| 198 | + std::string_view view{json}; |
| 199 | + const auto length = json.length(); |
| 200 | + auto size = length >= limit ? limit - 1 : length; |
| 201 | + std::strncpy(des, view.data(), size); |
| 202 | + des[size] = '\0'; |
| 203 | + } |
| 204 | +}; |
| 205 | + |
| 206 | +} // namespace JsonDeserialise |
| 207 | + |
| 208 | +#endif // JSON_DESERIALISER_NLOHMANN_H |
0 commit comments