Skip to content

Commit 68825f1

Browse files
committed
support std::filesystem::path
Fixes #3070
1 parent f8e078e commit 68825f1

File tree

4 files changed

+134
-61
lines changed

4 files changed

+134
-61
lines changed

include/nlohmann/detail/conversions/from_json.hpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919
#include <nlohmann/detail/meta/type_traits.hpp>
2020
#include <nlohmann/detail/value_t.hpp>
2121

22+
#ifdef JSON_HAS_CPP_17
23+
#include <filesystem>
24+
#endif
25+
2226
namespace nlohmann
2327
{
2428
namespace detail
@@ -444,6 +448,18 @@ void from_json(const BasicJsonType& j, std::unordered_map<Key, Value, Hash, KeyE
444448
}
445449
}
446450

451+
#ifdef JSON_HAS_CPP_17
452+
template<typename BasicJsonType>
453+
void from_json(const BasicJsonType& j, std::filesystem::path& p)
454+
{
455+
if (JSON_HEDLEY_UNLIKELY(!j.is_string()))
456+
{
457+
JSON_THROW(type_error::create(302, "type must be string, but is " + std::string(j.type_name()), j));
458+
}
459+
p = *j.template get_ptr<const typename BasicJsonType::string_t*>();
460+
}
461+
#endif
462+
447463
struct from_json_fn
448464
{
449465
template<typename BasicJsonType, typename T>

include/nlohmann/detail/conversions/to_json.hpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515
#include <nlohmann/detail/meta/type_traits.hpp>
1616
#include <nlohmann/detail/value_t.hpp>
1717

18+
#ifdef JSON_HAS_CPP_17
19+
#include <filesystem>
20+
#endif
21+
1822
namespace nlohmann
1923
{
2024
namespace detail
@@ -387,6 +391,14 @@ void to_json(BasicJsonType& j, const T& t)
387391
to_json_tuple_impl(j, t, make_index_sequence<std::tuple_size<T>::value> {});
388392
}
389393

394+
#ifdef JSON_HAS_CPP_17
395+
template<typename BasicJsonType>
396+
void to_json(BasicJsonType& j, const std::filesystem::path& p)
397+
{
398+
j = p.string();
399+
}
400+
#endif
401+
390402
struct to_json_fn
391403
{
392404
template<typename BasicJsonType, typename T>

single_include/nlohmann/json.hpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3950,6 +3950,10 @@ T conditional_static_cast(U value)
39503950
// #include <nlohmann/detail/value_t.hpp>
39513951

39523952

3953+
#ifdef JSON_HAS_CPP_17
3954+
#include <filesystem>
3955+
#endif
3956+
39533957
namespace nlohmann
39543958
{
39553959
namespace detail
@@ -4375,6 +4379,18 @@ void from_json(const BasicJsonType& j, std::unordered_map<Key, Value, Hash, KeyE
43754379
}
43764380
}
43774381

4382+
#ifdef JSON_HAS_CPP_17
4383+
template<typename BasicJsonType>
4384+
void from_json(const BasicJsonType& j, std::filesystem::path& p)
4385+
{
4386+
if (JSON_HEDLEY_UNLIKELY(!j.is_string()))
4387+
{
4388+
JSON_THROW(type_error::create(302, "type must be string, but is " + std::string(j.type_name()), j));
4389+
}
4390+
p = *j.template get_ptr<const typename BasicJsonType::string_t*>();
4391+
}
4392+
#endif
4393+
43784394
struct from_json_fn
43794395
{
43804396
template<typename BasicJsonType, typename T>
@@ -4610,6 +4626,10 @@ class tuple_element<N, ::nlohmann::detail::iteration_proxy_value<IteratorType >>
46104626
// #include <nlohmann/detail/value_t.hpp>
46114627

46124628

4629+
#ifdef JSON_HAS_CPP_17
4630+
#include <filesystem>
4631+
#endif
4632+
46134633
namespace nlohmann
46144634
{
46154635
namespace detail
@@ -4982,6 +5002,14 @@ void to_json(BasicJsonType& j, const T& t)
49825002
to_json_tuple_impl(j, t, make_index_sequence<std::tuple_size<T>::value> {});
49835003
}
49845004

5005+
#ifdef JSON_HAS_CPP_17
5006+
template<typename BasicJsonType>
5007+
void to_json(BasicJsonType& j, const std::filesystem::path& p)
5008+
{
5009+
j = p.string();
5010+
}
5011+
#endif
5012+
49855013
struct to_json_fn
49865014
{
49875015
template<typename BasicJsonType, typename T>

0 commit comments

Comments
 (0)