Skip to content

Commit

Permalink
✨ added overload for std::vector<bool> #494
Browse files Browse the repository at this point in the history
Adds a to_json function for std::vector<bool> to allow implicit
conversion from bit vectors to basic_json.
  • Loading branch information
nlohmann committed Mar 11, 2017
1 parent 758c4ad commit f5f6dac
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,19 @@ struct external_constructor<value_t::array>
j.m_value.array = j.template create<typename BasicJsonType::array_t>(begin(arr), end(arr));
j.assert_invariant();
}

This comment has been minimized.

Copy link
@nlohmann

nlohmann Mar 11, 2017

Author Owner

@theodelrieu This is the way to implement this, right?

This comment has been minimized.

Copy link
@theodelrieu

theodelrieu Mar 11, 2017

Contributor

Yes, that seems correct!

template<typename BasicJsonType>
static void construct(BasicJsonType& j, const std::vector<bool>& arr)
{
j.m_type = value_t::array;
j.m_value = value_t::array;
j.m_value.array->reserve(arr.size());
for (bool x : arr)
{
j.m_value.array->push_back(x);
}
j.assert_invariant();
}
};

template<>
Expand Down Expand Up @@ -562,6 +575,12 @@ void to_json(BasicJsonType& j, UnscopedEnumType e) noexcept
external_constructor<value_t::number_integer>::construct(j, e);
}

template<typename BasicJsonType>
void to_json(BasicJsonType& j, std::vector<bool> e) noexcept

This comment has been minimized.

Copy link
@theodelrieu

theodelrieu Mar 11, 2017

Contributor

missing const& for e

This comment has been minimized.

Copy link
@nlohmann

nlohmann Mar 11, 2017

Author Owner

Good point, thanks!

This comment has been minimized.

Copy link
@theodelrieu

theodelrieu Mar 11, 2017

Contributor

Also, i don't know if noexcept is correct here, since it allocates memory by using create internally.
IIRC, I only put noexcept on integer types and so on, and only in some to_json methods

This comment has been minimized.

Copy link
@nlohmann

nlohmann Mar 11, 2017

Author Owner

You are right - I just copied the line from the bool-to_json function.

This comment has been minimized.

Copy link
@nlohmann

nlohmann Mar 11, 2017

Author Owner

I fixed this in d9e2dd0.

{
external_constructor<value_t::array>::construct(j, e);
}

template <
typename BasicJsonType, typename CompatibleArrayType,
enable_if_t <
Expand Down
19 changes: 19 additions & 0 deletions src/json.hpp.re2c
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,19 @@ struct external_constructor<value_t::array>
j.m_value.array = j.template create<typename BasicJsonType::array_t>(begin(arr), end(arr));
j.assert_invariant();
}

template<typename BasicJsonType>
static void construct(BasicJsonType& j, const std::vector<bool>& arr)
{
j.m_type = value_t::array;
j.m_value = value_t::array;
j.m_value.array->reserve(arr.size());
for (bool x : arr)
{
j.m_value.array->push_back(x);
}
j.assert_invariant();
}
};

template<>
Expand Down Expand Up @@ -562,6 +575,12 @@ void to_json(BasicJsonType& j, UnscopedEnumType e) noexcept
external_constructor<value_t::number_integer>::construct(j, e);
}

template<typename BasicJsonType>
void to_json(BasicJsonType& j, std::vector<bool> e) noexcept
{
external_constructor<value_t::array>::construct(j, e);
}

template <
typename BasicJsonType, typename CompatibleArrayType,
enable_if_t <
Expand Down
9 changes: 9 additions & 0 deletions test/src/unit-regression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -795,4 +795,13 @@ TEST_CASE("regression tests")
std::string s2 = j2.dump();
CHECK(s1 == s2);
}

SECTION("issue #494 - conversion from vector<bool> to json fails to build")
{
std::vector<bool> boolVector = {false, true, false, false};
json j;
j["bool_vector"] = boolVector;

CHECK(j["bool_vector"].dump() == "[false,true,false,false]");
}
}

0 comments on commit f5f6dac

Please sign in to comment.