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

Fix problems in PR2117 #2213

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,17 @@ matrix:
sources: ['ubuntu-toolchain-r-test']
packages: ['g++-9', 'ninja-build']

- os: linux
compiler: gcc
env:
- COMPILER=g++-9
- CXXFLAGS=-std=c++17
- CMAKECXXSTANDARD=17
dota17 marked this conversation as resolved.
Show resolved Hide resolved
addons:
apt:
sources: ['ubuntu-toolchain-r-test']
packages: ['g++-9', 'ninja-build']

# Linux / Clang

- os: linux
Expand Down Expand Up @@ -328,7 +339,11 @@ script:

# compile and execute unit tests
- mkdir -p build && cd build
- cmake .. ${CMAKE_OPTIONS} -DJSON_MultipleHeaders=${MULTIPLE_HEADERS} -GNinja && cmake --build . --config Release
- if [[ "${CMAKECXXSTANDARD}" != "" ]]; then
cmake .. ${CMAKE_OPTIONS} -DJSON_MultipleHeaders=${MULTIPLE_HEADERS} -DCMAKE_CXX_STANDARD=${CMAKECXXSTANDARD} -GNinja && cmake --build . --config Release ;
else
cmake .. ${CMAKE_OPTIONS} -DJSON_MultipleHeaders=${MULTIPLE_HEADERS} -GNinja && cmake --build . --config Release ;
fi
- ctest -C Release --timeout 2700 -V -j
- cd ..

Expand Down
9 changes: 6 additions & 3 deletions test/src/unit-conversions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1710,7 +1710,8 @@ TEST_CASE("std::optional")
std::optional<std::string> opt_null;

CHECK(json(opt_null) == j_null);
CHECK(std::optional<std::string>(j_null) == std::nullopt);
CHECK_THROWS_WITH(std::optional<std::string>(j_null) == std::nullopt,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is null not converted to a nullopt here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See 2.b in my comments.

I agree with this view:

The reason is the template optional(U&&) constructor template, which is supposed to activate when T (int) is constructible from U and U is neither std::in_place_t nor optional, and direct-initialize T from it. And so it does, stamping out optional(foo&).

j_null is basic_json, and there is a way that basic_json can be converted to string by calling void from_json(const BasicJsonType& j, typename BasicJsonType::string_t& s)

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This may be a technical explanation, but as I user, when I ask to translate a JSON value to a std::optional<std::string>, then I expect this behavior:

  • string values are stored as value,
  • null is stored as std::nullopt, and
  • all other values throw

At least this is what the code

#ifdef JSON_HAS_CPP_17
template<typename BasicJsonType, typename T>
void from_json(const BasicJsonType& j, std::optional<T>& opt)
{
    if (j.is_null())
    {
        opt = std::nullopt;
    }
    else
    {
        opt = j.template get<T>();
    }
}
#endif

tries to accomplish.

"[json.exception.type_error.302] type must be string, but is null");
}

SECTION("string")
Expand Down Expand Up @@ -1746,7 +1747,8 @@ TEST_CASE("std::optional")
std::vector<std::optional<int>> opt_array = {{1, 2, std::nullopt}};

CHECK(json(opt_array) == j_array);
CHECK(std::vector<std::optional<int>>(j_array) == opt_array);
std::vector<std::optional<int>> tmp = j_array;
CHECK(tmp == opt_array);
}

SECTION("object")
Expand All @@ -1755,7 +1757,8 @@ TEST_CASE("std::optional")
std::map<std::string, std::optional<int>> opt_object {{"one", 1}, {"two", 2}, {"zero", std::nullopt}};

CHECK(json(opt_object) == j_object);
CHECK(std::map<std::string, std::optional<int>>(j_object) == opt_object);
std::map<std::string, std::optional<int>> tmp =j_object;
CHECK(tmp == opt_object);
}
}
#endif