Kotlin
Java

MP1: Serialization and Sorting
Java

Created By: Geoffrey Challen
/ Updated: 2021-10-19

Let's continue our work on the machine project! Today we'll begin our first real project checkpoint, MP1. We'll install the test suites and begin work on our first test case.

But most of today's lesson is about serialization, the process of converting data to a String and back again. Don't worry though—your programming task for today's lesson is straightforward.

Getting Started

For MP1 we'll be continuing on with the project that you started working on for MP0. You'll need the environment that you set up during that checkpoint to proceed.

Before you can get to work on MP1 in earnest, we need to install the MP1 test suites. Before we continue, please commit your work.

Since you are using Java, download the MP1 tests here and move the file to the app/src/test/java/edu/illinois/cs/cs124/ay2021/mp/ directory in your machine project. You'll also want to reconfigure grade.yaml in the root directory of your project to request that we grade Checkpoint 1. Let's look at how to do both of these things together:

Download and install the MP1 test suites and reconfigure grade.yaml.

Serialization

Today we'll examine how data flows through our app. Along the way, we'll naturally encounter a process called serialization. But what does that mean? Wikipedia defines serialization as:

In computing, serialization is the process of translating a data structure or object state into a format that can be stored (for example, in a file or memory data buffer) or transmitted (for example, across a computer network) and reconstructed later (possibly in a different computer environment). When the resulting series of bits is reread according to the serialization format, it can be used to create a semantically identical clone of the original object. For many complex objects, such as those that make extensive use of references, this process is not straightforward. Serialization of object-oriented objects does not include any of their associated methods with which they were previously linked.

Read through and discuss the Wikipedia definition of serialization. Focus on semantic equivalence. Also point out that when we serialize and deserialize objects we don't save their methods, just state.

JSON

If serialization is the process of converting object state into some format, what format should we use? There are many different options. But one particularly popular and ubiquitous object serialization format that we use in the MP is known as JSON: JavaScript Object Notation.

Here's one example. This object has one field named example with the value test:

In JSON, each object is enclosed in curly braces: { and }s. Arrays are enclosed in square brackets:

Numeric literals are not quoted, but all field names and most other values are.

Note that JSON requires the entire object to be represented as a string. For actual Strings and certain other Java classes there is an obvious way to do this. In other cases, it's a bit more interesting. We'll look at how some other Java classes convert themselves to Strings in the examples below.

Why JSON?

There are really two questions here. First, why serialization at all? Second, why the JSON format?

As we discussed above, serialization is one very powerful way to pass data between two different computer programs. In your app, JSON is how data about the list of restaurants is passed from the server to the client, which would normally be running on two different machines and communicating over a network connection. Because there are JSON libraries for almost every programming language, most can use JSON to communicate, meaning that a client written in Kotlin, or Java, or Python, or Lisp can talk to a server in Java, or Python, or Go, or Haskell. We'll see a concrete example of serialization when we examine the data path in our app below.

Data Model

Now that we understand a bit about serialization, let's return to our app and see it in action! We'll defer a discussion of the serialization library that we use, Jackson, to tomorrow's lesson.

Your app works with data about local restaurants. This involves both serializing that data to and from JSON, but also designing data models using class design features. Let's look at the core data model you'll be using during MP1, and briefly examine where the data comes from.

Review the restaurant model, and also show the restaurant CSV and a bit of the server-side code.

Comparing Restaurants

Finally, let's zero in on our first MP1 test case, and discuss how to implement a Comparator for your Restaurant class. Comparator is very similar to Comparable, except that instead of being an instance method that compares one object with a passed object for order, it's a method that compares two passed objects for order.

No Homework Today

As a reminder, on lessons where we focus on the machine project we will not assign a homework problem! However, the lesson will usually focus on helping you complete a particular part of the MP test suite, and so we encourage you to spend time on that in lieu of a homework problem.

Today your goal should be to

  1. install the MP1 test suites
  2. get your code to compile, and
  3. complete the restaurant comparator to pass the first test suite.

If you get stuck, find us for help on the help site or forum.