USCiLab / cereal

A C++11 library for serialization
BSD 3-Clause "New" or "Revised" License
4.24k stars 764 forks source link

std::wstring #309

Open TylerTheFox opened 8 years ago

TylerTheFox commented 8 years ago

Can you use serialize wide strings? (http://www.cplusplus.com/reference/string/wstring/)

1>cereal\inc\cereal/archives/json.hpp(879): error C2664: 'void cereal::JSONOutputArchive::saveValue(unsigned long)': cannot convert argument 1 from 'const std::basic_string<wchar_t,std::char_traits,std::allocator>' to 'bool' (compiling source file .cpp) 1> cereal\inc\cereal/archives/json.hpp(879): note: No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called (compiling source file .cpp) 1> cereal\inc\cereal/cereal.hpp(410): note: see reference to function template instantiation 'void cereal::save<wchar_t,std::char_traits,std::allocator>(cereal::JSONOutputArchive &,const std::basic_string<wchar_t,std::char_traits,std::allocator> &)' being compiled (compiling source file .cpp) 1> cereal\inc\cereal/cereal.hpp(333): note: see reference to function template instantiation 'ArchiveType &cereal::OutputArchive<ArchiveType,0>::processImplstd::wstring,0(const T &)' being compiled

AzothAmmo commented 8 years ago

This is not yet supported, see #95

tristanl-slalom commented 8 years ago

Is there a workaround? I'm trying to integrate cereal with legacy code, and they use wstring for everything.

TylerTheFox commented 8 years ago

std::wstring only works in the binary output/input archive. That was how I worked around it.

zhuebok commented 6 years ago

Next code allows to solve this problem (but it would work for std::wstring only, not for std::basic_string<wchar_t, custom...>):

namespace cereal
{
    template<class A>
    std::string CEREAL_SAVE_MINIMAL_FUNCTION_NAME( A const &, const std::wstring &in )
    {
        return boost::locale::conv::utf_to_utf<char>( in );
    }

    template<class A>
    void CEREAL_LOAD_MINIMAL_FUNCTION_NAME( A const &, std::wstring &out, const std::string &in )
    {
        out = boost::locale::conv::utf_to_utf<wchar_t>( in );
    }
}

CEREAL_SPECIALIZE_FOR_ALL_ARCHIVES( std::wstring, cereal::specialization::non_member_load_save_minimal );