-
-
Notifications
You must be signed in to change notification settings - Fork 6.7k
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
Comments
std::vector<std::pair<>>
std::vector<std::pair<>>
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. |
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. |
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. |
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:
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.
The text was updated successfully, but these errors were encountered: