You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
dump() method doesn't work with a custom allocator #268
Even if #161 isn't fixed, the dump() method still assumes the default allocator is used. Consider the following code:
nlohmann::basic_json<std::map, std::vector, StringWithMyAllocator, bool, int64_t, uint64_t, double, MyAllocator> j;
// add a string that is stored as std::string
j["name"] = "Niels";
// explicit conversion to string
StringWithMyAllocator s = j.dump(); // {\"happy\":true,\"pi\":3.141}
This results in a compiler error:
'return': cannot convert from 'std::basic_string<char,std::char_traits,std::allocator>' to 'std::basic_string<char,std::char_traits,MyAllocator>'
The issue is due to the stringstream that is used without specifying the allocator for the stream. As a result, dump fails to compile due to the call to ss.str().
I actually never tried to use custom allocators in the string type. Your example shows that there is an apparent disconnect between the (in theory) adjustable basic_json::string_t and the (implicitly assumed) std::string returned by str().
The quick "fix" would be to replace the return type of dump from basic_json::string_t to std::string, because the type used to store strings inside a JSON value does not necessarily need to be the same type of a serialization.
Alternatively, the user-defined allocator inside basic_json::string_t would need to be passed down into the string stream.
I created a fork of the project and a new branch feature/custom-allocator that contains initial changes. There are still a lot of assumptions built into the code using the default allocator for std::vector, std::map, and std::string.
Activity
nlohmann commentedon Jun 22, 2016
I actually never tried to use custom allocators in the string type. Your example shows that there is an apparent disconnect between the (in theory) adjustable
basic_json::string_tand the (implicitly assumed)std::stringreturned bystr().The quick "fix" would be to replace the return type of
dumpfrombasic_json::string_ttostd::string, because the type used to store strings inside a JSON value does not necessarily need to be the same type of a serialization.Alternatively, the user-defined allocator inside
basic_json::string_twould need to be passed down into the string stream.addressing the compiler error from #268
nlohmann commentedon Jun 22, 2016
I created a branch
feature/issue268(see a834338) which fixes the compilation error. Please let me know whether this works for you.nlohmann commentedon Jun 23, 2016
Thanks for checking back, and I am looking forward to see (the definitely needed) fixes!
michsco commentedon Jun 23, 2016
I created a fork of the project and a new branch feature/custom-allocator that contains initial changes. There are still a lot of assumptions built into the code using the default allocator for std::vector, std::map, and std::string.