MCPcopy Create free account
hub / github.com/CppMicroServices/CppMicroServices / ReadJSONDocument

Function ReadJSONDocument

tools/jsonschemavalidator/main.cpp:45–63  ·  view source on GitHub ↗

* \brief Utility function to create a rapidjson::Document object from a file path * * \jsonfile is the file path of the json data * * \returns rapidjson::Document object representing the json file * \throws std::runtime_error if the input file is non-existent or is not a valid json file */

Source from the content-addressed store, hash-verified

43 * \throws std::runtime_error if the input file is non-existent or is not a valid json file
44 */
45rapidjson::Document
46ReadJSONDocument(std::string const& filePath)
47{
48 std::ifstream fileStream(filePath.c_str());
49 if (!fileStream.is_open())
50 {
51 throw std::runtime_error("Failed to open file at " + filePath);
52 }
53 rapidjson::IStreamWrapper streamWrapper(fileStream);
54 rapidjson::Document doc;
55 doc.ParseStream(streamWrapper);
56 if (doc.HasParseError())
57 {
58 throw std::runtime_error("File '" + filePath + "' is not a valid JSON\n Error(offset "
59 + std::to_string(doc.GetErrorOffset())
60 + "): " + GetParseError_En(doc.GetParseError()));
61 }
62 return doc;
63}
64
65/**
66 * \brief Function to validate json file against a schema file

Callers 1

validateFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected