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

Copying ctor and moving ctor null ordered_json corrupts it (mingw 8) #4164

Open
2 tasks
matejk opened this issue Sep 25, 2023 · 2 comments
Open
2 tasks

Copying ctor and moving ctor null ordered_json corrupts it (mingw 8) #4164

matejk opened this issue Sep 25, 2023 · 2 comments
Labels
solution: duplicate the issue is a duplicate; refer to the linked issue instead state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated

Comments

@matejk
Copy link

matejk commented Sep 25, 2023

Description

Creating a copy of null JSON with copy ctor or moving it with move ctor corrupts it.

Type turns into array and that array contains one null element.

Null JSON is properly copied when using assignment operator.

I use the library from "include.zip".

Reproduction steps

See example below.

Expected vs. actual results

Copied null JSON shall still be null JSON and does not turn into something different.

Minimal code example

// A google test function demonstrating the problem.
TEST(Parameters, JsonCopyCtor)
{
    nlohmann::ordered_json null_json;

    std::cout << null_json.dump() << std::endl;
    EXPECT_TRUE(null_json.is_null());

    const auto json_copy { null_json };

    std::cout << json_copy.dump() << std::endl;
    EXPECT_TRUE(json_copy.is_null());
    EXPECT_FALSE(json_copy.is_array());

    const auto json_copy_2 = null_json ;

    std::cout << json_copy_2.dump() << std::endl;
    EXPECT_TRUE(json_copy_2.is_null());
    EXPECT_FALSE(json_copy_2.is_array());

    const auto json_copy_3 { std::move(null_json) } ;

    std::cout << json_copy_3.dump() << std::endl;
    EXPECT_TRUE(json_copy_3.is_null());
    EXPECT_FALSE(json_copy_3.is_array());
}

Error messages

Output from Google test:

[ RUN      ] Parameters.JsonCopyCtor
null
[null]
C:/Users/m.kenda/workspace/ConfigSerializer.cpp:78: Failure
Value of: json_copy.is_null()
  Actual: false
Expected: true
C:/Users/m.kenda/workspace/ConfigSerializer.cpp:79: Failure
Value of: json_copy.is_array()
  Actual: true
Expected: false
null
[null]
C:/Users/m.kenda/workspace/ConfigSerializer.cpp:90: Failure
Value of: json_copy_3.is_null()
  Actual: false
Expected: true
C:/Users/m.kenda/workspace/ConfigSerializer.cpp:91: Failure
Value of: json_copy_3.is_array()
  Actual: true
Expected: false
[  FAILED  ] Parameters.JsonCopyCtor (0 ms)

Compiler and operating system

MinGW 8.1, Windows 10

Library version

3.11.2

Validation

@nlohmann
Copy link
Owner

This is a know issue: compilers treat {} differently. With

const auto json_copy { null_json };

you create an array.

Use

const auto json_copy_2 = null_json ;

instead.

@nlohmann nlohmann added solution: duplicate the issue is a duplicate; refer to the linked issue instead and removed kind: bug labels Sep 25, 2023
Copy link

github-actions bot commented Feb 1, 2025

This issue has been marked as stale because it has been open for 90 days without activity. If this issue is still relevant, please add a comment or remove the "stale" label. Otherwise, it will be closed in 10 days. Thank you for helping us prioritize our work!

@github-actions github-actions bot added the state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated label Feb 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
solution: duplicate the issue is a duplicate; refer to the linked issue instead state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated
Projects
None yet
Development

No branches or pull requests

2 participants