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

Cannot convert from 'json' to 'char' #276

Closed
DPD85 opened this issue Jul 1, 2016 · 5 comments
Closed

Cannot convert from 'json' to 'char' #276

DPD85 opened this issue Jul 1, 2016 · 5 comments
Labels
confirmed solution: wontfix the issue will not be fixed (either it is impossible or deemed out of scope) state: help needed the issue needs help to proceed

Comments

@DPD85
Copy link

DPD85 commented Jul 1, 2016

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_integer
    char b = j; // compiler error: cannot convert from 'json' to 'char'
@nlohmann
Copy link
Owner

nlohmann commented Jul 1, 2016

Thanks for reporting!

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.

@nlohmann nlohmann added the state: please discuss please discuss the issue or vote for your favorite option label Jul 5, 2016
@nlohmann nlohmann added state: help needed the issue needs help to proceed and removed state: please discuss please discuss the issue or vote for your favorite option labels Jul 26, 2016
@nlohmann
Copy link
Owner

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.

@nlohmann nlohmann added the solution: wontfix the issue will not be fixed (either it is impossible or deemed out of scope) label Jul 31, 2016
@nlohmann nlohmann closed this as completed Nov 2, 2016
@theodelrieu
Copy link
Contributor

You can still use the get method though

@theodelrieu
Copy link
Contributor

Just for the record, this is linked to #958.

@ElectricRCAircraftGuy
Copy link

ElectricRCAircraftGuy commented 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 a char*, here is a demo that shows how:

printf("name:   %s\n", j1["name"].get_ref<const std::string&>().c_str());

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().

See:

  1. https://en.cppreference.com/w/cpp/string/basic_string/c_str
  2. https://json.nlohmann.me/api/basic_json/get_ref/

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed solution: wontfix the issue will not be fixed (either it is impossible or deemed out of scope) state: help needed the issue needs help to proceed
Projects
None yet
Development

No branches or pull requests

4 participants