添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
火爆的薯片  ·  javascript filter ...·  1 年前    · 
快乐的领结  ·  traefik - Why does ...·  2 年前    · 
爱热闹的沙发  ·  使用 AFX_EXT_CLASS ...·  2 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

However, type x; x = json; doesn't work, because that would require adding a new assignment operator for type .

I find myself needing to use expression (2) more often then expression (1). This can get quite annoying, especially if type is something complicated. In a few cases, I defined

template <typename U>
void convert(const json& j, U& x) { x = j.get<U>(); }

But it would be nice if get had an overload taking a reference as an argument, so that the following would be possible.

type x;
json.get(x);

Is there already a function that does that, that just has a different name? I couldn't find one in the documentation.

Edit: I've submitted an issue on GitHub.

Edit 2: An alternative to the get function, T& get_to(T&), will be included in release 3.3.0.

"But it would be nice if get had an overload taking a reference as an argument" - why do you not get in touch with the library author then and suggest that. Maybe even contribute a patch. ? – Jesper Juhl Sep 5, 2018 at 18:43

It actually does work. The basic_json type has a templated conversion operator which just calls get<T>() for you. The following code compiles just fine:

#include <nlohmann/json.hpp>
using nlohmann::json;
namespace ns {
    struct MyType {
        int i;
    void to_json(json& j, MyType m) {
        j = json{{"i", m.i}};
    void from_json(json const& j, MyType& m) {
        m.i = j.at("i");
int main() {
    json j{{"i", 42}};
    ns::MyType type;
    type = j;
                Well, it doesn't seem to always work. You commented on another related question I asked a couple months ago.
– SU3
                Sep 5, 2018 at 19:11
                @SU3 When it doesn't work, it's because it can't determine which ValueType to deduce for the templated conversion operator. It could convert to multiple different types, so it's ambiguous.
– Justin
                Sep 5, 2018 at 19:13
                @SU3 It worked fine on Godbolt but not on your machine because you need to update nlohmann json. Select an older version of nlohmann json and you can see the same error (you removed that part of your comment, just want to be clear why it happened).
– Justin
                Sep 5, 2018 at 19:15
                @SU3 That is... peculiar. Indeed there is apparently a warning (but it looks a lot like an error), but it's just giving us the stack trace instead of any diagnostic. That's probably a GCC bug (would want to verify it offline to be sure). Clang rejects it with a more reasonable error message (godbolt.org/z/AGTc8T)
– Justin
                Sep 5, 2018 at 19:21
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.