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_INTRUSIVE_WITH_DEFAULT throw an exception with an empty json #4507

Open
1 of 2 tasks
NissimBendanan opened this issue Nov 21, 2024 · 2 comments · May be fixed by #4323 or #4508
Open
1 of 2 tasks

NLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_DEFAULT throw an exception with an empty json #4507

NissimBendanan opened this issue Nov 21, 2024 · 2 comments · May be fixed by #4323 or #4508
Labels
kind: bug solution: proposed fix a fix for the issue has been proposed and waits for confirmation

Comments

@NissimBendanan
Copy link

Description

NLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_DEFAULT should not throw an exception when the json instance is empty and should use the default values of the member fields

Reproduction steps

run the Minimal code example and get the exception

Expected vs. actual results

don't expect to get exception with an empty JSON
expect that foo member fields are filled with they default values

Minimal code example

class Foo {
 public:
  NLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_DEFAULT(Foo, a, b)

  Foo() = default;

  int a{10};
  std::string b{"bar"};

  void log() {
    std::cout << "a: " << a << std::endl;
    std::cout << "b: " << b << std::endl;
  }
};

// with an empty json, an exception is thrown instead of getting default values
static void JsonTest_empty_json() {
  nlohmann::json json{};
  auto foo = Foo(json);
  foo.log();
}

int main(int argc, char **argv) {
  JsonTest_empty_json();
  return 0;
}

Error messages

No response

Compiler and operating system

clang++ / C++17 / Windows OS

Library version

ref: v3.11.2 - commit: 7d2f9ad500cf062394040f44d006791ff7cfc1c1

Validation

@nlohmann
Copy link
Owner

I think there is already an issue for this.

@nlohmann nlohmann linked a pull request Nov 21, 2024 that will close this issue
4 tasks
@nlohmann
Copy link
Owner

nlohmann commented Nov 21, 2024

It was not an issue, but a PR: #4323. @NissimBendanan can you check if that PR would be helping you?

@nlohmann nlohmann added the solution: proposed fix a fix for the issue has been proposed and waits for confirmation label Nov 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: bug solution: proposed fix a fix for the issue has been proposed and waits for confirmation
Projects
None yet
2 participants