Created
July 25, 2023 09:27
-
-
Save laxerrrr/61d5ce36c4b79661e77c8032c6c45027 to your computer and use it in GitHub Desktop.
Concatenated incremental JSON stream parsing with nlohmann/json (somewhat π© code!)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//mtx_ is a Boost mutex | |
void Parser::put_into_internal_string_stream(std::string& str) { | |
mtx_.lock(); | |
internal_ss_.clear(); | |
internal_ss_.str(internal_ss_.str() + str); | |
mtx_.unlock(); | |
} | |
//Parser.cpp contains internal_ss_ and last_parse_pos_ | |
std::vector<json> Parser::extract_json_blobs_from_internal_string_stream_new() { | |
mtx_.lock(); | |
std::cout << "in mutex\n"; | |
std::vector<json> return_vec; | |
//std::cout << "string stream contents: " << internal_ss_.str() << std::endl << std::endl; | |
internal_ss_.clear(); | |
bool prepended = false; | |
while(internal_ss_.peek() != EOF) { | |
json json_obj_buf; | |
try { | |
internal_ss_ >> json_obj_buf; | |
return_vec.emplace_back(json_obj_buf); | |
last_parse_pos_ = internal_ss_.tellg(); | |
} catch(json::parse_error &e) { | |
internal_ss_.clear(); | |
internal_ss_.seekg(last_parse_pos_); | |
if (internal_ss_.eof()) break; //IMPORTANT OR IF ITS EMPTY, WE GET EXC_BAD_ACCESS ON READ | |
while(internal_ss_.peek() != EOF) { | |
prepend_string_.push_back(internal_ss_.get()); | |
} | |
std::stringstream(prepend_string_).swap(internal_ss_); //Reset internal string stream | |
prepend_string_ = ""; //Reset prepend | |
last_parse_pos_ = 0; | |
break; | |
} | |
} | |
mtx_.unlock(); | |
return return_vec; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment