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

output_adapter not user extensible #1534

Closed
abrownsword opened this issue Mar 20, 2019 · 18 comments
Closed

output_adapter not user extensible #1534

abrownsword opened this issue Mar 20, 2019 · 18 comments
Labels
kind: enhancement/improvement state: please discuss please discuss the issue or vote for your favorite option state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated

Comments

@abrownsword
Copy link

I have a type that isn't a vector, string or ostream that I really want to use with to_msgpack directly, rather than having to cause an 'extra' memory copy. I thought that output_adapter would be perfect for this... unfortunately I've discovered that it only has 3 constructors for the aforementioned 3 std types.

If it had an additional constructor that accepted an output_adapter_t, then I could construct my own adapter object and pass it in.

@nlohmann
Copy link
Owner

Good point! I shall have a look.

@nlohmann
Copy link
Owner

FYI: Started working on this issue: https://github.com/nlohmann/json/compare/feature/extensible_output_adapter

Is this what you had in mind?

@abrownsword
Copy link
Author

Yeah, that looks pretty much exactly like what I envisioned. Should be very useful to adapt to all manner of containers, stream APIs, etc.

@nlohmann
Copy link
Owner

Before opening the input and output adapters for extension, I cleaned up a bit, and I would love to get some feedback on this:

  • Started to move the logics to create an input_adapter_t or output_adapter_t from the respective constructors in the input_adapter/output_adapter class to static functions make_input_adapter/make_output_adapter. Eventually, we should not be needing class input_adapter/output_adapter any longer.
  • Removed char type template parameter from output adapter protocol (and thus, from class output_adapter, binary_writer). The JSON library now uses uint8_t for bytes to write, and it the job of the output adapter to do conversions where required. This removes a lot of logics, because serializer worked with char and binary_writer with uint_8, but only the serializer was able to cope with different char types...

Any ideas on this?

@nlohmann nlohmann added the state: please discuss please discuss the issue or vote for your favorite option label Mar 23, 2019
@abrownsword
Copy link
Author

Scanned through your changes and it looks like a nice cleanup and refinement. One thing would make my life even easier that I don't see in there: I have a class in which I've concretely implemented your two write_characters functions (but I don't want to inherit from your protocol), and it would be nice if there was a templated version of the adapter that could trivially adapt my class to your output function.

@nlohmann
Copy link
Owner

This is basically what we're doing with the SAX interface: we do have an interface, but we accept any object as long as it passes a SFINAE check. This would be more work, though, and I am not sure whether this can be done without a breaking change...

@abrownsword
Copy link
Author

Can't you just provide a template that can be used to adapt anything to be your protocol though? Its not transparent, but its at least a 1-liner?

@abrownsword
Copy link
Author

Your latest readme update gave me an idea for another input_adapter use case: a comment stripper.

@stale
Copy link

stale bot commented May 5, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated label May 5, 2019
@stale stale bot closed this as completed May 12, 2019
@abrownsword
Copy link
Author

abrownsword commented May 14, 2019

This has gone stale thanks to the bot, but is the feature still on the todo list? @nlohmann

@abrownsword
Copy link
Author

@nlohmann -- is there a plan to implement this idea in the near future?

@nlohmann
Copy link
Owner

I did not come to it... Any help is greatly appreciated.

@nlohmann
Copy link
Owner

I'll reopen and see if someone can take a look.

@nlohmann nlohmann reopened this Jul 22, 2019
@stale stale bot removed the state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated label Jul 22, 2019
@stale
Copy link

stale bot commented Aug 21, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated label Aug 21, 2019
@stale stale bot closed this as completed Aug 28, 2019
@abrownsword
Copy link
Author

@nlohmann -- keeping the hope alive.

@jzakrzewski
Copy link

I'd also like to see this implemented. It'd save me a lot coping data around.

@abrownsword
Copy link
Author

I keep having new use cases for this showing up (mostly because writing to, or at least not directly to, std::ostream is very often not where we want to send data).

@FrancoisChabot
Copy link
Contributor

I am of the opinion that this should be wrapped within a larger refactor of the output_adapter system (see #2172).

If we do go through with moving away from the abstract interface to a templated approach for performance, then it makes sense to do add that feature within that context.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: enhancement/improvement state: please discuss please discuss the issue or vote for your favorite option state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated
Projects
None yet
Development

No branches or pull requests

4 participants