- 
          
 - 
                Notifications
    
You must be signed in to change notification settings  - Fork 7.2k
 
Description
Description
I am not really sure how to go about using user-defined nlohhman::to_string() (in a consistent manner).
Is there any reason for this template to be implemented on nlohmann side at all (since it's pretty much .dump() alias anyway).
If this wasn't intended, is there any chance to alter this behaviour.
Reproduction steps
Example for context below, granted this template behaviour is to be expected so I'm not really sure how provided example was supposed to work in the first place, unless user version is not supposed to be implemented inside nlohmann, which seems inconsistent for using with rest of the API (that or I missed something obvious in documentation linked).
Expected vs. actual results
Removing example implementation and leaving implementing up to user or providing macro to enable default one seems like a good alternative.
Minimal code example
Example user implementation
namespace nlohmann // same for namespace "ns" in examples below
{
template<typename BasicJsonType>
std::string to_string(const BasicJsonType &j)
{
    if (j.type() == nlohmann::json::value_t::string)
        return j.template get<std::string>();
    else
        return j.dump();
}
}
Example calls
// All examples below prioritize internal/first implementation
std::cout << nlohmann::to_string(jsonobj["key"]) << std::endl;
std::cout << to_string(jsonobj["key"]) << std::endl;
{
    using namespace nlohmann;
    std::cout << to_string(jsonobj["key"]) << std::endl;
}
{
    using namespace ns;
    std::cout << to_string(jsonobj["key"]) << std::endl;
}
// Calls like these still work
std::cout << ns::to_string(jsonobj["key"]) << std::endl;Error messages
No response
Compiler and operating system
gcc 12.2.1
Library version
3.11.2
Validation
-  The bug also occurs if the latest version from the 
developbranch is used. - I can successfully compile and run the unit tests.