-
-
Notifications
You must be signed in to change notification settings - Fork 6.7k
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
Comments
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. |
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 Please also note issue #623 which discusses improvement in the parser families. |
@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. |
This should work with a callback function as well. This should work without a |
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;
} |
Did you try the example with the callback function? |
Woops, I missed your reply, sorry. I'll have a chance to try it soon. |
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.
The text was updated successfully, but these errors were encountered: