1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
|
//Main Method
#include "MovieLibrary.h"
#include <iostream>
#include <jsoncpp/json/json.h>
using namespace std;
int main(int argc, const char* argv[])
{
MovieLibrary movieLibrary;
movieLibrary = MovieLibrary("movies.json");
movieLibrary.toJsonFile("moviesCPP.json");
}
//MovieDescription Header
class MovieDescription
{
public:
string title;
string rating;
string release;
string runtime;
string plot;
string filename;
string genre;
string actors;
MovieDescription();
MovieDescription(string title, string rating, string release, string runtime, string plot, string filename, string genre, string actors);
string getGenre();
void setGenre(string genre);
string getActors();
void setActors(string actors);
void setTitle(string title);
string getTitle();
void setRating(string rating);
string getRating();
void setRelease(string release);
string getRelease();
void setRuntime(string runtime);
string getRuntime();
void setPlot(string plot);
string getPlot();
void setFilename(string filename);
string getFilename();
};
//MovieDescription Code
using namespace std;
MovieDescription::MovieDescription()
{
title = " ";
rating = " ";
release = " ";
runtime = " ";
plot = " ";
filename = " ";
genre = " ";
actors = " ";
}
MovieDescription::MovieDescription(string title, string rating, string release,
string runtime, string plot, string filename,
string genre, string actors)
{
this->title = title;
this->rating = rating;
this->release = release;
this->runtime = runtime;
this->plot = plot;
this->filename = filename;
this->genre = genre;
this->actors = actors;
}
void MovieDescription::setTitle(string title)
{
this->title = title;
}
string MovieDescription::getTitle()
{
return title;
}
void MovieDescription::setRating(string rating)
{
this->rating = rating;
}
string MovieDescription::getRating()
{
return rating;
}
void MovieDescription::setRelease(string release)
{
this->release = release;
}
string MovieDescription::getRelease()
{
return release;
}
void MovieDescription::setRuntime(string runtime)
{
this->runtime = runtime;
}
string MovieDescription::getRuntime()
{
return runtime;
}
void MovieDescription::setPlot(string plot)
{
this->plot = plot;
}
string MovieDescription::getPlot()
{
return plot;
}
void MovieDescription::setFilename(string filename)
{
this->filename = filename;
}
string MovieDescription::getFilename()
{
return filename;
}
string MovieDescription::getGenre()
{
return genre;
}
void MovieDescription::setGenre(string genre)
{
this->genre = genre;
}
string MovieDescription::getActors()
{
return actors;
}
void MovieDescription::setActors(string actors)
{
this->actors = actors;
}
//MovieLibrary Header
class MovieLibrary
{
private:
vector<MovieDescription> movieLib;
int arraySize;
public:
MovieLibrary();
MovieLibrary(string jsonFileName);
bool add(MovieDescription aClip);
bool remove(string aTitle);
MovieDescription* get(string aTitle);
vector<string> getTitles();
int indexOf(string aTitle);
void toJsonFile(string jsonFileName);
protected:
string name;
map<string, MovieDescription> users;
};
//MovieLibrary Code
#include "MovieLibrary.h"
#include <string>
#include <iostream>
#include <fstream>
#include <cstddef>
#include <jsoncpp/json/json.h>
using namespace std;
MovieLibrary::MovieLibrary()
{
vector<MovieDescription> movieLib;
int arraySize = 0;
}
bool MovieLibrary::add(MovieDescription aClip)
{
movieLib.push_back(aClip);
arraySize++;
return true;
}
bool MovieLibrary::remove(string aTitle)
{
int i = indexOf(aTitle);
if(i != -1){
movieLib.erase(movieLib.begin() + i);
arraySize--;
}
return true;
}
MovieDescription* MovieLibrary::get(string aTitle)
{
int i = indexOf(aTitle);
if(i == -1){
return NULL;
}
return &movieLib.at(i);
}
vector<string> MovieLibrary::getTitles()
{
vector<string> s;
for(int i = 0; i < movieLib.size(); i++)
{
s.push_back(movieLib.at(i).getTitle());
}
return s;
}
int MovieLibrary::indexOf(string aTitle)
{
for(int i = 0; i < movieLib.size(); i++)
if(movieLib.at(i).getTitle() == aTitle)
{
return i;
}
return -1;
}
MovieLibrary::MovieLibrary(string jsonFileName)
{
ifstream jsonFile(jsonFileName.c_str());
string line;
cout << "The content of the Json file as a string: " << endl;
if(jsonFile.is_open()){
while(getline(jsonFile,line)){
cout << line << endl;
}
}else{
cout << "Json file not opened properly" << endl;
}
jsonFile.close();
Json::Reader reader;
Json::Value root;
std::ifstream json(jsonFileName.c_str(), std::ifstream::binary);
bool parseSuccess = reader.parse(json,root,false);
if(parseSuccess){
Json::Value::Members mbr = root.getMemberNames();
for(vector<string>::const_iterator i = mbr.begin(); i!= mbr.end(); i++){
Json::Value jsonUser = root[*i];
string nameStr = "name";
if(nameStr.compare(*i)==0){
name = jsonUser.asString();
}else{
string title = jsonUser["Title"].asString();
string rating = jsonUser["Rated"].asString();
string release = jsonUser["Released"].asString();
string runtime = jsonUser["Runtime"].asString();
string plot = jsonUser["Plot"].asString();
string filename = jsonUser["Filename"].asString();
string actors = jsonUser["Actors"].asString();
string genre = jsonUser["Genre"].asString();
MovieDescription * user = new MovieDescription(title, rating, release, runtime, plot, filename, actors, genre);
users[*i] = *user;
}
}
}
}
void MovieLibrary::toJsonFile(string jsonFileName)
{
Json::Value jsonLib;
for(std::map<string,MovieDescription>::iterator i = users.begin(); i!= users.end(); i++){
string key = i->first;
Json::Value jsonUser;
MovieDescription usr = users[key];
jsonUser["Title"] = usr.title;
jsonUser["Rated"] = usr.rating;
jsonUser["Released"] = usr.release;
jsonUser["Runtime"] = usr.runtime;
jsonUser["Plot"] = usr.plot;
jsonUser["Filename"] = usr.filename;
jsonUser["Actors"] = usr.actors;
jsonUser["Genre"] = usr.genre;
jsonLib[key] = jsonUser;
}
Json::StyledStreamWriter ssw(" ");
std::ofstream jsonOutFile(jsonFileName.c_str(), std::ofstream::binary);
ssw.write(jsonOutFile, jsonLib);
jsonOutFile.flush();
jsonOutFile.close();
}
|