Skip to content

Commit f0c49de

Browse files
committed
feat: 更新meojson
1 parent 9e60c60 commit f0c49de

6 files changed

Lines changed: 144 additions & 46 deletions

File tree

include/meojson/common/array.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ class array
268268
{
269269
jsonization_t dst {};
270270
if (!dst.from_json(*this)) {
271-
throw exception("Wrong JSON");
271+
throw exception("Deserialization failed: from_json() returned false for array, size=" + std::to_string(size()));
272272
}
273273
return dst;
274274
}
@@ -280,7 +280,7 @@ class array
280280
{
281281
jsonization_t dst {};
282282
if (!ext::jsonization<std::decay_t<jsonization_t>>().from_json(*this, dst)) {
283-
throw exception("Wrong JSON");
283+
throw exception("Deserialization failed: from_json() returned false for array, size=" + std::to_string(size()));
284284
}
285285
return dst;
286286
}
@@ -292,7 +292,7 @@ class array
292292
{
293293
jsonization_t dst {};
294294
if (!ext::jsonization<std::decay_t<jsonization_t>>().move_from_json(std::move(*this), dst)) {
295-
throw exception("Wrong JSON");
295+
throw exception("Deserialization failed: move_from_json() returned false for array, size=" + std::to_string(size()));
296296
}
297297
return dst;
298298
}

