adding cs142 code
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
#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;
|
||||
}
|
||||
Reference in New Issue
Block a user