Skip to content

overwrite adl_serializer<bool, void> to change behaviour #1638

Description

@Virtual-felix

I would like to change the behaviour of how boolean are serialized, so to_json would transform boolean to string.

I tried to do an adl_serializer specialized for bolean like this


namespace nlohmann {

template<>
struct adl_serializer<bool> {
  static void to_json(json& j, bool b) {
    j = std::string(b ? "true" : "false");
  }

  static void from_json(const json& j, bool& b) {
    b = j.template get<std::string>() == "true";
  }
};

} // namespace nlohmann

Knowing that I tried to do

b = *j.get_ptr<const json::boolean_t*>()
but it was returning a null ptr (no idea wny the bool is stocked as a string)

The fact is that this try compile and work with Clang but does not compile with GCC.
GCC output:

error: partial specialization of ‘struct nlohmann::adl_serializer<T, typename std::enable_if<std::is_same<bool, _U1>::value, void>::type>’ after instantiation of ‘struct nlohmann::adl_serializer<bool, void>’ [-fpermissive]
struct adl_serializer<T, std::enable_if_t<std::is_same<bool, T>::value>> {

But I can't understand where this instantiation happen and why it happens with GCC and not Clang.
I was wondering if there is another way to change the behaviour of a boolean serialization if it is possible.

I'm using the tag 3.5 of the library.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions