-
-
Notifications
You must be signed in to change notification settings - Fork 6.7k
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
Single char converted to ASCII code instead of string #413
Comments
I understand the issue, but I fear that implicitly converting a single character to a string could be surprising, because For your convenience, you may implement a function like json from_char(char c)
{
return std::string(1, c);
} |
char is weird and the STL treats the three types [char/signed char/unsigned char] inconsistently, in my opinion. For example, "std::cout << ((uint8_t)65)" will print "A", but "std::cout << ((uint16_t)65)" prints a number. In my ideal world, 'char' would represent a character (not a number!) and 'signed char'/'unsigned char' (aka int8_t, uint8_t) would represent numeric values. There are three distinct types, but the STL gives them no semantic difference. For purity, adding "basic_json(char) = delete" removes confusion because every implicit conversion doesn't make sense (either as a character or a number); note that uint8_t and int8_t still convert as a number. But, in practice, I do not think it's worth the breaking change. |
I see, so I will continue with manual conversion to a string. Many thanks both of you for your answers, very helpful :) |
I also agree that deleting the Thanks for the discussion! I shall add a not to the documentation. |
Hello,
I'm getting two types of data in a api callback function:
and here is my function:
Type2 is recognized as a string by the json lib, but it seems type1 being a single char is converted to the equivalent ASCII number. I'm a correct? So right now I handle this by explicitly converting the single char to a string like this:
But this is not really convenient. Any idea on how to better handle this single char case, so that it's implicitly converted to a string by the library?
Many thanks for your help!
The text was updated successfully, but these errors were encountered: