| 385 | } |
| 386 | |
| 387 | void JSONReader::ParseJSONArray() { |
| 388 | std::ifstream file(path_); |
| 389 | if (!file.is_open()) throw IOException(ErrorCode::FILE_NOT_FOUND, "Cannot open: " + path_); |
| 390 | |
| 391 | std::string content((std::istreambuf_iterator<char>(file)), |
| 392 | std::istreambuf_iterator<char>()); |
| 393 | |
| 394 | size_t pos = 0; |
| 395 | JSONParser::SkipWhitespace(content, pos); |
| 396 | |
| 397 | if (pos >= content.size() || content[pos] != '[') { |
| 398 | // Try as NDJSON. |
| 399 | options_.array_format = false; |
| 400 | file.close(); |
| 401 | ParseNDJSON(); |
| 402 | return; |
| 403 | } |
| 404 | |
| 405 | pos++; // skip [ |
| 406 | JSONParser::SkipWhitespace(content, pos); |
| 407 | |
| 408 | while (pos < content.size() && content[pos] != ']') { |
| 409 | if (content[pos] != '{') break; |
| 410 | |
| 411 | // Parse object. |
| 412 | std::unordered_map<std::string, Value> record; |
| 413 | pos++; // skip { |
| 414 | JSONParser::SkipWhitespace(content, pos); |
| 415 | |
| 416 | while (pos < content.size() && content[pos] != '}') { |
| 417 | if (content[pos] != '"') break; |
| 418 | auto key_val = JSONParser::ParseString(content, pos); |
| 419 | auto key = key_val.GetValue<std::string>(); |
| 420 | |
| 421 | JSONParser::SkipWhitespace(content, pos); |
| 422 | if (pos < content.size() && content[pos] == ':') pos++; |
| 423 | JSONParser::SkipWhitespace(content, pos); |
| 424 | |
| 425 | auto val = JSONParser::Parse(content, pos); |
| 426 | record[key] = val; |
| 427 | |
| 428 | JSONParser::SkipWhitespace(content, pos); |
| 429 | if (pos < content.size() && content[pos] == ',') pos++; |
| 430 | JSONParser::SkipWhitespace(content, pos); |
| 431 | } |
| 432 | |
| 433 | if (pos < content.size() && content[pos] == '}') pos++; |
| 434 | records_.push_back(std::move(record)); |
| 435 | |
| 436 | JSONParser::SkipWhitespace(content, pos); |
| 437 | if (pos < content.size() && content[pos] == ',') pos++; |
| 438 | JSONParser::SkipWhitespace(content, pos); |
| 439 | } |
| 440 | parsed_ = true; |
| 441 | } |
| 442 | |
| 443 | void JSONReader::DetectSchema() { |
| 444 | if (!parsed_) { |
nothing calls this directly
no test coverage detected