school/cs142/lab09/io.cpp

31 lines
872 B
C++
Raw Normal View History

2016-04-06 20:45:34 -07:00
#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;
}