Skip to content

Commit 61ef548

Browse files
committed
UDT serialization of paths
1 parent 43bfabc commit 61ef548

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

include/boost/json/impl/serializer.hpp

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -767,6 +767,53 @@ write_impl(optional_conversion_tag, writer& w, stream& ss)
767767
return done;
768768
}
769769

770+
template<class T, bool StackEmpty>
771+
BOOST_FORCEINLINE
772+
bool
773+
write_impl(path_conversion_tag, writer& w, stream& ss)
774+
{
775+
#if defined(_MSC_VER)
776+
# pragma warning( push )
777+
# pragma warning( disable : 4127 )
778+
#endif
779+
if(StackEmpty || w.st_.empty())
780+
#if defined(_MSC_VER)
781+
# pragma warning( pop )
782+
#endif
783+
{
784+
BOOST_ASSERT( w.p_ );
785+
T const* pt = reinterpret_cast<T const*>(w.p_);
786+
787+
std::string const s = pt->generic_string();
788+
w.cs0_ = { s.data(), s.size() };
789+
if(BOOST_JSON_LIKELY( write_string(w, ss) ))
790+
return true;
791+
792+
std::size_t const used = w.cs0_.used( s.data() );
793+
w.st_.push( used );
794+
w.st_.push( std::move(s) );
795+
return false;
796+
}
797+
else
798+
{
799+
std::string s;
800+
std::size_t used;
801+
w.st_.pop( s );
802+
w.st_.pop( used );
803+
804+
w.cs0_ = { s.data(), s.size() };
805+
w.cs0_.skip(used);
806+
807+
if(BOOST_JSON_LIKELY( resume_string(w, ss) ))
808+
return true;
809+
810+
used = w.cs0_.used( s.data() );
811+
w.st_.push( used );
812+
w.st_.push( std::move(s) );
813+
return false;
814+
}
815+
}
816+
770817
template<class T, bool StackEmpty>
771818
bool
772819
write_impl(writer& w, stream& ss)

test/serializer.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@
2121
#include <vector>
2222
#include <limits.h>
2323

24+
#ifndef BOOST_NO_CXX17_HDR_FILESYSTEM
25+
#include <filesystem>
26+
#endif // BOOST_NO_CXX17_HDR_FILESYSTEM
27+
2428
#include "parse-vectors.hpp"
2529
#include "test.hpp"
2630
#include "test_suite.hpp"
@@ -866,6 +870,27 @@ class serializer_test
866870
check_udt( o, "2315" );
867871
}
868872
#endif // BOOST_NO_CXX17_HDR_OPTIONAL
873+
#ifndef BOOST_NO_CXX17_HDR_FILESYSTEM
874+
{
875+
std::filesystem::path p = "from/here";
876+
check_udt(p, R"("from/here")");
877+
878+
p = "to/there";
879+
check_udt(p, R"("to/there")");
880+
881+
p = "";
882+
check_udt(p, R"("")");
883+
884+
p = "c:/";
885+
check_udt(p, R"("c:/")");
886+
887+
p = "..";
888+
check_udt(p, R"("..")");
889+
890+
p = "../";
891+
check_udt(p, R"("../")");
892+
}
893+
#endif // BOOST_NO_CXX17_HDR_FILESYSTEM
869894
}
870895

871896
void

0 commit comments

Comments
 (0)