school/cs142/lab09/io.cpp

31 lines
872 B
C++

#include <string>
#include <fstream>
#include <streambuf>
#include <stdexcept>
using namespace std;
#include "jsoncpp/json/json.h"
#include "io.h"
#include "restaurant.h"
vector<restaurant> parse_file(string filename) {
vector<restaurant> restaurants;
ifstream infile(filename.c_str());
string file_contents((istreambuf_iterator<char>(infile)),
istreambuf_iterator<char>());
Json::Value root;
Json::Reader reader;
bool good_parse = reader.parse(file_contents, root);
if(not good_parse) {
throw runtime_error(reader.getFormattedErrorMessages());
}
for(unsigned int i = 0; i < root.size(); i++) {
string name = root[i]["name"].asString();
int score = root[i]["score"].asInt();
restaurant r(name, score);
restaurants.push_back(r);
}
return restaurants;
}