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/iJwzRNkH
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
Makefile
works. -
Implement the
toJson()
function: In the fileCat.cpp
implement thetoJson()
function convert an instance of the class to JSON. -
Update the
main
function: In the filemain.cpp
modify themain
function to call thetoJson()
function for the “cheddar” instance of theCat
class. After calling the function, save the resulting JSON to a file calledcheddar.json
. -
Compile and test: Compile and run
program
to 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.cpp
implement thefromJson()
function convert JSON document to an instance of theCat
class. -
Update the
main
function: In the filemain.cpp
modify themain
function to read thecheddar.json
file. Then call thefromJson()
function for the “cheddar” instance of theCat
class. -
Compile and test: Compile and run
program
to 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
Makefile
works. -
Implement the
toJson()
function: In the fileItem.cpp
implement thetoJson()
function convert an instance of the class to JSON. -
Update the
main
function: In the filemain.cpp
modify themain
function to call thetoJson()
function for each instance of theItem
class in thecart
vector. 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
cart
to 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.cpp
implement thefromJson()
function convert JSON document to an instance of theItem
class. -
Update the
main
function: In the filemain.cpp
modify themain
function to read thecart.json
file. Then call thefromJson()
function for each instance of theItem
class.
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
cart
to test yourfromJson()
function.
$ make
$ ./cart