Kotlin
Java

MP1: Serialization and Data Loading
Kotlin

Created By: Geoffrey Challen
/ Updated: 2022-10-27

Let's continue our work on the machine project! We'll begin our first real project checkpoint, MP1, and get some practice with Jackson, the JSON serialization library used in our app. We'll install the test suites and begin work on our first test case.

But most of this lesson is tracing how data flows through our app, and discussing various ways in which data is stored and retrieved from Strings. Don't worry though—your programming task for this lesson is straightforward.

Note that this lesson is a bit on the longer side. It should average out with the next few.

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. You should make sure to commit both immediately before and immediately after we install the MP1 test suites. (If there are no changes to commit beforehand, that's fine.)

You should have received the MP1 test suites via email. Move the file to the app/src/test/kotlin/edu/illinois/cs/cs124/ay2022/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

Next 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 Kotlin 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 Kotlin 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 places 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.

Jackson

Next, let's examine how we can use JSON in our Kotlin programs. This is one of the many things that you should never do yourself! There are many great libraries that support JSON in Kotlin programs.

We're going to be using a library called Jackson to assist us with serialization. Next, let's look at how we can use Jackson to serialize and deserialize our Kotlin objects!

Serialization

Let's start with serialization. Here's how to use Jackson to automatically generate JSON from our Kotlin objects:

Show how to use Jackson to serialize a Java object. Demonstrate how setters are needed and how the field names are defined.

Deserialization

Now that we have a string, let's look at how we can deserialize that String and convert it back into a Kotlin object.

Show how to use Jackson to deserialize a Kotlin object. Point out the class literal, and show how the object methods can change.

Practice with Serialization

The following homework problem is only for practice, but it may help you understand how to complete later parts of the MP.

Show how to complete the homework problem above. Feel free to cover multiple approaches!

Data Model

Now that we understand a bit about serialization, let's return to our app and see it in action!

Your app works with data about favorite places in the Champaign-Urbana area. 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 examine where the data comes from and how it flows through your app.

Review the places model and trace the data path.

Fixing the Place Data Path

Finally, let's zero in on our first MP1 test case. Something is wrong on the data path loading data from the places.csv file! Your job is to hunt down and fix that bug.

If you have some extra time after today's lesson, you may want to move on and begin work on the next lesson, which also covers MP1.

No Homework for This Lesson

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.

Right now your goal should be to

  1. install the MP1 test suites
  2. get your code to compile, and
  3. fix the bug with data loading!

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

MP1 Scores

More Practice

Need more practice? Head over to the practice page.