Module 9: Serialization in C++
- First read this page then start the module with the GitHub classroom link below.
- Github Classroom Link: https://classroom.github.com/a/yPjHuC-R
Exercise 1: Convert to and from JSON.
In this exercise, you will write a toJson() method that will convert a C++ object to JSON document, saving the object state. And a fromJson()method that will convert from a JSON document to a C++ class object.
Steps:
-
Setup the environment: Review the code in the exercise1 directory in the module9 GitHub repository. Make note of the contents of each file, and how the
Makefileworks. -
Implement the
toJson()function: In the fileCat.cppimplement thetoJson()function convert an instance of the class to JSON. -
Update the
mainfunction: In the filemain.cppmodify themainfunction to call thetoJson()function for the “cheddar” instance of theCatclass. After calling the function, save the resulting JSON to a file calledcheddar.json. -
Compile and test: Compile and run
programto test yourtoJson()function.
$ make
$ ./program
From the command line, read the JSON file to confirm that your code worked correctly.
$ cat cheddar.json
-
Implement the
fromJson()function: In the fileCat.cppimplement thefromJson()function convert JSON document to an instance of theCatclass. -
Update the
mainfunction: In the filemain.cppmodify themainfunction to read thecheddar.jsonfile. Then call thefromJson()function for the “cheddar” instance of theCatclass. -
Compile and test: Compile and run
programto test yourfromJson()function.
$ make
$ ./program
Exercise 2: Saving and Loading a vector of class objects
In this exercise, you will write a toJson() method that will convert a C++ object to JSON document, saving the object state. And a fromJson()method that will convert from a JSON document to a C++ class object.
Steps:
-
Setup the environment: Review the code in the exercise2 directory in the module9 GitHub repository. Make note of the contents of each file, and how the
Makefileworks. -
Implement the
toJson()function: In the fileItem.cppimplement thetoJson()function convert an instance of the class to JSON. -
Update the
mainfunction: In the filemain.cppmodify themainfunction to call thetoJson()function for each instance of theItemclass in thecartvector. After calling the function, save the resulting JSON to a file calledcart.json.
Hint: review the lecture example:
json jsonOutput = jsoncars;
for (Car car : cars)
{
jsonOutput.at("cars").push_back(car.toJson());
}
// Write JSON to a file.
ofstream outfile("cars.json");
outfile << jsonOutput.dump(INDENT_SPACES);
outfile.close();
- Compile and test: Compile and run
cartto test yourtoJson()function.
$ make
$ ./cart
From the command line, read the JSON file to confirm that your code worked correctly.
$ cat cart.json
-
Implement the
fromJson()function: In the fileItem.cppimplement thefromJson()function convert JSON document to an instance of theItemclass. -
Update the
mainfunction: In the filemain.cppmodify themainfunction to read thecart.jsonfile. Then call thefromJson()function for each instance of theItemclass.
Hint: review the lecture example:
cars.clear();
// Read JSON from a file.
ifstream infile("cars.json");
json jsonFromFile = json::parse(infile);
infile.close();
for (json carJson : jsonFromFile.at("cars"))
{
cars.push_back(Car{carJson});
}
- Compile and test: Compile and run
cartto test yourfromJson()function.
$ make
$ ./cart