u8json: char aint a compatible integer type !? Differentiate between char and integer types. Proposal. #4294
ingo-loehken
started this conversation in
General
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Background
While adapting
nlohmann::json
to supportstd::u8string
(C++20 and beyond) asstring_type
, different problems occur, because the change in the character type is not yet represented explicitly in type_traits and apis. There is currently one test, that uses an alternativestring_type
, but still useschar
as character-type.C++11: Char Type Candidates
char
andwchar_t
are called ordinary char types.charN_t
are called char types.std::uint8_t
akaunsigned char
andstd::int8_t
akasigned char
Of those
char
,char8_t
,std::uint8_t
andstd::int8_t
are candidates (or any enum (class) with such an underlying type). Due to lack of an explicit character type that is associated with utf8,char
was for C++11 the obvious choice or (to disambiguity by type, wherechar
may represent any encoding)unsigned char
.C++20: An Alternative:
std::u8string
->u8json
Paradigmen: I would expect, that anywhere a string literal, or range or whatever is used to represent a sequence of chars is possible, only
char8_t
is accepted as character type to avoid MojibakeTherefore
char
based sequence withu8json
is rejectedchar
withu8json
is rejectedThis means that
char
should not be interpreted as a compatible integer type, but as a character type (at least for C++20 and u8json). The C++ standard defineschar
andbool
as integral types (ofc technical the types are integral), but no one would argue aboutbool
not beeing a compatible integer type (as most won't do forchar8_t
,char16_t
,char32_t
, which are also integral types). Therefore I argue that differentiating between char and integer types would avoid misinterpretation and should be at least mandatory foru8json
...Example
Currently it is legal to write code like
with this proposal
and for all other character types
nlohmann::json
it is currently interprected asnlohmann::json::array_t
I would prefer to deny all character types anywhere else (for
u8json
), so that the user has to explicitly state its intent not mixing number and character types.Notify
@nlohmann
Please share your thoughts.
Beta Was this translation helpful? Give feedback.
All reactions