Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix JSON Character-Like-Type Input #129

Merged
merged 1 commit into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions RareCppTest/json_input_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
10 changes: 10 additions & 0 deletions include/rarecpp/json.h
Original file line number Diff line number Diff line change
Expand Up @@ -3107,6 +3107,14 @@ namespace Json
if constexpr ( !std::is_const_v<EnumType> )
value = (RareTs::remove_pointer_t<Value>)temp;
}

template <typename Value>
constexpr void charInt(std::istream & is, Value & value)
{
int temp {};
is >> temp;
value = Value(temp);
}

inline std::string fieldName(std::istream & is, char & c)
{
Expand Down Expand Up @@ -3428,6 +3436,8 @@ namespace Json
Read::enumInt<T>(is, value);
else if constexpr ( std::is_same_v<bool, std::remove_const_t<T>> )
Read::boolean<InArray>(is, c, value);
else if constexpr ( RareTs::is_char_v<RareTs::remove_cvref_t<T>> )
Read::charInt<T>(is, value);
else if constexpr ( std::is_const_v<T> )
Consume::value<InArray>(is, c);
else
Expand Down
6 changes: 6 additions & 0 deletions include/rarecpp/reflect.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <typename TypeIfVoid> struct replace_void<void, TypeIfVoid> { using type = TypeIfVoid; };
template <typename T, typename TypeIfVoid> using replace_void_t = typename replace_void<T, TypeIfVoid>::type;

template <typename T> struct is_char : std::false_type {};
template <> struct is_char<char> : std::true_type {};
template <> struct is_char<signed char> : std::true_type {};
template <> struct is_char<unsigned char> : std::true_type {};
template <typename T> inline constexpr bool is_char_v = is_char<T>::value;

template <typename T> struct promote_char { using type = T; };
template <typename T> struct promote_char<const T> { using type = std::add_const_t<typename promote_char<T>::type>; };
template <> struct promote_char<char> { using type = int; };
Expand Down