You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm not sure if this is an issue,
but it's not possible to assign a json object to a variable of type char.
With all other integral types it works, only the char type makes error.
Example:
json j;
j = 'B'; // ok, store a value as type number_integerchar b = j; // compiler error: cannot convert from 'json' to 'char'
I tried the code and got the following error message:
error: no viable conversion from 'json' (aka 'basic_json<>') to 'char'
char b = j; // compiler error: cannot convert from 'json' to 'char'
^ ~
./src/json.hpp:2995:20: note: candidate template ignored: disabled by 'enable_if' [with ValueType = char]
not std::is_pointer<ValueType>::value
^
./src/json.hpp:2465:15: note: candidate function
constexpr operator value_t() const noexcept
^
char is the character type for string_t and is excluded from the possible values for operator ValueType() const to avoid ambiguities as these types implicitly convert to std::string. I am not sure whether it would make sense to make a change to support this edge case.
While fixing #144, the following comment has been added to the operator ValueType() function:
The character type of string_t as well as an initializer list of this type is excluded to avoid ambiguities as these types implicitly convert to std::string.
I don't think this issue can be fixed without breaking the #144.
For anyone who lands on this page trying to figure out how to print or use something like jsonObject["my_key"] as a char*, here is a demo that shows how:
The j1["name"].get_ref<const std::string&>().c_str() part reads the value out of the "name" key from the j1 json object as a const std::string& reference, and then obtains the const char* C-string from that using .c_str().
Activity
nlohmann commentedon Jul 1, 2016
Thanks for reporting!
I tried the code and got the following error message:
charis the character type forstring_tand is excluded from the possible values foroperator ValueType() constto avoid ambiguities as these types implicitly convert tostd::string. I am not sure whether it would make sense to make a change to support this edge case.nlohmann commentedon Jul 31, 2016
While fixing #144, the following comment has been added to the operator ValueType() function:
I don't think this issue can be fixed without breaking the #144.
theodelrieu commentedon Feb 24, 2017
You can still use the
getmethod thoughtheodelrieu commentedon Apr 12, 2018
Just for the record, this is linked to #958.
ElectricRCAircraftGuy commentedon Jun 4, 2022
For anyone who lands on this page trying to figure out how to print or use something like
jsonObject["my_key"]as achar*, here is a demo that shows how:The
j1["name"].get_ref<const std::string&>().c_str()part reads the value out of the"name"key from thej1json object as aconst std::string&reference, and then obtains theconst char*C-string from that using.c_str().See:
For my full demo of this and more, see json_nlohmann_demo.cpp in my eRCaGuy_hello_world repo.
@theodelrieu , thanks for your comment about the
get()method. That helped me figure out the above code.