Last active
July 8, 2018 21:08
-
-
Save shailrshah/0f310de820cfd25d24ed5017900ed649 to your computer and use it in GitHub Desktop.
JSON serialization/deserialization
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Student.java | |
package com.shail; | |
import lombok.*; | |
@Getter | |
@NoArgsConstructor | |
@ToString | |
class Student { | |
private int id; | |
private String name; | |
} | |
//////////////////////////////////////////////////// | |
//Main.java | |
package com.shail; | |
import com.fasterxml.jackson.databind.ObjectMapper; | |
import java.io.IOException; | |
public class Main { | |
private static ObjectMapper mapper = new ObjectMapper(); | |
private static final String jsonString = "{\"id\":\"21\", \"name\":\"Shail\"}"; | |
public static void main(String[] args) throws IOException { | |
Student student = mapper.readValue(jsonString, Student.class); | |
String studentString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(student); | |
System.out.println(student); | |
System.out.println(studentString); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment