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

[Help] How to Improve Json Output Performance with Large Json Arrays #1624

Closed
calben opened this issue Jun 3, 2019 · 3 comments
Closed

[Help] How to Improve Json Output Performance with Large Json Arrays #1624

calben opened this issue Jun 3, 2019 · 3 comments
Labels
kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation

Comments

@calben
Copy link

calben commented Jun 3, 2019

Hey everyone,

I have class A which includes a std::deque<class B>.
Both A and B have an AsJson() that return a json object, where A's AsJson pushes back many instances of B onto an array, and later on I print this Json as a string.

    nlohmann::json json;
    // add some other fields
    ...
    ...
    for (auto e : events) {
        json["events"].push_back(e->AsJson());
    }
    return json;
    // later outputting json to stream as string or cbor

Constructing the large Json takes a bit of memory and time and then the whole things needs to be processed again to format to a string as well.
Is there a better way to handling constructing and outputting Jsons with large arrays with this library?

Thanks!

@igoloe
Copy link

igoloe commented Jun 4, 2019

Use an json array and reserve.

nlohmann::json::array_t eventsAsJson;
eventsAsJson.reserve(events.size());

// for-loop or std::for_each

return {"events",std::move(eventsAsJson)};

@nlohmann
Copy link
Owner

@calben Does @igoloe 's proposal work for you?

@nlohmann nlohmann added kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation labels Jun 22, 2019
@calben
Copy link
Author

calben commented Jun 29, 2019

That does help a bit!
Thank you.

@calben calben closed this as completed Jun 29, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation
Projects
None yet
Development

No branches or pull requests

3 participants