Last active
April 9, 2021 09:24
-
-
Save paddy74/acbdf359ed0c3f08d4418643c328df9b to your computer and use it in GitHub Desktop.
Read the values of a JSON file using C++ REST SDK
This file contains hidden or 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
#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