Kotlin
Java

MP1: Serialization and Searching
Kotlin

Created By: Geoffrey Challen
/ Updated: 2021-11-02

Let's make more progress on MP1! Today we'll complete a search method that we'll later us to enable search in our restaurant view screen. We'll also get some practice with Jackson, the JSON serialization library used in our app.

Jackson

Let's pick up our discussion of serialization from yesterday and 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!

Serialization

Let's start with serialization. Here's how to use Jackson to automatically generate JSON from our Java 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 Java 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!

Solution Walkthrough

Restaurant Search

Now let's return our attention to the project and work on the next test case.

Improve Restaurant Data Processing

In this case, we have a helper test case set up to handle first. It will expose some issues with the way that we are parsing the restaurant CSV file, and with our Restaurant class design. Let's fix those together first.

Search Method

Now that we're loading the right data, we can complete our Restaurant search method. This is quite similar to a homework problem that we might assign! Let's review the problem description and discuss how to structure our algorithm together.

As a more complete description of your search method, you should write a List<Restaurant> extension method search that a search String. If the search String exactly matches a cuisine value, you should return only all of the restaurants with that cuisine value. Otherwise you should return all Restaurants where either the name or cuisine contain the search String. All comparisons should be case-insensitive, so both "PiZzA" and "pIzZa" match the cuisine "pizza" or the name "Pizza Palace". You should also remove surrounding whitespace from the search String. If the search String is blank or empty, you should return a copy of the entire list of restaurants.

Kotlin extension methods allow you to add methods to existing classes. Inside them you access the object that the method is called on using this. So the following two pieces of code do the same thing, although they are called differently:

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 complete the restaurant search method and pass the next test case! If you get stuck, find us for help on the help site or forum.