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

More use of move semantics in deserialization #786

Closed
SylvainCorlay opened this issue Oct 16, 2017 · 4 comments
Closed

More use of move semantics in deserialization #786

SylvainCorlay opened this issue Oct 16, 2017 · 4 comments

Comments

@SylvainCorlay
Copy link

SylvainCorlay commented Oct 16, 2017

Short version

For certain cases, where copying things is an issue (as the one described below), I would like to favor examples similar to move_only_type over the creation of a from_json function, but specializations of adl_serializer does not seem to work with containers of types for which the specialization exist.

Ideally, I would like to have a "return by value" version of from_json. The STL container of those types would be built by emplacing the values returned by from_json into the container.

Long version / context

We currently use your library (vendoring the json header for now) in xeus, a native C++ implementation of the Jupyter protocol.

In this work, some of the classes that we deserialize from json have a value semantics and implement the RAII pattern. Basically, constructor and destructors amount to acquiring and freeing the resources. Any non-move copy acquires a new version of the resource (with a different id). Move constructor and assignment transfers the resource to the moved-to object.

This is a context in which being able to move things to a container would make a lot of sense...

@theodelrieu
Copy link
Contributor

Hello, do you have a small example that fails to compile?

If I understand you correctly, the bug is in the library-provided conversions?

@SylvainCorlay
Copy link
Author

I just want to avoid copies when filling the e.g. an std::vector<my_type>, using e.g. emplace_back.

@theodelrieu
Copy link
Contributor

The following code works fine, there is no copy involved:

#include <iostream>
#include <json.hpp>

struct test {
  test() = default;
  test(test const &) = delete;
  test(test &&) { std::cout << "move" << std::endl; };

  test &operator=(test const &) = delete;
  test &operator=(test &&) = default;
  ~test() = default;
};

namespace nlohmann {
template <> struct adl_serializer<::test> {
  static ::test from_json(json const &) { return {}; }
};
}

int main(int argc, char const *argv[]) {
  auto j = nlohmann::json{1, 2, 3};
  auto v = j.get<std::vector<test>>();
  v.emplace_back(nlohmann::json(1));
}

I think I misunderstand your issue, feel free to post some code :)

@SylvainCorlay
Copy link
Author

Hum, it seems that you have well understood that issue, and your snippet works well. Issue must be on my side (I am tracking assignments and seeing a few). Closing, thanks for your reply.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants