Skip to content

std::pair treated as an array instead of key-value in std::vector<std::pair<>> #1520

@xsacha

Description

@xsacha
  • What is the issue you have?
    I have a variable with std::vector<std::pair<std::string, std::string>> that cannot be deserialised by nlohmann::json.

  • Please describe the steps to reproduce the issue. Can you provide a small but working code example?

JSON: { "options": [{"video_size": ""}, {"framerate": "0"}, {"vcodec": ""}] }

std::vector<std::pair<std::string,std::string>> m_Options;
Non-working Code:
j["options].get_to(m_Options);
Working code:

for (auto &i : j["options"].get<std::vector<std::map<std::string, std::string>>>())
{
    m_Options.push_back(*i.begin());
}
  • What is the expected behavior?
    Should be able to deserialise to an STL type automatically

  • And what is the actual behavior instead?
    Failed to deserialise because wrong type

  • Which compiler and operating system are you using? Is it a supported compiler?
    MSVC 2017

  • Did you use a released version of the library or the version from the develop branch?
    V3.2.0 - V3.5.0 exhibit this issue

Notes: When serialising m_Options I get:
JSON: { "options":[["video_size",""],["framerate","0"],["vcodec",""]] }

Basically, it treats a std::pair as an array with two elements, rather than a key-value pair.

Metadata

Metadata

Assignees

No one assigned

    Labels

    confirmedstate: stalethe issue has not been updated in a while and will be closed automatically soon unless it is updated

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions