boostorg / json

A C++11 library for parsing and serializing JSON to and from a DOM container in memory.
https://boost.org/libs/json
Boost Software License 1.0
432 stars 94 forks source link

Unable to extract boolean value in numberic format #987

Closed k-nero closed 7 months ago

k-nero commented 7 months ago

Version of Boost 1.84.0

Code:

class X
{
public:
    X() : x(false) {}
    X(bool x) : x(x) {}
protected:
    bool x;
    BOOST_DESCRIBE_CLASS(X, (), (), (x), ())
};

int main()
{
    try
    {
        boost::json::value jv = { { "x", 0 } };
        auto a = boost::json::value_to<X>(jv);
    }
    catch (std::exception const& e)
    {
        std::cerr << "Error: " << e.what() << std::endl;
    }
}

This would faild and throw an exception: Error: value is not boolean

grisumbras commented 7 months ago

Yes. This is by design. 0 is not a boolean value.