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

Get size without .dump() #634

Closed
UndarkAido opened this issue Jun 22, 2017 · 7 comments
Closed

Get size without .dump() #634

UndarkAido opened this issue Jun 22, 2017 · 7 comments
Labels
kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation

Comments

@UndarkAido
Copy link

Is there a way to get the size of a json object without dumping it first? I'm doing some load testing and would like to avoid the time it takes to dump a 4GB json file just to see how big it is, then do nothing with the dump.

@jaredgrubb
Copy link
Contributor

Yeh, I've wanted this as well.

I personally think this (and many other similar requests like "is this string valid json") could be solved by factoring the parser into a Builder pattern. In this case, the Builder would just accumulate the size of each element's textual size, if it had been dumped, without constructing anything. I think Issue #623 might help with this.

@nlohmann
Copy link
Owner

Are you only interested in the number of key-value pairs of the top-level object? If so, you may pass a callback function to the parse function. This function could count the parse_event_t::key events and avoids actually creating values. If you can provide a small example, I could provide an example.

Please also note issue #623 which discusses improvement in the parser families.

@UndarkAido
Copy link
Author

UndarkAido commented Jun 22, 2017

@nlohmann No, I'm interested in the size of all the values.

Edit: And maybe the keys too, since you can make those big as well.

@nlohmann
Copy link
Owner

This should work with a callback function as well. This should work without a dump call.

@nlohmann
Copy link
Owner

An example (also look at the documentation)

#include "json.hpp"

using json = nlohmann::json;

int main()
{
    std::string json_text = R"({"foo": {"bar": 1, "baz": 2}})";
    
    // variable to count keys
    size_t key_count = 0;
    
    // define a callback function
    json::parser_callback_t cb = [&key_count](int depth,
                                              json::parse_event_t event,
                                              json & parsed)
    {
        // skip object elements with key "Thumbnail"
        if (event == json::parse_event_t::key)
        {
            ++key_count;
        }
        
        return true;
    };
    
    // parse and use the callback function
    json::parse(json_text, cb);
    
    std::cout << "the JSON value has " << key_count << " keys" << std::endl;
}

@nlohmann nlohmann added kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation labels Jun 23, 2017
@nlohmann
Copy link
Owner

nlohmann commented Jul 6, 2017

Did you try the example with the callback function?

@nlohmann nlohmann closed this as completed Jul 7, 2017
@UndarkAido
Copy link
Author

Woops, I missed your reply, sorry. I'll have a chance to try it soon.

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