Skip to content

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

Closed

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();
}

Activity

  1. nlohmann commented on Jun 22, 2016

    @nlohmann
    Owner

    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.

  2. nlohmann commented on Jun 22, 2016

    @nlohmann
    Owner

    I created a branch feature/issue268 (see a834338) which fixes the compilation error. Please let me know whether this works for you.

  3. nlohmann commented on Jun 23, 2016

    @nlohmann
    Owner

    Thanks for checking back, and I am looking forward to see (the definitely needed) fixes!

  4. michsco commented on Jun 23, 2016

    @michsco
    Author

    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.

  5. added
    solution: wontfixthe issue will not be fixed (either it is impossible or deemed out of scope)
    on Mar 28, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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