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

How to parse multi object json from file? #1470

Closed
ufosky opened this issue Feb 1, 2019 · 3 comments
Closed

How to parse multi object json from file? #1470

ufosky opened this issue Feb 1, 2019 · 3 comments
Labels
state: needs more info the author of the issue needs to provide more details state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated

Comments

@ufosky
Copy link

ufosky commented Feb 1, 2019

I have followed this issue #367, but when my file contains whitespaces at the end, it will throw parse error exception. Is there a way to parse this file normally?

The file content like this, there are some whitespace lines at the end:

{"a":1}
{"a":2}


The code:

void JsonLoader::load_messages(const char *filepath,
                               std::deque<google::protobuf::Message *> *out_msgs) {
  out_msgs->clear();
  nlohmann::json j;
  std::ifstream fs(filepath);
  int index = 0;
  while (fs.peek() != EOF) {
    try {
      fs >> j;
    } catch (nlohmann::json::exception &e)
    {
      LOG(WARNING) << "    std::exception:" << e.id << " " << e.what() << std::endl;
      break;
    }

    ++index;
    LOG(WARNING) << "Load " << index << "-th json.";
    if (!j.empty()) {
      google::protobuf::Message *request = _request_prototype->New();
      Pb2Json::Json2Message(j, *request, true);
      out_msgs->push_back(request);
      LOG(WARNING) << "Loaded " << out_msgs->size() << " jsons";
    }
  }

}
@ufosky ufosky changed the title How to parse How to parse multi object json from file? Feb 1, 2019
@nlohmann
Copy link
Owner

nlohmann commented Feb 1, 2019

In order to asses your issue, we need the following information:

  • What is the issue you have?

  • Please describe the steps to reproduce the issue. Can you provide a small but working code example?

  • What is the expected behavior?

  • And what is the actual behavior instead?

  • Which compiler and operating system are you using? Is it a supported compiler?

  • Did you use a released version of the library or the version from the develop branch?

  • If you experience a compilation error: can you compile and run the unit tests?

@nlohmann nlohmann added the state: needs more info the author of the issue needs to provide more details label Feb 1, 2019
@nickaein
Copy link
Contributor

nickaein commented Feb 2, 2019

Each call of fs >> j; lead to reading one JSON from file and leaving stream at where the parsing has ended. When the parser is called for the third time, there is no JSON available in the rest of file contents and that's causes an exception as there is no valid JSON.

I believe this is an expected behavior. This is basically same as having an input file containing only white-spaces. Your solution to continue reading and break on exception seems fine. Is there any reason to avoid it?
One other solution is to manually skip over the whitespace characters until you reach an non-space character or end of file.

@stale
Copy link

stale bot commented Mar 4, 2019

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 4, 2019
@stale stale bot closed this as completed Mar 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
state: needs more info the author of the issue needs to provide more details 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

3 participants