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

NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT can not parse { "key" : null} #3458

Closed
1 of 2 tasks
engine5d opened this issue Apr 28, 2022 · 4 comments
Closed
1 of 2 tasks
Labels
solution: proposed fix a fix for the issue has been proposed and waits for confirmation

Comments

@engine5d
Copy link

Description

struct Data{
std::string key;
}

if json likes
{ "key": null }
the NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT to "Data" type occurs exception.

if json like
{ "key" : "" }

it' OK.

Reproduction steps

reproduction ...

Expected vs. actual results

error

Minimal code example

No response

Error messages

No response

Compiler and operating system

win10 vs 2017

Library version

develope

Validation

@nlohmann
Copy link
Owner

I assume the exception complains about the type. null cannot be converted into a std::string automatically. If you want to have some custom behavior in this situation (e.g., null converting to the empty string), then you need to define the conversion manually with user-defined to_json/from_json functions.

See https://json.nlohmann.me/api/macros/nlohmann_define_type_non_intrusive/#examples for examples which code the macros create. You can start from there and add your desired behavior.

@nlohmann nlohmann added solution: proposed fix a fix for the issue has been proposed and waits for confirmation and removed kind: bug labels Apr 28, 2022
@nlohmann
Copy link
Owner

@engine5d Do you need further assistance with this issue?

@engine5d
Copy link
Author

thanks. with user-defined to_json/from_json is ok although it's complicated

@wwwtiger2022
Copy link

It's better add a flag in these templates, or a new template, to allow json null convert to empty string.

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

3 participants