Skip to content

dump() method doesn't work with a custom allocator #268

@michsco

Description

@michsco

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().

string_t dump(const int indent = -1) const
{
    std::stringstream ss;

    if (indent >= 0)
    {
        dump(ss, true, static_cast<unsigned int>(indent));
    }
    else
    {
        dump(ss, false, 0);
    }

    return ss.str();
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    kind: bugsolution: wontfixthe issue will not be fixed (either it is impossible or deemed out of scope)state: please discussplease discuss the issue or vote for your favorite option

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions