-
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
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
to_json for pairs, tuples #614
Comments
I'm not sure what you mean. Could you provide more details or an example please? |
Thanks for the reply! For example, let's say i want to encode an std::pair<int, int> into json. struct Person
{
std::pair<int, int> position
}; void to_json( json& j, const Person& d )
{
j = json{
{ "position", d.position }
};
}
void from_json( const json& j, Person& d )
{
d.position= j.at( "position" ).get<std::pair<int, int>>();
} But sadly this gives an error. Hopefully I've just misunderstood something and there is a direct method, otherwise it would be absolutely great to be able to do this. |
Have you tried something like this? (I'm not sure if it will work, just speculating) void to_json( json& j, const Person& d )
{
to_json(j["position"], d.position);
}
void from_json( const json& j, lvn::NTextbox::NChar::Data& d )
{
from_json(j["position"], d.position);
} |
There is no default conversion between #include "json.hpp"
using json = nlohmann::json;
struct Person
{
std::pair<int, int> position;
};
void to_json( json& j, const Person& d )
{
// encode pair as a list
j = json{ { "position", {d.position.first, d.position.second} } };
}
void from_json( const json& j, Person& d )
{
auto& pos = j.at("position");
// decode list as pair
d.position = std::make_pair(pos[0], pos[1]);
}
int main()
{
// create a person
Person p;
p.position = {17, 42};
// person -> json
json j = p;
std::cout << std::setw(2) << j << std::endl;
// json -> person
Person p2 = j;
std::cout << std::boolalpha << (p.position == p2.position) << std::endl;
} |
I added support for I didn't think about representing pairs as arrays (e.g. |
In my own codebase, I have a function that encodes them as an object with |
I think encoding pairs and tuples as JSON arrays could be a good idea. However, I am not sure about |
Since almost any Fixing #600 while staying generic-friendly in the implementation of I don't really know what to do with this |
I think you're right, we should convert pairs and tuples as arrays, element ordering being mandatory for those. People with custom conversions with |
Exactly. I wanted to write the same. I think it is a good and predictable behavior to use arrays. |
Ok, I have (finally) some time to work on the library, I'm starting to implement this. I will then try to extract some nested classes as we discussed in #474. |
Great. #458 shows some challenges when it comes to parsing. Some restructuring of the code may help here. |
I wouldn't be affected anyway, as my functions were written before |
With #624, the library now also supports the conversion from/to |
Hi, I was wondering if there might be a simple way to to_json() pairs and tuples, instead of having to explicitly create each element for json.
The text was updated successfully, but these errors were encountered: