Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
xsacha opened this issue Mar 16, 2019 · 3 comments
Closed
Labels
confirmed state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated

Comments

@xsacha
Copy link

xsacha commented Mar 16, 2019

  • 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.

@xsacha xsacha changed the title Cannot deserialise std::vector<std::pair<>> std::pair treated as an array instead of key-value in std::vector<std::pair<>> Mar 16, 2019
@nlohmann
Copy link
Owner

I can confirm your observations. We currently can only convert arrays to pairs:

template<typename BasicJsonType, typename A1, typename A2>
void from_json(const BasicJsonType& j, std::pair<A1, A2>& p)
{
    p = {j.at(0).template get<A1>(), j.at(1).template get<A2>()};
}

Your case would be possible in principle, but it is a very special usecase to have a list of objects where each object has only one entry.

@xsacha
Copy link
Author

xsacha commented Mar 16, 2019

I know it doesn't make much sense because it should just be a map but I'm trying to support this specific STL type that was previously serialised like this in a different json library.
I think it should support going to/from a pair as a key-value.

@stale
Copy link

stale bot commented Apr 15, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated label Apr 15, 2019
@stale stale bot closed this as completed Apr 22, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated
Projects
None yet
Development

No branches or pull requests

2 participants