Skip to content

Instantly share code, notes, and snippets.

@paddy74
Last active April 9, 2021 09:24
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paddy74/acbdf359ed0c3f08d4418643c328df9b to your computer and use it in GitHub Desktop.
Save paddy74/acbdf359ed0c3f08d4418643c328df9b to your computer and use it in GitHub Desktop.
Read the values of a JSON file using C++ REST SDK
#pragma once
#include <cpprest/json.h>
/**
* @brief Parse a JSON file into a JSON object.
*
* @param jsonFileName The path to the JSON file to parse.
*/
web::json::value readJsonFile(std::string const & jsonFileName)
{
web::json::value output; // JSON read from input file
try
{
// Open the file stream
std::ifstream f(jsonFileName);
// String stream for holding the JSON file
std::stringstream strStream;
// Stream file stream into string stream
strStream << f.rdbuf();
f.close(); // Close the filestream
// Parse the string stream into a JSON object
output = web::json::value::parse(strStream);
}
catch (web::json::json_exception excep)
{
throw web::json::json_exception("Error Parsing JSON file " + jsonFileName);
}
return output;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment