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

Add json_view type support to avoid excessive copying #1954

Closed
slonm opened this issue Feb 24, 2020 · 3 comments
Closed

Add json_view type support to avoid excessive copying #1954

slonm opened this issue Feb 24, 2020 · 3 comments
Labels
kind: enhancement/improvement state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated

Comments

@slonm
Copy link

slonm commented Feb 24, 2020

Feature name and mission inspired by std::string_view. json_view is immutable json object which refers inside to another outer json object and do not keep data. json will have

Motivation example:

//For illustate how to possible use json_view args reportA and reportB declared differently
//but from caller perspective both cases guarantee constness of passes arguments

void prepareAndSendReport(const json& reportA, json_view reportB) {
 //while combine rpt reportA and reportB will be not copied
	json rpt;
	rpt["reportA"] = json_view{reportA};
	rpt["reportB"] = reportB;
	rpt["reporttime"] = time(NULL);
	serviceClient.post(rpt);
}

struct A {
	json getReport();
}

struct B {
	const json& getReport();
	void fillReport();
  private:
	json report;
}

void main() {
	A a;
	B b;
	b.fillReport();
//a.getReport() returns temporary object but that's ok while stack is not unwind
//b.getReport() returns refecence to B::report
	prepareAndSendReport(a.getReport(), b.getReport());
}
@FrancoisChabot
Copy link
Contributor

FrancoisChabot commented Feb 24, 2020

I kinda like the idea (usual reservations wrt. dangling references footguns aside) , but not the name. If I see json_view, I'm immediately expecting some kind of lazy evaluated interpretation of a raw string buffer.

@slonm
Copy link
Author

slonm commented Feb 24, 2020

I'm immediately expecting some kind of lazy evaluated interpretation of a raw string buffer.

Basically view should looks like immutable json object and could have any underlying storage. It could be json or some raw string or primitive type, etc. Don't see contradiction here

@stale
Copy link

stale bot commented Mar 26, 2020

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 Mar 26, 2020
@stale stale bot closed this as completed Apr 2, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: enhancement/improvement 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