Module 9: Serialization in C++ | CMSC 240 Software Systems Development - Spring 2024

Module 9: Serialization in C++

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:

  1. 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.

  2. Implement the toJson() function: In the file Cat.cpp implement the toJson() function convert an instance of the class to JSON.

  3. Update the main function: In the file main.cpp modify the main function to call the toJson() function for the “cheddar” instance of the Cat class. After calling the function, save the resulting JSON to a file called cheddar.json.

  4. Compile and test: Compile and run program to test your toJson() function.

$ make
$ ./program

From the command line, read the JSON file to confirm that your code worked correctly.

$ cat cheddar.json
  1. Implement the fromJson() function: In the file Cat.cpp implement the fromJson() function convert JSON document to an instance of the Cat class.

  2. Update the main function: In the file main.cpp modify the main function to read the cheddar.json file. Then call the fromJson() function for the “cheddar” instance of the Cat class.

  3. Compile and test: Compile and run program to test your fromJson() 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:

  1. 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.

  2. Implement the toJson() function: In the file Item.cpp implement the toJson() function convert an instance of the class to JSON.

  3. Update the main function: In the file main.cpp modify the main function to call the toJson() function for each instance of the Item class in the cart vector. After calling the function, save the resulting JSON to a file called cart.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();
  1. Compile and test: Compile and run cart to test your toJson() function.
$ make
$ ./cart

From the command line, read the JSON file to confirm that your code worked correctly.

$ cat cart.json
  1. Implement the fromJson() function: In the file Item.cpp implement the fromJson() function convert JSON document to an instance of the Item class.

  2. Update the main function: In the file main.cpp modify the main function to read the cart.json file. Then call the fromJson() function for each instance of the Item 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});
    }
  1. Compile and test: Compile and run cart to test your fromJson() function.
$ make
$ ./cart