Skip to content

Instantly share code, notes, and snippets.

@paddy74
Last active April 9, 2021 09:24
Show Gist options
  • Save paddy74/acbdf359ed0c3f08d4418643c328df9b to your computer and use it in GitHub Desktop.
Save paddy74/acbdf359ed0c3f08d4418643c328df9b to your computer and use it in GitHub Desktop.

Revisions

  1. paddy74 revised this gist Aug 20, 2019. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion readJsonFile.hpp
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@

    #pragma once

    #include <cpprest/json.h>
    @@ -27,7 +28,7 @@ web::json::value readJsonFile(std::string const & jsonFileName)
    }
    catch (web::json::json_exception excep)
    {
    throw web::json::json_exception("Error Parsing JSON file");
    throw web::json::json_exception("Error Parsing JSON file " + jsonFileName);
    }

    return output;
  2. paddy74 revised this gist Aug 20, 2019. 1 changed file with 2 additions and 3 deletions.
    5 changes: 2 additions & 3 deletions readJsonFile.hpp
    Original file line number Diff line number Diff line change
    @@ -27,9 +27,8 @@ web::json::value readJsonFile(std::string const & jsonFileName)
    }
    catch (web::json::json_exception excep)
    {
    std::cout << "ERROR Parsing JSON: ";
    std::cout << excep.what();
    throw web::json::json_exception("Error Parsing JSON file");
    }

    return output;
    }
    }
  3. paddy74 created this gist Jun 19, 2019.
    35 changes: 35 additions & 0 deletions readJsonFile.hpp
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,35 @@
    #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)
    {
    std::cout << "ERROR Parsing JSON: ";
    std::cout << excep.what();
    }

    return output;
    }