JSON DTO Exercise

Part 1

Answer the following questions: link for help

  • What does JSON stand for?
  • What is the difference between JSON and XML?
  • For what is JSON generally used for?
  • Write down the 6 data types in JSON.
  • Write down the 4 JSON syntax rules.

Part 2

  1. Create a new file called account.json
  2. Create a JSON object with the following properties in account.json (the notation is pseudo code):
 `firstName` (string)
 `lastName` (string)
 `birthDate` (string)
 `address` (object)
        - `street` (string)
        - `city` (string)
        - `zipCode` (integer)
 `account` (object)
        - `id` (string)
        - `balance` (string)
        - `isActive` (boolean)
  1. Wrap the JSON object in an array
  2. Add 5 more accounts to the array
  3. Save the file
  4. Open the file in a browser and verify that it is valid JSON. You do this by paste the absolute path to the file in the browser address bar. For example: `file:///Users/username/Desktop/account.json

Part 3

  1. Open a Java project in your IDE and create classes for the JSON object you created in part 2.
  2. Create a method that can read the JSON object from the file and return an array of Account objects.
  3. You can use a JAVA library to convert the JSON object to a JAVA object or vice versa. For example Jackson.
  4. Create a DTO class that has the following properties:
 `fullName` (string)
 `city` (string)
 `zipCode` (string)
 `isActive` (string)
  1. Create a method that can print out the DTO objects in the array in a nice format.

Hint! - if you need help reading the JSON file in Java

Here’s a Java method called `readJsonFromFile` that reads a JSON file from the resources folder into a `String`:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.stream.Collectors;

public class JsonFileReader {

    public static String readJsonFromFile(String filename) {
        // Get the resource file from the classpath
        InputStream inputStream = JsonFileReader.class.getClassLoader().getResourceAsStream(filename);

        if (inputStream == null) {
            throw new IllegalArgumentException("File not found: " + filename);
        }

        // Read the file content into a string
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8))) {
            return reader.lines().collect(Collectors.joining("\n"));
        } catch (IOException e) {
            throw new RuntimeException("Failed to read the file: " + filename, e);
        }
    }
}

Explanation

  1. InputStream: This reads the file as a stream from the resources folder using getResourceAsStream().
  2. BufferedReader: Wraps the input stream for efficient reading.
  3. Collectors.joining("\n"): Collects the lines of the file into a single string, separated by newlines.
  4. Exception Handling: If the file is not found, an IllegalArgumentException is thrown. If an I/O error occurs, a RuntimeException is thrown.

How to use

Place your JSON file in the resources folder, and call the method like this:

String jsonString = JsonFileReader.readJsonFromFile("data.json");
System.out.println(jsonString);

This will read the contents of the data.json file into a String. Make sure the JSON file is in the resources folder within the project structure.


Top

3. semester efterår 2024