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

convert json value to std::string??? #1061

Closed
Mamlesh opened this issue Apr 19, 2018 · 13 comments
Closed

convert json value to std::string??? #1061

Mamlesh opened this issue Apr 19, 2018 · 13 comments
Labels
solution: proposed fix a fix for the issue has been proposed and waits for confirmation

Comments

@Mamlesh
Copy link

Mamlesh commented Apr 19, 2018

how to convert json value to string ..

@nlohmann
Copy link
Owner

What did you try? Was there an error message? Did you have a look at the README?

@nlohmann nlohmann added the state: needs more info the author of the issue needs to provide more details label Apr 19, 2018
@Mamlesh
Copy link
Author

Mamlesh commented Apr 19, 2018

thanks for replay sir....
i need to convert the json value ....example

json jk; json jsd;
jk.pushback(jsd);

string str=jk;

want to use 'jk' value as string....

@whackashoe
Copy link
Contributor

From README

You can also get a string representation of a JSON value (serialize):

// explicit conversion to string
std::string s = j.dump();    // {\"happy\":true,\"pi\":3.141}

// serialization with pretty printing
// pass in the amount of spaces to indent
std::cout << j.dump(4) << std::endl;
// {
//     "happy": true,
//     "pi": 3.141
// }

@nlohmann nlohmann added solution: proposed fix a fix for the issue has been proposed and waits for confirmation and removed state: needs more info the author of the issue needs to provide more details labels Apr 20, 2018
@nlohmann
Copy link
Owner

Can I close this issue?

@AOrazaev
Copy link

AOrazaev commented Apr 27, 2018

@nlohmann Probably not, looks like case with operator= messed up:

  json fail = R"({"foo": "bar"})"_json;
  // std::string barExpected;`
  // barExpected = fail["foo"]; // fails
  std::string barExpected = fail["foo"]; //works

@nlohmann
Copy link
Owner

@AOrazaev I cannot reproduce the error. Which compiler are you using? What is the error message?

@rue-ryuzaki
Copy link

rue-ryuzaki commented Apr 27, 2018

Maybe like this?

std::string value = parser["value"].get<std::string>();

@AOrazaev
Copy link

@rue-ryuzaki Yep, I've worked around this like you said.

@nlohmann I compiled example from VS2017.
For some reason, when I'm compiling from developer console on small example using cl.exe I'm not getting this error, but from VS2017 actually I'm getting next:

Severity	Code	Description	Project	File	Line	Suppression State	Detail Description
Error (active)	E0350	more than one operator "=" matches these operands:	check_json	check_json.cpp	13		            function "std::basic_string<_Elem, _Traits, _Alloc>::operator=(std::basic_string<_Elem, _Traits, _Alloc> &&_Right) [with _Elem=char, _Traits=std::char_traits<char>, _Alloc=std::allocator<char>]"
            function "std::basic_string<_Elem, _Traits, _Alloc>::operator=(std::initializer_list<_Elem> _Ilist) [with _Elem=char, _Traits=std::char_traits<char>, _Alloc=std::allocator<char>]"
            operand types are: std::string = const nlohmann::basic_json<std::map, std::vector, std::string, bool, int64_t, uint64_t, double, std::allocator, nlohmann::adl_serializer>

Code I used to reproduce:

#include "json.hpp"
#include "stdafx.h" // only if using VS
#include <iostream>
#include <string>

using nlohmann::json;

struct JsonWrapper {
    std::string _foo;
public:
    JsonWrapper(const json& obj)
    {
        _foo = obj["foo"];
    }

    void log() {
        std::cout << "Foo: " << _foo << std::endl;
    }
};

int main() {
    json obj = R"({"foo": "bar"})"_json;
    JsonWrapper wrap{ obj };
    wrap.log();
    return 0;
}

@AOrazaev
Copy link

And if insead of:

    JsonWrapper(const json& obj)
    {
        _foo = obj["foo"];
    }

Use:

    JsonWrapper(const json& obj)
        : _foo(obj["foo"])
    {    }

It will fail in developer cmd with cl.exe too. complaining about

error C2668: 'std::basic_string<char,std::char_traits<char>,std::allocator<char>>::basic_string': ambiguous call to overloaded function

@nlohmann
Copy link
Owner

@AOrazaev Your issue seems not related to the original comment.

@nlohmann
Copy link
Owner

@AOrazaev For #1061 (comment), using

JsonWrapper(const json& obj)
    : _foo(obj["foo"].get<std::string>())
{}

works though.

@AOrazaev
Copy link

AOrazaev commented Apr 30, 2018

I mean, probably topic starter tried his example in VS and hit issue I described before.

I personally worked around everything with get<std::string>, but bug is still there.

@nlohmann
Copy link
Owner

nlohmann commented May 4, 2018

@Mamlesh Can I close this issue?

ghutchis added a commit to ghutchis/avogadrolibs that referenced this issue Apr 18, 2020
Thanks to nlohmann/json#1061

Signed-off-by: Geoff Hutchison <geoff.hutchison@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
solution: proposed fix a fix for the issue has been proposed and waits for confirmation
Projects
None yet
Development

No branches or pull requests

5 participants