From b61b6345b73a0c1d7ee094eb37f5d8e8b8658861 Mon Sep 17 00:00:00 2001 From: Sami Date: Fri, 11 Oct 2024 08:22:17 -0400 Subject: [PATCH] day 6: Serializationa and Deserialization --- .idea/uiDesigner.xml | 124 ++++++++++++++++++ jsonServer/students.json | 11 ++ .../zacksolutions/DaySix/POJODeserialize.java | 49 +++++++ .../SerializationDeserializationTest.java | 42 ++++++ 4 files changed, 226 insertions(+) create mode 100644 .idea/uiDesigner.xml create mode 100644 src/test/java/zacksolutions/DaySix/POJODeserialize.java create mode 100644 src/test/java/zacksolutions/DaySix/SerializationDeserializationTest.java diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml new file mode 100644 index 0000000..2b63946 --- /dev/null +++ b/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/jsonServer/students.json b/jsonServer/students.json index f949ca8..4243685 100644 --- a/jsonServer/students.json +++ b/jsonServer/students.json @@ -99,6 +99,17 @@ "name": "Leena", "location": "Denver" }, + { + "id": "9", + "courses": [ + "C++", + "Game Development", + "Graphics Programming" + ], + "phone": "+1-555-4455", + "name": "Leena", + "location": "Denver" + }, { "id": "9", "courses": [ diff --git a/src/test/java/zacksolutions/DaySix/POJODeserialize.java b/src/test/java/zacksolutions/DaySix/POJODeserialize.java new file mode 100644 index 0000000..ab995dd --- /dev/null +++ b/src/test/java/zacksolutions/DaySix/POJODeserialize.java @@ -0,0 +1,49 @@ +package zacksolutions.DaySix; + +public class POJODeserialize { + String author; + String category; + String serialNum; + double price; + String name; + + public String getAuthor() { + return author; + } + + public void setAuthor(String author) { + this.author = author; + } + + public String getCategory() { + return category; + } + + public void setCategory(String category) { + this.category = category; + } + + public String getSerialNum() { + return serialNum; + } + + public void setSerialNum(String serialNum) { + this.serialNum = serialNum; + } + + public double getPrice() { + return price; + } + + public void setPrice(double price) { + this.price = price; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} diff --git a/src/test/java/zacksolutions/DaySix/SerializationDeserializationTest.java b/src/test/java/zacksolutions/DaySix/SerializationDeserializationTest.java new file mode 100644 index 0000000..4b36a12 --- /dev/null +++ b/src/test/java/zacksolutions/DaySix/SerializationDeserializationTest.java @@ -0,0 +1,42 @@ +package zacksolutions.DaySix; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.testng.annotations.Test; + +public class SerializationDeserializationTest { + + //converting the pojo object to a Json data... Serialization + @Test(priority = 1) + void SerializationTest() throws JsonProcessingException { + POJODeserialize pojo = new POJODeserialize(); + pojo.setAuthor("George R R Martin"); + pojo.setCategory("Fantasy"); + pojo.setSerialNum("D25K"); + pojo.setPrice(18.99); + pojo.setName("Song Of Ice and Fire"); + + ObjectMapper objMapper = new ObjectMapper(); + String jsonData = objMapper.writerWithDefaultPrettyPrinter().writeValueAsString(pojo); + System.out.println(jsonData); + } + // converting jsonData to a pojo... Deserialization + @Test(priority = 2) + void DeserializationTest() throws JsonProcessingException { + String jsonData = "{\n" + + " \"author\" : \"George R R Martin\",\n" + + " \"category\" : \"Fantasy\",\n" + + " \"serialNum\" : \"D25K\",\n" + + " \"price\" : 18.99,\n" + + " \"name\" : \"Song Of Ice and Fire\"\n" + + "}"; + + ObjectMapper objectMapper = new ObjectMapper(); + POJODeserialize pojo = objectMapper.readValue(jsonData, POJODeserialize.class); + System.out.println(pojo.getAuthor()); + System.out.println(pojo.getCategory()); + System.out.println(pojo.getSerialNum()); + System.out.println(pojo.getPrice()); + System.out.println(pojo.getName()); + } +}