0

With nlohmann::json an object can be parsed using a couple different expressions:

  1. type x = json;
  2. type x; x = json.get<type>();

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.

SU3
  • 5,064
  • 3
  • 35
  • 66
  • "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 05 '18 at 18:43

1 Answers1

2

However, type x; x = json; doesn't work

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;
}
Justin
  • 24,288
  • 12
  • 92
  • 142
  • Well, it doesn't seem to always work. You commented on another related [question](https://stackoverflow.com/q/50881469/2640636) I asked a couple months ago. – SU3 Sep 05 '18 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 05 '18 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 05 '18 at 19:15
  • I removed that part because I noticed that there were still warnings. – SU3 Sep 05 '18 at 19:17
  • @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 (https://godbolt.org/z/AGTc8T) – Justin Sep 05 '18 at 19:21
  • `GCC 7.2.0` gives an error with `-std=c++14` (that's what I normally use for compatibility reasons with other libraries) and the same warnings as Godbolt with `-std=c++17`. – SU3 Sep 05 '18 at 19:28