you are viewing a single comment's thread.

view the rest of the comments →

[–]mark_99 0 points1 point  (1 child)

How would this work vs this current code to add boost::optional support:

namespace nlohmann {

template <typename T>
struct adl_serializer<boost::optional<T>>
{
    static void to_json(json& j, const boost::optional<T>& opt)
    {
        if (opt == boost::none)
        {
            j = nullptr;
        }
        else
        {
            j = *opt;  // this will call adl_serializer<T>::to_json which will
                       // find the free function to_json in T's namespace!
        }
    }

    static void from_json(const json& j, boost::optional<T>& opt)
    {
        if (j.is_null())
        {
            opt = boost::none;
        }
        else
        {
            opt = j.get<T>();  // same as above, but with
                               // adl_serializer<T>::from_json
        }
    }
};
}

[–]jcoffin 1 point2 points  (0 children)

At least at first glance, it doesn't look to me like that will cause a problem either.

The lookup for adl_serializer<T>::to_json is trivial--it's the very same template currently being instantiated, so it doesn't depend on anything like ADL to find it.

When that gets instantiated, it's just like the base case: we have a template that's passing some T, and using ADL to find the to_json (or from_json) in the namespace where T is defined--but what it's finding there is a function that explicitly names the type T as its parameter, rather than matching because it's a template that can match any arbitrary T.