From d1125a3393b25d57df6a6c61edd9cf668d81dced Mon Sep 17 00:00:00 2001 From: Justin F Date: Sun, 24 Nov 2024 23:58:19 -0600 Subject: [PATCH] Fix JSON Character-Like-Type Input --- RareCppTest/json_input_test.cpp | 38 +++++++++++++++++++++++++++++++++ include/rarecpp/json.h | 10 +++++++++ include/rarecpp/reflect.h | 6 ++++++ 3 files changed, 54 insertions(+) diff --git a/RareCppTest/json_input_test.cpp b/RareCppTest/json_input_test.cpp index a8101d7..80e0ffe 100644 --- a/RareCppTest/json_input_test.cpp +++ b/RareCppTest/json_input_test.cpp @@ -2099,4 +2099,42 @@ TEST_HEADER(JsonInput, InProxyReflected) EXPECT_EQ(5, object.a); } +struct CharacterLikeTypes +{ + char a = '\0'; + signed char b = '\0'; + unsigned char c = '\0'; + std::int8_t d = 0; + std::uint8_t e = 0; + + REFLECT(CharacterLikeTypes, a, b, c, d, e) +}; + +TEST_HEADER(JsonInput, InCharacterLikeTypes) +{ + std::stringstream smallInput("{\"a\":1,\"b\":2,\"c\":3,\"d\":4,\"e\":5}"); + CharacterLikeTypes small {}; + smallInput >> Json::in(small); + EXPECT_EQ(1, small.a); + EXPECT_EQ(2, small.b); + EXPECT_EQ(3, small.c); + EXPECT_EQ(4, small.d); + EXPECT_EQ(5, small.e); + + std::stringstream largeInput("{\"a\":100,\"b\":101,\"c\":102,\"d\":103,\"e\":104}"); + CharacterLikeTypes large {}; + largeInput >> Json::in(large); + EXPECT_EQ(100, large.a); + EXPECT_EQ(101, large.b); + EXPECT_EQ(102, large.c); + EXPECT_EQ(103, large.d); + EXPECT_EQ(104, large.e); + + std::stringstream negativeInput("{\"b\":-101,\"d\":-102}"); + CharacterLikeTypes negative {}; + negativeInput >> Json::in(negative); + EXPECT_EQ(-101, negative.b); + EXPECT_EQ(-102, negative.d); +} + #endif \ No newline at end of file diff --git a/include/rarecpp/json.h b/include/rarecpp/json.h index c9d6138..16c6db0 100644 --- a/include/rarecpp/json.h +++ b/include/rarecpp/json.h @@ -3107,6 +3107,14 @@ namespace Json if constexpr ( !std::is_const_v ) value = (RareTs::remove_pointer_t)temp; } + + template + constexpr void charInt(std::istream & is, Value & value) + { + int temp {}; + is >> temp; + value = Value(temp); + } inline std::string fieldName(std::istream & is, char & c) { @@ -3428,6 +3436,8 @@ namespace Json Read::enumInt(is, value); else if constexpr ( std::is_same_v> ) Read::boolean(is, c, value); + else if constexpr ( RareTs::is_char_v> ) + Read::charInt(is, value); else if constexpr ( std::is_const_v ) Consume::value(is, c); else diff --git a/include/rarecpp/reflect.h b/include/rarecpp/reflect.h index 8e2ea77..8d9f4e9 100644 --- a/include/rarecpp/reflect.h +++ b/include/rarecpp/reflect.h @@ -254,6 +254,12 @@ i0,i1,i2,i3,i4,i5,i6,i7,i8,i9,j0,j1,j2,j3,j4,j5,j6,j7,j8,argAtArgMax,...) argAtA template struct replace_void { using type = TypeIfVoid; }; template using replace_void_t = typename replace_void::type; + template struct is_char : std::false_type {}; + template <> struct is_char : std::true_type {}; + template <> struct is_char : std::true_type {}; + template <> struct is_char : std::true_type {}; + template inline constexpr bool is_char_v = is_char::value; + template struct promote_char { using type = T; }; template struct promote_char { using type = std::add_const_t::type>; }; template <> struct promote_char { using type = int; };