include/meojson/common/array_impl.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ inline T array::as() const&
333333
if constexpr (_utils::is_fixed_array<T>) {
334334
constexpr size_t size = _utils::fixed_array_size<T>;
335335
if (_array_data.size() != size) {
336-
throw exception("Array size mismatch");
336+
throw exception("Array size mismatch: expected=" + std::to_string(size) + ", actual=" + std::to_string(_array_data.size()));
337337
}
338338

339339
T result;
@@ -357,7 +357,7 @@ inline T array::as() const&
357357
else if constexpr (_utils::is_tuple_like<T>) {
358358
constexpr size_t tuple_size = std::tuple_size_v<T>;
359359
if (_array_data.size() != tuple_size) {
360-
throw exception("Array size mismatch for tuple conversion");
360+
throw exception("Array size mismatch for tuple conversion: expected=" + std::to_string(tuple_size) + ", actual=" + std::to_string(_array_data.size()));
361361
}
362362

363363
T result;
@@ -375,7 +375,7 @@ inline T array::as() &&
375375
if constexpr (_utils::is_fixed_array<T>) {
376376
constexpr size_t size = _utils::fixed_array_size<T>;
377377
if (_array_data.size() != size) {
378-
throw exception("Array size mismatch");
378+
throw exception("Array size mismatch: expected=" + std::to_string(size) + ", actual=" + std::to_string(_array_data.size()));
379379
}
380380

381381
T result;
@@ -401,7 +401,7 @@ inline T array::as() &&
401401
else if constexpr (_utils::is_tuple_like<T>) {
402402
constexpr size_t tuple_size = std::tuple_size_v<T>;
403403
if (_array_data.size() != tuple_size) {
404-
throw exception("Array size mismatch for tuple conversion");
404+
throw exception("Array size mismatch for tuple conversion: expected=" + std::to_string(tuple_size) + ", actual=" + std::to_string(_array_data.size()));
405405
}
406406

407407
T result;

include/meojson/common/enum_reflection.hpp

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
#define MEOJSON_ENUM_REFLECTION_MAX_ENUMS 128
1414
#endif
1515

16+
#define MEOJSON_ENUM_RANGE(min_enum, max_enum) _MEOJSON_ENUM_MIN = min_enum, _MEOJSON_ENUM_MAX = max_enum
17+
1618
namespace json
1719
{
1820
namespace _reflection
@@ -21,6 +23,7 @@ template <typename E, E V>
2123
constexpr std::string_view name() noexcept
2224
{
2325
#if defined(__clang__) || defined(__GNUG__)
26+
2427
std::string_view name = __PRETTY_FUNCTION__;
2528
auto start = name.find("V = ");
2629
if (start == std::string_view::npos) {
@@ -41,7 +44,9 @@ constexpr std::string_view name() noexcept
4144
name = name.substr(sep + 1);
4245
}
4346
return name;
47+
4448
#elif defined(_MSC_VER)
49+
4550
std::string_view name = __FUNCSIG__;
4651
auto start = name.find(',');
4752
if (start == std::string_view::npos) {
@@ -64,8 +69,11 @@ constexpr std::string_view name() noexcept
6469
name = name.substr(sep + 1);
6570
}
6671
return name;
72+
6773
#else
74+
6875
return {};
76+
6977
#endif
7078
}
7179

@@ -102,11 +110,55 @@ constexpr bool is_valid() noexcept
102110
return true;
103111
}
104112

113+
// 检测枚举类型是否存在 _MEOJSON_ENUM_MIN
114+
template <typename E, typename = void>
115+
struct has_enum_min : std::false_type
116+
{};
117+
118+
template <typename E>
119+
struct has_enum_min<E, std::void_t<decltype(E::_MEOJSON_ENUM_MIN)>> : std::true_type
120+
{};
121+
122+
// 检测枚举类型是否存在 _MEOJSON_ENUM_MAX
123+
template <typename E, typename = void>
124+
struct has_enum_max : std::false_type
125+
{};
126+
127+
template <typename E>
128+
struct has_enum_max<E, std::void_t<decltype(E::_MEOJSON_ENUM_MAX)>> : std::true_type
129+
{};
130+
131+
// 获取枚举最小值
132+
template <typename E, bool = has_enum_min<E>::value>
133+
struct get_enum_min
134+
{
135+
static constexpr int value = MEOJSON_ENUM_REFLECTION_MIN_ENUMS;
136+
};
137+
138+
template <typename E>
139+
struct get_enum_min<E, true>
140+
{
141+
static constexpr int value = static_cast<int>(E::_MEOJSON_ENUM_MIN);
142+
};
143+
144+
// 获取枚举最大值
145+
template <typename E, bool = has_enum_max<E>::value>
146+
struct get_enum_max
147+
{
148+
static constexpr int value = MEOJSON_ENUM_REFLECTION_MAX_ENUMS;
149+
};
150+
151+
template <typename E>
152+
struct get_enum_max<E, true>
153+
{
154+
static constexpr int value = static_cast<int>(E::_MEOJSON_ENUM_MAX);
155+
};
156+
105157
template <typename E>
106158
struct enum_range
107159
{
108-
static constexpr int min = MEOJSON_ENUM_REFLECTION_MIN_ENUMS;
109-
static constexpr int max = MEOJSON_ENUM_REFLECTION_MAX_ENUMS;
160+
static constexpr int min = get_enum_min<E>::value;
161+
static constexpr int max = get_enum_max<E>::value;
110162
};
111163

112164
template <typename E, int... Is>

include/meojson/common/object.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ class object
176176
{
177177
jsonization_t dst {};
178178
if (!dst.from_json(*this)) {
179-
throw exception("Wrong JSON");
179+
throw exception("Deserialization failed: from_json() returned false for object, size=" + std::to_string(size()));
180180
}
181181
return dst;
182182
}
@@ -188,7 +188,7 @@ class object
188188
{
189189
jsonization_t dst {};
190190
if (!ext::jsonization<std::decay_t<jsonization_t>>().from_json(*this, dst)) {
191-
throw exception("Wrong JSON");
191+
throw exception("Deserialization failed: from_json() returned false for object, size=" + std::to_string(size()));
192192
}
193193
return dst;
194194
}
@@ -200,7 +200,7 @@ class object
200200
{
201201
jsonization_t dst {};
202202
if (!ext::jsonization<std::decay_t<jsonization_t>>().move_from_json(std::move(*this), dst)) {
203-
throw exception("Wrong JSON");
203+
throw exception("Deserialization failed: move_from_json() returned false for object, size=" + std::to_string(size()));
204204
}
205205
return dst;
206206
}

include/meojson/common/value.hpp

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ class value
3838
string,
3939
number,
4040
array,
41-
object
41+
object,
42+
43+
MEOJSON_ENUM_RANGE(invalid, object)
4244
};
4345

4446
using var_t = std::variant<std::string, array_ptr, object_ptr>;
@@ -87,7 +89,7 @@ class value
8789
{
8890
#ifndef MEOJSON_ENUM_AS_NUMBER
8991
if (as_string().empty()) {
90-
throw exception("Unknown Enum Value");
92+
throw exception("Cannot convert to enum: unknown enum value, " + value_info());
9193
}
9294
#endif
9395
}
@@ -339,6 +341,10 @@ class value
339341

340342
value_type type() const noexcept;
341343

344+
std::string type_name() const noexcept;
345+
346+
std::string value_info() const noexcept;
347+
342348
const value& at(size_t pos) const;
343349
const value& at(const std::string& key) const;
344350

@@ -480,14 +486,14 @@ class value
480486
return *enum_opt;
481487
}
482488
else {
483-
throw exception("Wrong Enum String Value:" + as_string());
489+
throw exception("Cannot convert to enum: invalid enum string value, " + value_info());
484490
}
485491
}
486492
else if (is_number()) {
487493
return static_cast<enum_t>(static_cast<std::underlying_type_t<enum_t>>(*this));
488494
}
489495
else {
490-
throw exception("Wrong Type");
496+
throw exception("Cannot convert to enum: expected string or number type, " + value_info());
491497
}
492498
}
493499

@@ -496,7 +502,7 @@ class value
496502
{
497503
jsonization_t dst {};
498504
if (!dst.from_json(*this)) {
499-
throw exception("Wrong JSON");
505+
throw exception("Deserialization failed: from_json() returned false, " + value_info());
500506
}
501507
return dst;
502508
}
@@ -508,7 +514,7 @@ class value
508514
{
509515
jsonization_t dst {};
510516
if (!ext::jsonization<std::decay_t<jsonization_t>>().from_json(*this, dst)) {
511-
throw exception("Wrong JSON");
517+
throw exception("Deserialization failed: from_json() returned false, " + value_info());
512518
}
513519
return dst;
514520
}
@@ -520,7 +526,7 @@ class value
520526
{
521527
jsonization_t dst {};
522528
if (!ext::jsonization<std::decay_t<jsonization_t>>().move_from_json(std::move(*this), dst)) {
523-
throw exception("Wrong JSON");
529+
throw exception("Deserialization failed: move_from_json() returned false, " + value_info());
524530
}
525531
return dst;
526532
}

0 commit comments

Comments
 (0)