Skip to content

Commit

Permalink
Merge pull request #129 from TheNitesWhoSay/bugfix/character-like-types
Browse files Browse the repository at this point in the history
Fix JSON Character-Like-Type Input
  • Loading branch information
TheNitesWhoSay authored Nov 25, 2024
2 parents e1a6541 + d1125a3 commit 917a702
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
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

0 comments on commit 917a702

Please sign in to comment.