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

Can we have a json_view class like std::string_view? #1158

Closed
qduyang opened this issue Jul 10, 2018 · 6 comments
Closed

Can we have a json_view class like std::string_view? #1158

qduyang opened this issue Jul 10, 2018 · 6 comments
Labels
state: help needed the issue needs help to proceed 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

@qduyang
Copy link

qduyang commented Jul 10, 2018

  • Describe the feature in as much detail as possible.
    As copy a json would have many allocations, it would nice to have a view class like std::string_view for performance's sake.
@nlohmann
Copy link
Owner

Could you provide more information or examples?

@nlohmann nlohmann added the state: needs more info the author of the issue needs to provide more details label Jul 10, 2018
@qduyang
Copy link
Author

qduyang commented Jul 11, 2018

The use case is that, there is an immutable template nlohmann::json object as below, and there would have thousands of concurrent access to it.

{
   "id": 1234567,
   "data": [{"price": 123, "quantity": 1} , {"price": 123, "quantity": 1}
}

Each access will

  • make copy of the template
  • update the "id" field only
  • dump it to string
  • then send the generated string to some other applications.

I want to avoid the the copy in the first step.
If there is a view class which could generate a joint view from the "data" field (in the immutable template) and a new "id" field, we can avoid the allocations for duplicates "data".
The general concept is to have a json_view that creates a jointly view from multiple nlohmann::json objects without any extra allocations.
The idea is stolen from https://www.boost.org/doc/libs/1_67_0/libs/mpl/doc/refmanual/joint-view.html
And there are also some other views, https://www.boost.org/doc/libs/1_67_0/libs/mpl/doc/refmanual/views.html

(The concept is not same as std::string_view class, but the non-owning concept would be similar)

@nlohmann nlohmann removed the state: needs more info the author of the issue needs to provide more details label Jul 11, 2018
@nlohmann
Copy link
Owner

I think I understand the issue, but I am skeptical whether an implementation to this library is possible (or at least: possible without too much effort).

@nlohmann nlohmann added state: please discuss please discuss the issue or vote for your favorite option state: help needed the issue needs help to proceed labels Jul 21, 2018
@stale
Copy link

stale bot commented Aug 20, 2018

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 20, 2018
@qduyang
Copy link
Author

qduyang commented Aug 24, 2018

One premature thinking is to have a json_view class which accepts const JSON references parameters to construct new JSONs. In this case, we avoid data copying, but leave the referred objects' lifetime management to the user, and It will cause undefined behavior if use incorrectly.
I will find time to read the code, and see if I could give you some suggestion.

@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 Aug 24, 2018
@stale
Copy link

stale bot commented Sep 23, 2018

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 Sep 23, 2018
@stale stale bot closed this as completed Sep 30, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
state: help needed the issue needs help to proceed 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

2 participants