diff --git a/include/rfl/json/Reader.hpp b/include/rfl/json/Reader.hpp index 5ac3edf5..75e0b456 100644 --- a/include/rfl/json/Reader.hpp +++ b/include/rfl/json/Reader.hpp @@ -79,11 +79,16 @@ struct Reader { return rfl::Error("Could not cast to double."); } return static_cast(yyjson_get_num(_var.val_)); + } else if constexpr (std::is_unsigned>()) { + if (!yyjson_is_int(_var.val_)) { + return rfl::Error("Could not cast to int."); + } + return static_cast(yyjson_get_uint(_var.val_)); } else if constexpr (std::is_integral>()) { if (!yyjson_is_int(_var.val_)) { return rfl::Error("Could not cast to int."); } - return static_cast(yyjson_get_int(_var.val_)); + return static_cast(yyjson_get_sint(_var.val_)); } else { static_assert(rfl::always_false_v, "Unsupported type."); } diff --git a/include/rfl/json/Writer.hpp b/include/rfl/json/Writer.hpp index c47da53c..81d794f1 100644 --- a/include/rfl/json/Writer.hpp +++ b/include/rfl/json/Writer.hpp @@ -150,8 +150,10 @@ class Writer { return OutputVarType(yyjson_mut_bool(doc_, _var)); } else if constexpr (std::is_floating_point>()) { return OutputVarType(yyjson_mut_real(doc_, static_cast(_var))); + } else if constexpr (std::is_unsigned>()) { + return OutputVarType(yyjson_mut_uint(doc_, static_cast(_var))); } else if constexpr (std::is_integral>()) { - return OutputVarType(yyjson_mut_int(doc_, static_cast(_var))); + return OutputVarType(yyjson_mut_int(doc_, static_cast(_var))); } else { static_assert(rfl::always_false_v, "Unsupported type."); } diff --git a/tests/json/test_integers.cpp b/tests/json/test_integers.cpp new file mode 100644 index 00000000..f0ebc4dd --- /dev/null +++ b/tests/json/test_integers.cpp @@ -0,0 +1,22 @@ +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_integers { + +TEST(json, test_integers) { + + struct Integers { + int i32; + unsigned int u32; + long long i64; + unsigned long long u64; + }; + + write_and_read(Integers{.i32 = INT32_MAX, .u32 = UINT32_MAX, .i64 = INT64_MAX, .u64 = UINT64_MAX}, R"({"i32":2147483647,"u32":4294967295,"i64":9223372036854775807,"u64":18446744073709551615})"); + write_and_read(Integers{.i32 = INT32_MIN, .u32 = 0, .i64 = INT64_MIN, .u64 = 0}, R"({"i32":-2147483648,"u32":0,"i64":-9223372036854775808,"u64":0})"); +} + +} // namespace test_integers