Skip to content

Commit 2ead92c

Browse files
committed
support nlohmann and fix bugs
1 parent 32e1997 commit 2ead92c

File tree

9 files changed

+329
-30
lines changed

9 files changed

+329
-30
lines changed

CMakeLists.txt

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,17 @@ if(NOT CMAKE_CXX_STANDARD OR CMAKE_CXX_STANDARD LESS 17)
55
endif()
66
set(CMAKE_CXX_STANDARD_REQUIRED ON)
77

8-
find_package(Boost)
9-
if(NOT Boost_FOUND)
10-
message(WARNING "Can't find Boot Library, please add boost_preprocessor/include in the current dir ${CMAKE_CURRENT_LIST_DIR} to your include_path.")
11-
add_subdirectory(boost_preprocessor)
12-
endif()
138
include_directories(${CMAKE_CURRENT_LIST_DIR}/boost_preprocessor/include)
149

15-
message("Currently only Qt supported.")
10+
message("Currently only Qt & Nlohmann supported.")
1611
option(JSON_DESERIALISE_DEFAULT_JSON_LIBRARY "Default Json Library" OFF)
1712
option(JSON_DESERIALISE_JSON_LIBRARIES "All libs that Need to be generated." OFF)
1813

1914
if(NOT JSON_DESERIALISE_JSON_LIBRARIES)
2015
if(NOT JSON_DESERIALISE_DEFAULT_JSON_LIBRARY)
21-
set(JSON_DESERIALISE_JSON_LIBRARIES "Qt")
22-
else(NOT JSON_DESERIALISE_JSON_LIBRARIES)
16+
set(JSON_DESERIALISE_DEFAULT_JSON_LIBRARY "Nlohmann")
17+
set(JSON_DESERIALISE_JSON_LIBRARIES "Nlohmann")
18+
else()
2319
set(JSON_DESERIALISE_JSON_LIBRARIES ${JSON_DESERIALISE_DEFAULT_JSON_LIBRARY})
2420
endif()
2521
endif()

adaptor.nlohmann.h

Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
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

adaptor.qt.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,12 @@ struct QtJsonLib {
8484
return object.contains(key);
8585
}
8686

87+
inline static void insert(JsonObject& object, const String& key, Json&& json) {
88+
object.insert(key, std::move(json));
89+
}
90+
8791
inline static void append(JsonArray& array, Json&& json) {
88-
return array.append(std::move(json));
92+
array.append(std::move(json));
8993
}
9094

9195
inline static Json uint2json(unsigned integer) {
@@ -115,6 +119,13 @@ struct QtJsonLib {
115119
throw std::ios_base::failure("Type Unmatch!");
116120
return result;
117121
}
122+
123+
inline static String tolower(const String& str) {
124+
return str.toLower();
125+
}
126+
inline static bool empty_str(const String& str) {
127+
return str.isEmpty();
128+
}
118129

119130
// String Convertors
120131

basic_types.hpp.in

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,16 @@ namespace JsonDeserialise::@LIB_ID@Private {
195195
using Type = Impl::Integer<false, sizeof(unsigned)>;
196196
};
197197

198+
template <>
199+
struct Deserialisable<int64_t> {
200+
using Type = Impl::Integer<true, 8>;
201+
};
202+
203+
template <>
204+
struct Deserialisable<uint64_t> {
205+
using Type = Impl::Integer<false, 8>;
206+
};
207+
198208
template <>
199209
struct Deserialisable<double> {
200210
using Type = Impl::Real<double>;

decorator.h.in

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
#ifndef JSON_DESERIALISER_ @LIB_ID @_DECORATOR
2-
#define JSON_DESERIALISER_ @LIB_ID @_DECORATOR
1+
#ifndef JSON_DESERIALISER_@LIB_ID@_DECORATOR
2+
#define JSON_DESERIALISER_@LIB_ID@_DECORATOR
33

44
#include "adaptor.@[email protected]"
55
#include "json_deserialise.hpp"
@@ -24,12 +24,12 @@ struct Deserialiser : public Impl::DeserialisableType<T> {
2424

2525
template <typename String>
2626
inline void from_json_string(String&& str) {
27-
Impl::JsonDeserialiser(*this)->deserialise(std::forward<String>(str));
27+
Impl::JsonDeserialiser(*this).deserialise(std::forward<String>(str));
2828
}
2929

3030
template <typename String>
3131
inline void from_file(String&& filepath) {
32-
Impl::JsonDeserialiser(*this)->deserialise_file(std::forward<String>(filepath));
32+
Impl::JsonDeserialiser(*this).deserialise_file(std::forward<String>(filepath));
3333
}
3434
};
3535

@@ -170,7 +170,7 @@ template <typename T, typename MapStyle>
170170
struct MapDeserialiser
171171
: public JsonDeserialise::@LIB_ID@::Deserialisable<T>::template Style<MapStyle::value>::Type {
172172
using Base
173-
= typename JsonDeserialise::V_ABCD_V::Deserialisable<T>::template Style<MapStyle::value>::Type;
173+
= typename JsonDeserialise::@LIB_ID@::Deserialisable<T>::template Style<MapStyle::value>::Type;
174174
template <typename... Args>
175175
MapDeserialiser(MapStyle&, T& target, Args&&... args)
176176
: Base(std::forward<Args>(args)..., target) {}

0 commit comments

Comments
 (0)