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

Implicit conversion causes "cannot use operator[] with a string argument with string" #2098

Closed
tawmoto opened this issue May 9, 2020 · 2 comments
Labels
solution: proposed fix a fix for the issue has been proposed and waits for confirmation

Comments

@tawmoto
Copy link

tawmoto commented May 9, 2020

  • What is the issue you have?
    I get an exception when I do an implicit conversion and then read a value.
    I read here that json = string should work.

  • Please describe the steps to reproduce the issue. Can you provide a small but working code example?

using json = nlohmann::json;
string str = R"({"happy": true,"pi": 3.141})";

// if I replace it with "json js = json::parse(str.c_str());", it works!		
json js = str;

// this returns ""[json.exception.type_error.305] cannot use operator[] with a string argument with string"
auto v1 = js["happy"].get<bool>();
  • Which compiler and operating system are you using? Is it a supported compiler?
    VS2017

  • Did you use a released version of the library or the version from the develop branch?
    develop, tried also the latest release and happens also

Maybe I am doing something wrong? I am evaluating this project to replace cjson and I hope that I will make the switch. 👍
Thank you

@nlohmann
Copy link
Owner

nlohmann commented May 9, 2020

By calling json js = str;, you are not parsing the string to a JSON value, but create a JSON value that holds that string. For that reason, calling js["happy"] makes no sense, as js is a string and not an object, hence the exception.

The README says that you can do the following:

// create a JSON value from a string
json j = "Hello";

// create a string from a JSON value
string s = j;

@nlohmann nlohmann added solution: proposed fix a fix for the issue has been proposed and waits for confirmation and removed kind: bug labels May 9, 2020
@tawmoto
Copy link
Author

tawmoto commented May 9, 2020

I see, so I will just use the "parse()" function then.
Thank you

@tawmoto tawmoto closed this as completed May 9, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
solution: proposed fix a fix for the issue has been proposed and waits for confirmation
Projects
None yet
Development

No branches or pull requests

2 participants