Skip to content

Conversion error for std::map<int, std::string>  #739

@MariaRamos89

Description

@MariaRamos89

Hi,

I'm trying to save a std::map<int, std::string> into a json, and then the json is saved in a file.

nlohmann::json db;
db = mymap;
std::ofstream output_file(filename);
output_file << std::setw(4) << db << std::endl;

The problem is the result of the json:

I'm expecting this kind of output:
{ 1 : "one", 2 : "two"}
However, the result is:
[ [1, "one"], [2, "two"]]

So, when in other program I'm trying to read that file and try to save it in a json for doing the conversion to std::map again, I'm having the next error:

error: ambiguous overload for ‘operator=’ (operand types are ‘std::map<int, std::__cxx11::basic_string<char> >’ and ‘nlohmann::json {aka nlohmann::basic_json<>}’)

I've been looking in similar errors like #607, but I don't find a solution.
I've tried to build the map manually but the output is the same. Maybe I'm missing something.

Thanks in advance

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions