Skip to content

Commit

Permalink
support std::filesystem::path
Browse files Browse the repository at this point in the history
  • Loading branch information
theodelrieu committed Oct 13, 2021
1 parent a12ee7f commit d31b4ed
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 61 deletions.
16 changes: 16 additions & 0 deletions include/nlohmann/detail/conversions/from_json.hpp
Expand Up @@ -19,6 +19,10 @@
#include <nlohmann/detail/meta/type_traits.hpp>
#include <nlohmann/detail/value_t.hpp>

#ifdef JSON_HAS_CPP17
#include <filesystem>
#endif

namespace nlohmann
{
namespace detail
Expand Down Expand Up @@ -444,6 +448,18 @@ void from_json(const BasicJsonType& j, std::unordered_map<Key, Value, Hash, KeyE
}
}

#ifdef JSON_HAS_CPP17
template<typename BasicJsonType>
void from_json(const BasicJsonType& j, std::filesystem::path& p)
{
if (JSON_HEDLEY_UNLIKELY(!j.is_string()))
{
JSON_THROW(type_error::create(302, "type must be string, but is " + std::string(j.type_name()), j));
}
p = *j.template get_ptr<const typename BasicJsonType::string_t*>();
}
#endif

struct from_json_fn
{
template<typename BasicJsonType, typename T>
Expand Down
12 changes: 12 additions & 0 deletions include/nlohmann/detail/conversions/to_json.hpp
Expand Up @@ -14,6 +14,10 @@
#include <nlohmann/detail/meta/type_traits.hpp>
#include <nlohmann/detail/value_t.hpp>

#ifdef JSON_HAS_CPP17
#include <filesystem>
#endif

namespace nlohmann
{
namespace detail
Expand Down Expand Up @@ -386,6 +390,14 @@ void to_json(BasicJsonType& j, const T& t)
to_json_tuple_impl(j, t, make_index_sequence<std::tuple_size<T>::value> {});
}

#ifdef JSON_HAS_CPP17
template<typename BasicJsonType>
void to_json(BasicJsonType& j, const std::filesystem::path& p)
{
j = p.string();
}
#endif

struct to_json_fn
{
template<typename BasicJsonType, typename T>
Expand Down
28 changes: 28 additions & 0 deletions single_include/nlohmann/json.hpp
Expand Up @@ -3944,6 +3944,10 @@ T conditional_static_cast(U value)
// #include <nlohmann/detail/value_t.hpp>


#ifdef JSON_HAS_CPP17
#include <filesystem>
#endif

namespace nlohmann
{
namespace detail
Expand Down Expand Up @@ -4369,6 +4373,18 @@ void from_json(const BasicJsonType& j, std::unordered_map<Key, Value, Hash, KeyE
}
}

#ifdef JSON_HAS_CPP17
template<typename BasicJsonType>
void from_json(const BasicJsonType& j, std::filesystem::path& p)
{
if (JSON_HEDLEY_UNLIKELY(!j.is_string()))
{
JSON_THROW(type_error::create(302, "type must be string, but is " + std::string(j.type_name()), j));
}
p = *j.template get_ptr<const typename BasicJsonType::string_t*>();
}
#endif

struct from_json_fn
{
template<typename BasicJsonType, typename T>
Expand Down Expand Up @@ -4602,6 +4618,10 @@ class tuple_element<N, ::nlohmann::detail::iteration_proxy_value<IteratorType >>
// #include <nlohmann/detail/value_t.hpp>


#ifdef JSON_HAS_CPP17
#include <filesystem>
#endif

namespace nlohmann
{
namespace detail
Expand Down Expand Up @@ -4974,6 +4994,14 @@ void to_json(BasicJsonType& j, const T& t)
to_json_tuple_impl(j, t, make_index_sequence<std::tuple_size<T>::value> {});
}

#ifdef JSON_HAS_CPP17
template<typename BasicJsonType>
void to_json(BasicJsonType& j, const std::filesystem::path& p)
{
j = p.string();
}
#endif

struct to_json_fn
{
template<typename BasicJsonType, typename T>
Expand Down

0 comments on commit d31b4ed

Please sign in to comment.