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

Support parse std::pair to Json object #563

Closed
nguoithichkhampha opened this issue Apr 19, 2017 · 17 comments
Closed

Support parse std::pair to Json object #563

nguoithichkhampha opened this issue Apr 19, 2017 · 17 comments
Assignees
Labels
kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation
Milestone

Comments

@nguoithichkhampha
Copy link

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"}

@nlohmann
Copy link
Owner

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 value_type of a std::map. Then, you can also assign the map at once:

std::map<std::string, std::string> m;
m["key1"] = "value1";
m["key2"] = "value2";

json j = m;

What do you think?

@nguoithichkhampha
Copy link
Author

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.

@nlohmann
Copy link
Owner

But you can use the initializer syntax as well.

@nguoithichkhampha
Copy link
Author

nguoithichkhampha commented Apr 25, 2017

No, I mean I have a struct
struct A { std::pair<std::string,std::string> value; };
And I want to parse struct A to Json object and reverse

@nlohmann
Copy link
Owner

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

@nguoithichkhampha
Copy link
Author

nguoithichkhampha commented Apr 25, 2017

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++

@nlohmann
Copy link
Owner

I see. But I'm not sure whether a constructor to create a singleton object would be helpful for others.

@nlohmann nlohmann added the state: please discuss please discuss the issue or vote for your favorite option label Apr 25, 2017
@TurpentineDistillery
Copy link

@nguoithichkhampha

No, I mean I have a struct
struct A { std::pair<std::string,std::string> value; };
And I want to parse struct A to Json object and reverse

Can you provide sample implementation?

@nguoithichkhampha
Copy link
Author

nguoithichkhampha commented Apr 26, 2017

yes, this is my case:
I want to create a json aray:
[{"record1":"note1"},{"record2":"note2"}......]
so I use a std::vector<std::pair<std::string,std::string>> I don't use std::map<std::string,std::string> because I don't want to the element auto sorted by key

@nlohmann
Copy link
Owner

nlohmann commented May 3, 2017

If you use std::vector<std::pair<std::string,std::string>>, where do you use the JSON library?

@nguoithichkhampha
Copy link
Author

I'm building a web service. the response of ws is json format. I use JSON library at all when send reply.

@nlohmann
Copy link
Owner

nlohmann commented May 4, 2017

Again, please provide a concrete example.

@nguoithichkhampha
Copy link
Author

I can't give detail code in here. Just my idea to support std::pair. if you feel it not useful. You can discard.

@JohanMabille
Copy link

JohanMabille commented Aug 22, 2017

@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 ?

@nlohmann
Copy link
Owner

nlohmann commented Aug 22, 2017

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?

@JohanMabille
Copy link

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.
Thanks for your quick reply.

@nlohmann
Copy link
Owner

Sorry for the inconvenience!

@nlohmann nlohmann added solution: proposed fix a fix for the issue has been proposed and waits for confirmation and removed state: please discuss please discuss the issue or vote for your favorite option labels Aug 22, 2017
@nlohmann nlohmann added this to the Release 3.0.0 milestone Aug 22, 2017
@nlohmann nlohmann self-assigned this Aug 22, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation
Projects
None yet
Development

No branches or pull requests

4 participants