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

Empty std::tuple<> cannot be serialized and de-serialized #4530

Closed
2 tasks done
moratom opened this issue Dec 6, 2024 · 2 comments · Fixed by #4594
Closed
2 tasks done

Empty std::tuple<> cannot be serialized and de-serialized #4530

moratom opened this issue Dec 6, 2024 · 2 comments · Fixed by #4594
Labels
kind: bug solution: proposed fix a fix for the issue has been proposed and waits for confirmation

Comments

@moratom
Copy link

moratom commented Dec 6, 2024

Description

The behavior of serializing and de-sereailzing an empty tuple changed from 3.9.1 to 3.10.0.

null used to de-serialize without a problem to std::tuple<> but since throws an error since 3.10.0.

The change of behavior happened in #2576.

Note that I'm not entirely sure this is a bug per-say, but we relied on this behavior in https://github.com/luxonis/depthai-core so I decided to report in case the previous behavior was "more correct".

Reproduction steps

To reproduce run the MRE with both 3.10.0 and 3.9.1.

Expected vs. actual results

Expected - null succesfully de-serializes when j.get<std::tuple<>>() is called on null.
Actual:

terminate called after throwing an instance of 'nlohmann::detail::type_error'
  what():  [json.exception.type_error.302] type must be array, but is null

Minimal code example

#include <nlohmann/json.hpp>

int main() {
    auto sourceTuple = std::tuple<>();
    nlohmann::json j = sourceTuple;
    auto returnedTuple = j.get<std::tuple<>>();
    return 0;
}

Error messages

terminate called after throwing an instance of 'nlohmann::detail::type_error'
  what():  [json.exception.type_error.302] type must be array, but is null
Aborted (core dumped)

Compiler and operating system

Ubuntu 20.04, GCC 9.4.0

Library version

3.11.3

Validation

@codenut
Copy link
Contributor

codenut commented Jan 12, 2025

This bug occurs because an empty tuple std::tuple<>() is being serialized as "null" instead of "[]".

Current behavior

nlohmann::json j = sourceTuple;  
auto js = j.dump(); // "null"

Expected behavior

nlohmann::json j = sourceTuple;  
auto js = j.dump(); // "[]"

@nlohmann I will work on a fix for this.

@nlohmann nlohmann added the solution: proposed fix a fix for the issue has been proposed and waits for confirmation label Jan 18, 2025
@nlohmann nlohmann added this to the Release 3.11.4 milestone Jan 18, 2025
@nlohmann
Copy link
Owner

As discussed in #4594 (comment), the expected behavior for an empty tuple is to be serialized to [] and from there back to an empty tuple. The expectation to deserialize null to an empty tuple is wrong, as other types such as strings or numbers also do not deserialize from null to any value.

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
Development

Successfully merging a pull request may close this issue.

3 participants