Skip to content

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

Closed

Description

@DPD85

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'

Activity

  1. nlohmann commented on Jul 1, 2016

    @nlohmann
    Owner

    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.

  2. added
    state: please discuss
    on Jul 5, 2016
  3. added
    state: help needed
    and removed
    state: please discuss
    on Jul 26, 2016
  4. nlohmann commented on Jul 31, 2016

    @nlohmann
    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.

  5. added
    solution: wontfix
    on Jul 31, 2016
  6. theodelrieu commented on Feb 24, 2017

    @theodelrieu
    Contributor

    You can still use the get method though

  7. theodelrieu commented on Apr 12, 2018

    @theodelrieu
    Contributor

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

  8. ElectricRCAircraftGuy commented on Jun 4, 2022

    @ElectricRCAircraftGuy

    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    confirmedsolution: wontfixstate: help needed

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions