-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
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
Sequential reading of JSON arrays #851
Comments
Are you able to change the format at all? Could you write as individual objects instead of an array? Essentially JSON Lines http://jsonlines.org/ |
@gregmarr I'm really sorry to bother you, I have a question about json array of string, it doesn't have a key, how can I detect the value in the array ? Following is my solution
Does their has any function like this ?
|
@pingsoli You can use |
@nlohmann I appreciate your reply, thank you very much. Your reply is really fast, thanks for your replay again. Have a good day. : ) |
Gregg, thank you for your suggestion. I'll see if I can convince upstream to change the format. |
You could try to use a callback function which you can pass to the parse function, see https://nlohmann.github.io/json/classnlohmann_1_1basic__json_aecae491e175f8767c550ae3c59e180e3.html#aecae491e175f8767c550ae3c59e180e3: #include <iostream>
#include "json.hpp"
using json = nlohmann::json;
int main(int argc, char* argv[]) {
json x = {1,2,3,4,5,6,7,8,9};
std::string s = x.dump();
json::parser_callback_t cb = [](int depth, json::parse_event_t event, json & parsed)
{
if (event == json::parse_event_t::value)
{
std::cout << parsed << std::endl;
return false;
}
else
{
return true;
}
};
json y = json::parse(s, cb);
std::cout << y << std::endl;
} The example shows how to process values individually (here: print to |
Can I close this issue? |
Is there any way to do the callback-based parsing while reading from an |
I am afraid this is not possible in the moment. There is a private API, but it is not exposed publicly so far. |
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. |
I have an application that logs events as an array of JSON objects. The objects are written one by one to some network stream. I would like to process them when they arrive, not after the application has closed and the array is complete.
So, I would like to have (or be able to write) a class with an interface like this:
Usage could be like this:
In practice the strings (msgs) would come from a recv call of a socket or something like that.
Am I right that currently this is hard to implement? It seems that I can't parse partial JSON input.
The text was updated successfully, but these errors were encountered: