Skip to content

Instantly share code, notes, and snippets.

@vinniefalco
Created November 15, 2019 18:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vinniefalco/5ebc4bd5a37a750cfde3bda8da03ad31 to your computer and use it in GitHub Desktop.
Save vinniefalco/5ebc4bd5a37a750cfde3bda8da03ad31 to your computer and use it in GitHub Desktop.
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