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

.value() with optional default value fails to compile #3859

Open
audaki opened this issue Dec 2, 2022 · 7 comments
Open

.value() with optional default value fails to compile #3859

audaki opened this issue Dec 2, 2022 · 7 comments
Labels
confirmed kind: bug solution: proposed fix a fix for the issue has been proposed and waits for confirmation

Comments

@audaki
Copy link

audaki commented Dec 2, 2022

Description

We upgraded from 3.10.5 to 3.11.2

Since the Upgrade we can't compile value() with optional fallback value anymore

Reproduction steps

Try to compile value with std::optional<std::string>{} as fallback. Is this still possible at all, was I using a non-supported edge-case before?

Expected vs. actual results

Compiles vs not

Minimal code example

Beforehand we said:

std::optional<std::string> customer_id;
customer_id = json.value("customer_id", std::optional<std::string>{});

This no longer compiles. Even without the assignment and I tried different versions, all fail to compile:

json.value("customer_id", std::optional<std::string>{});
json.value<std::optional<std::string>>("customer_id", std::optional<std::string>{});


### Error messages

_No response_

### Compiler and operating system

Ubuntu 22.04, Clang 14

### Library version

3.11.2

### Validation

- (haven't tested this) [ ] The bug also occurs if the latest version from the [`develop`](https://github.com/nlohmann/json/tree/develop) branch is used.
- [X] I can successfully [compile and run the unit tests](https://github.com/nlohmann/json#execute-unit-tests). **All passed**
@gregmarr
Copy link
Contributor

gregmarr commented Dec 2, 2022

Looks like this type is failing the detail::is_getable<> test because get<> doesn't work for it. The get<> function didn't work in 3.10.5 either, so the check on value() is now stricter than it was before.

@theodelrieu This came from 74b446f Any thoughts?

@audaki
Copy link
Author

audaki commented Dec 2, 2022

According to your answer it seems like it was never intended to work.

Therefore we solved it with a lambda for now:

    auto json_to_opt_string = [](const nlohmann::json& v) -> std::optional<std::string> {
      if (v.empty())
        return {};

      return v.get<std::string>();
    };

Is there maybe another intended way in the library for this pattern that I overlooked in the documentation?

@audaki
Copy link
Author

audaki commented Dec 2, 2022

PS: Maybe add a get_opt member function that is like get but wraps every type in optional?

@gregmarr
Copy link
Contributor

gregmarr commented Dec 5, 2022

I honestly don't know if it was intended or not. I was just looking at why it wasn't working now. I know we've had various requests for and PRs about better std::optional handling.

@sgrzeszc
Copy link

Bumping this up. This issue has hit me hard when upgrading the version of this library from 2.x to 3.x in a project I'm trying to resurrect.

@Xtreme-G
Copy link

Xtreme-G commented Oct 18, 2024

I too ran into this problem and would find it convenient to use value with an optional fallback instead of checking contains and parse into the std::optional, which is also more error prone.

@nlohmann
Copy link
Owner

The issue is still there even after merging #4036.

theodelrieu pushed a commit to theodelrieu/json that referenced this issue Dec 2, 2024
theodelrieu pushed a commit to theodelrieu/json that referenced this issue Dec 2, 2024
theodelrieu pushed a commit to theodelrieu/json that referenced this issue Dec 2, 2024
@nlohmann nlohmann linked a pull request Dec 2, 2024 that will close this issue
@nlohmann nlohmann added the solution: proposed fix a fix for the issue has been proposed and waits for confirmation label Dec 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed kind: bug solution: proposed fix a fix for the issue has been proposed and waits for confirmation
Projects
None yet
Development

No branches or pull requests

5 participants