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

assigning nullptr to std::string #2118

Closed
ghost opened this issue May 17, 2020 · 2 comments
Closed

assigning nullptr to std::string #2118

ghost opened this issue May 17, 2020 · 2 comments
Labels
kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation

Comments

@ghost
Copy link

ghost commented May 17, 2020

Question

According to the document for "JSON for Modern C++", when the variable type is "std::nullptr_t", it returns "null".

It's a silly question, but if assigning "nullptr" to "std::string" like "Example", which one is intended as the specification?

(1) it causes the segmentation fault by undefined behavior of std::string
(2) it returns "null" as same as "std::nullptr_t"

It seems that (1) is the specification: it's undesired to assign "nullptr" to "std::string".
Is it correct about my understanding?

[Example]

#include <iostream>
#include <nlohmann/json.hpp>

int main() {
    nlohmann::json j;
    std::string value = nullptr;
    j[ "value" ] = value;
    std::cout << j << std::endl;

    ...
}

Version

  • JSON for Modern C++ 3.7.3
  • Linux gcc 8.3.0
@ghost ghost added the kind: question label May 17, 2020
@nlohmann
Copy link
Owner

I do not fully understand the issue. If you want to store the JSON value null at key "value", then it's sufficient to write

j[ "value" ] = nullptr;

There is no need to have a std::string involved.

@ghost
Copy link
Author

ghost commented May 18, 2020

Thank you for the polite answer to store the JSON value "null" . Please close this question.

@nlohmann nlohmann added the solution: proposed fix a fix for the issue has been proposed and waits for confirmation label May 18, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation
Projects
None yet
Development

No branches or pull requests

1 participant