Created
November 15, 2019 18:56
-
-
Save vinniefalco/5ebc4bd5a37a750cfde3bda8da03ad31 to your computer and use it in GitHub Desktop.
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
json::value | |
parse_file( char const* filename ) | |
{ | |
json::error_code ec; | |
auto f = std::fopen( filename, "r" ); | |
if( ! f ) | |
{ | |
ec.assign( errno, json::generic_category() ); | |
throw json::system_error(ec); | |
} | |
json::parser p; | |
p.start(); | |
do | |
{ | |
char buf[4096]; | |
// Read from the file into our buffer. | |
auto const nread = fread( buf, 1, sizeof(buf), f ); | |
if( std::ferror(f) ) | |
{ | |
ec.assign( errno, json::generic_category() ); | |
throw json::system_error(ec); | |
} | |
auto nparsed = p.write_some( buf, nread, ec); | |
// Make sure we use all the characters in the file. | |
if( ! ec && nparsed < nread ) | |
nparsed = p.write_some( buf + nparsed, sizeof(buf) - nparsed, ec ); | |
if( ec ) | |
throw json::system_error(ec); | |
} | |
while( ! std::feof(f) ); | |
// Tell the parser there is no more serialized JSON. | |
p.write_eof(ec); | |
if( ec ) | |
throw json::system_error(ec); | |
return p.release(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment