Wednesday, July 17, 2019

Kotlin in Action: Chapter 5 Exercises

Just like the last few weeks, here's a practice problem, this time for chapter 5.

Exercise 1: Json Manipulation

In this example we will continue using the Gson library, We will manipulate multiple JsonArrays to return a list mapping the user's first name and last name to the titles of the books that the user has checked out.
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import static java.util.stream.StreamSupport.stream;

public class JavaExample {

  public static void main(String[] args) {
    String usersJson = "[\n" +
                       "  {\n" +
                       "    \"id\": \"543\",\n" +
                       "    \"username\": \"john123\",\n" +
                       "    \"firstName\": \"John\",\n" +
                       "    \"lastName\": \"Smith\",\n" +
                       "    \"email\": \"john@smith.com\"\n" +
                       "  }, " +
                       "  {\n" +
                       "    \"id\": \"438\",\n" +
                       "    \"username\": \"janedoe5\",\n" +
                       "    \"firstName\": \"Jane\",\n" +
                       "    \"lastName\": \"Doe\",\n" +
                       "    \"email\": \"jane.doe@gmail.com\"\n" +
                       "  }\n" +
                       "]";
    String booksJson = "[\n" +
                       "  {\"id\": \"1\", \"title\": \"Kotlin in Action\"},\n" +
                       "  {\"id\": \"2\", \"title\": \"Kotlin in Action\"},\n" +
                       "  {\"id\": \"3\", \"title\": \"Learning RxJava\"},\n" +
                       "  {\"id\": \"4\", \"title\": \"Refactoring\"},\n" +
                       "  {\"id\": \"5\", \"title\": \"Grokking Algorithms\"},\n" +
                       "  {\"id\": \"6\", \"title\": \"Code Complete\"}\n" +
                       "]";
    String checkoutsJson = "[\n" +
                           "  {\"userId\": \"543\", \"bookId\": \"1\"},\n" +
                           "  {\"userId\": \"543\", \"bookId\": \"5\"},\n" +
                           "  {\"userId\": \"438\", \"bookId\": \"2\"},\n" +
                           "  {\"userId\": \"438\", \"bookId\": \"3\"}\n" +
                           "]";

    JsonParser parser = new JsonParser();
    JsonArray users = parser.parse(usersJson).getAsJsonArray();
    JsonArray books = parser.parse(booksJson).getAsJsonArray();
    JsonArray checkouts = parser.parse(checkoutsJson).getAsJsonArray();

    JsonArray userCheckouts = stream(users.spliterator(), false)
        .map(JsonElement::getAsJsonObject)
        .flatMap(user -> stream(checkouts.spliterator(), false)
            .map(JsonElement::getAsJsonObject)
            .filter(checkout -> user.get("id").getAsString().equals(
                checkout.get("userId").getAsString()))
            .map(checkout -> {
              JsonObject jo = new JsonObject();
              jo.addProperty("firstName", user.get("firstName").getAsString());
              jo.addProperty("lastName", user.get("lastName").getAsString());
              jo.addProperty("bookId", checkout.get("bookId").getAsString());
              return jo;
            }))
        .flatMap(userCheckout -> stream(books.spliterator(), false)
            .map(JsonElement::getAsJsonObject)
            .filter(book -> userCheckout.get("bookId").getAsString().equals(
                book.get("id").getAsString()))
            .map(book -> {
              JsonObject jo = userCheckout.deepCopy();
              jo.addProperty("title", book.get("title").getAsString());
              jo.remove("bookId");
              return jo;
            }))
        .collect(JsonArray::new, JsonArray::add, JsonArray::addAll);

    System.out.println(userCheckouts);
  }
}
Similar to previous weeks, an answer will be shared in a follow up post. (And the answer can be found here.)