-
-
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
Support parse std::pair to Json object #563
Comments
No, this is currently not supported. You can, however, create an object with an initializer list json j = { {"test1", "value1"} }; I'm not sure whether implementing a constructor for a pair would be too helpful. The only scenario where pairs may be of interest would be if they are the std::map<std::string, std::string> m;
m["key1"] = "value1";
m["key2"] = "value2";
json j = m; What do you think? |
I think very useful if can support std::pair . Sometimes my json object only one field, so I don't want to use a std::map. Although I can use a std::map with one element but I can't access directly key and value in map in case don't know key. Otherwise , with std::pair, I only call pair.first and pair.second to get info in pair. |
But you can use the initializer syntax as well. |
No, I mean I have a struct |
You may want to have a look at this if you want to convert arbitrary types to JSON: https://github.com/nlohmann/json#arbitrary-types-conversions |
Yes, I know but I need to parse manually , need to get pair.first and pair.second and after that add them in Json object. I did this . But if support parse std::pair I don't need write manual code . And this library should support because pair is basic container of C++ |
I see. But I'm not sure whether a constructor to create a singleton object would be helpful for others. |
Can you provide sample implementation? |
yes, this is my case: |
If you use |
I'm building a web service. the response of ws is json format. I use JSON library at all when send reply. |
Again, please provide a concrete example. |
I can't give detail code in here. Just my idea to support std::pair. if you feel it not useful. You can discard. |
@SylvainCorlay and I are also interested in support for std::pair and std::tuple. The framework that we are building serializes patches of modified attributes in the context of an observer pattern. Some of these attributes may be std::pair. We have implemented specializations of adl_serializer as explained here. Would you be interested in a PR with that stuff ? |
Meanwhile, this code compiles: #include "json.hpp"
#include <iostream>
using json = nlohmann::json;
int main() {
auto p = std::make_pair(1, 2);
auto t = std::make_tuple("foo", "bar", "baz");
json j;
j["pair"] = p;
j["tuple"] = t;
std::cout << std::setw(4) << j << std::endl;
} Output: {
"pair": [
1,
2
],
"tuple": [
"foo",
"bar",
"baz"
]
} Also, this is possible: std::pair<int, int> p2 = j["pair"];
std::tuple<std::string, std::string, std::string> t2 = j["tuple"]; Is this helpful for you, @JohanMabille? |
Indeed, this works in develop branch, but not in v2.1.1 (which we are using). We'll wait for the v3.0.0 then. |
Sorry for the inconvenience! |
Hi, I really interested with this library. But did you support parse a std::pair to Json object ?
Something like this:
std::pair<std::string,std::string>{"test1","value1"}
will have a json object:
{ "test1":"value1"}
The text was updated successfully, but these errors were encountered: