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 few test cases.
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
We’ll also discuss how the list view component we use works.
Your programming task for this lesson is fairly straightforward, and we’ll guide
you to it.
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’ll receive the MP1 Java test suites via email.
Move that file to the
app/src/test/java/edu/illinois/cs/cs124/ay2023/mp/test/ 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:
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.
Here’s one example.
This object has one field named
example with the value
In JSON, each object is enclosed in curly braces:
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.
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.
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 courses 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.
Next, let’s examine how we can use JSON in our Java programs. This is one of the many things that you should never do yourself! There are many great libraries that support JSON in Java 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 Java objects!
Let’s start with serialization. Here’s how to use Jackson to automatically generate JSON from our Java objects:
Now that we have a string, let’s look at how we can deserialize that
String and convert it back into a Java object.
The following homework problem is only for practice, but it may help you understand how to complete later parts of the MP.
Up until now we've used public constructors to create instances of our classes.
But there are other patterns for enabling class creation, and this problem explores one.
Specifically, we're going to show how to deserialize a CSV
String into instances of a custom class.
final class named
Course should not provide a public constructor, although you will probably want to create a
Course should provide a class method named
fromCSV which takes a single
String argument and
returns an array of
Course maintains a
department and a
Here's how they are loaded from a CSV
Given the following CSV contents:
CS, 125 IE, 333 MUS, 230
You should return an array containing three
Course instances: the first with
number="125", the second with
Your array should contain the courses in the same order in which they appear in the CSV
fromCSV method should assert that the passed
String is not
null or empty.)
Course should provide getters for the department and number named
To complete this problem you'll want to review our previous work with CSV
Strings, and apply what you now know
about constructing objects. Have fun!
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 courses at the University of Illinois. 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.
Finally, let’s zero in on our first MP1 test case, and discuss how to correctly implement
Comparable for your
Your default comparison should compare summaries first by their number, and next by their subject.
For example, “IS 100” should be shown after “CS 100” but before “STAT 200”.
Let’s briefly examine where you need to make changes, and how to test your
Once you’re passing
test0_SummaryComparison, it’s a small change to also complete
The video walkthroughs above should give you a hint.
Also notice that, once a class implements
Comparable, you can sort it using Java’s built-in sorting methods, as shown below.
Do not implement your own sort method!
We’ll do that together later.
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
Summaryclass and in the
MainActivity, so that summaries are sorted properly when the app starts