day 6: Serializationa and Deserialization
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user