Jackson – ObjectMapper

Use this if you want to make json files or read json text strings

Class:

OrderSerializer
id
name

Problem(s):
Want to get json out in a file
Need to read/create object from json later

Steps:
(Add maven/grade dependencies)
Create pojo to hold the data ie “OrderSerializer”, add jackson databind

Create some ObjectMappers that will Map Objects to Json or Json to Objects


Caveats: I needed to add a constructor with super(); in it, not sure why.

OrderSerializerImplemeted.java

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.File;
import java.io.IOException;

/*
Some Jackson Object Mapper Code
https://www.youtube.com/watch?v=pv1VCFWTP-I&ab_channel=SwatechTalks
 */
public class OrderSerializerImplemented {
    public static void main(String[] args) {
        var order1 = new OrderSerializer("ID123", "Joey Mc Doughey");
        var order2 = new OrderSerializer("IDID1010", "Kevin Mc Douchy");

        ObjectMapper objectMapper = new ObjectMapper();

        try {
            // Writing files to files, same them for later.
            objectMapper.writeValue(new File("target/order1.json"), order1);
            objectMapper.writeValue(new File("target/order2.json"), order2);
        } catch (IOException e) {
            e.printStackTrace();
        }

        // load from a string literal
        try {
            ObjectMapper objectMapper1 = new ObjectMapper();
            String orderJson = "{\"id\":\"ID123\",\"name\":\"Joey Mc Doughey\"}";
            // Put the String data in objectMapper using OrderSerializer.class as the Class to use
            OrderSerializer coolOrder = objectMapper1.readValue(orderJson, OrderSerializer.class);
            System.out.println(coolOrder.getId());
        } catch (final Exception e){
            e.printStackTrace();
        } finally {
            System.out.printf("done with reading object from file");
        }

        // Using JsonNode to map string as an json object
        String orderJson = "{\"id\":\"ID123\",\"name\":\"Blaze Mc Weedy\"}";
        ObjectMapper objectMapper2 = new ObjectMapper();
        JsonNode jsonNode = null;
        try {
            jsonNode = objectMapper2.readTree(orderJson);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        System.out.println("jsonNode.get(\"name\").asText() = " + jsonNode.get("name").asText());
        System.out.println("jsonNode.get(\"id\").asText() = " + jsonNode.get("id").asText());
        // now lets write it all out as a string through objectMapper
        ObjectMapper objectMapper3 = new ObjectMapper();
        try {
            System.out.println("Here's the whole Json String or an error: ");
            System.out.println(objectMapper3.writeValueAsString(jsonNode));
            System.out.println("Here's the same data, just nicer to look at.");
            System.out.println(jsonNode.toPrettyString());
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
    }
}


Here’s the console output:

ID123
done with reading object from filejsonNode.get("name").asText() = Blaze Mc Weedy
jsonNode.get("id").asText() = ID123
Here's the whole Json String or an error: 
{"id":"ID123","name":"Blaze Mc Weedy"}
Here's the same data, just nicer to look at.
{
  "id" : "ID123",
  "name" : "Blaze Mc Weedy"
}

Process finished with exit code 0