├── README.md ├── pom.xml ├── post.json ├── src └── main │ └── java │ └── net │ └── javaguides │ └── jackson │ ├── JacksonJsonToList.java │ ├── JacksonJsonToMap.java │ ├── JacksonJsonToSet.java │ ├── JacksonListToJson.java │ ├── JacksonMapToJson.java │ ├── JacksonSetToJson.java │ ├── ReadJsonUsingJsonParser.java │ ├── WriteJsonUsingJsonGenerator.java │ ├── annotations │ ├── Demo.java │ ├── Employee.java │ ├── JsonIncludeAnnotationTest.java │ └── User.java │ ├── ignore │ ├── CustomerDTO.java │ ├── IgnoreFieldTest.java │ ├── JsonIgnoreTypeTest.java │ └── UserDTO.java │ ├── jsontopojo │ └── JacksonJsonToPojo.java │ └── pojotojson │ ├── JacksonPojoToJson.java │ ├── Post.java │ └── Tag.java └── target ├── Jackson-json-tutorial-0.0.1-SNAPSHOT.jar ├── classes └── net │ └── javaguides │ └── jackson │ ├── JacksonJsonToList.class │ ├── JacksonJsonToMap.class │ ├── JacksonJsonToSet.class │ ├── JacksonListToJson.class │ ├── JacksonMapToJson.class │ ├── JacksonSetToJson.class │ ├── ReadJsonUsingJsonParser.class │ ├── WriteJsonUsingJsonGenerator.class │ ├── annotations │ ├── Demo.class │ ├── Employee.class │ ├── JsonIncludeAnnotationTest.class │ └── User.class │ ├── ignore │ ├── CustomerDTO.class │ ├── IgnoreFieldTest.class │ ├── JsonIgnoreTypeTest.class │ ├── UserDTO$Name.class │ └── UserDTO.class │ ├── jsontopojo │ └── JacksonJsonToPojo.class │ └── pojotojson │ ├── JacksonPojoToJson.class │ ├── Post.class │ └── Tag.class ├── maven-archiver └── pom.properties └── maven-status └── maven-compiler-plugin └── compile └── default-compile ├── createdFiles.lst └── inputFiles.lst /README.md: -------------------------------------------------------------------------------- 1 | # jackson-json-tutorial 2 | Tutorial and examples of Jackson APIs 3 | 4 |
5 |

6 | On Java Guides:

7 |
8 |
9 | 12 | 15 | 18 | 21 | 24 | 27 | 30 | 33 | 36 |
37 |
38 | 41 | 44 |
45 |
46 | 49 | 52 | 55 |
56 |
57 | 60 | 63 |
64 |
65 | The source code of these tutorials available on my GitHub repository at https://github.com/RameshMF/jackson-json-tutorial
66 |

67 | Other Interesting Writeups:

68 | 71 | 74 | 77 | 80 | 83 | 86 | 89 | 92 | 95 | 98 | 101 | 104 | 107 | 110 |
111 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 4 | 4.0.0 5 | javaguides 6 | Jackson-json-tutorial 7 | 0.0.1-SNAPSHOT 8 | Jackson-json-tutorial 9 | Jackson-json-tutorial 10 | 11 | 12 | 13 | com.fasterxml.jackson.core 14 | jackson-databind 15 | 2.9.8 16 | 17 | 18 | 19 | 20 | 21 | 22 | org.apache.maven.plugins 23 | maven-compiler-plugin 24 | 3.8.0 25 | 26 | 8 27 | 8 28 | 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /post.json: -------------------------------------------------------------------------------- 1 | { 2 | "id" : 100, 3 | "title" : "Jackson JSON API Guide", 4 | "description" : "Post about Jackson JSON API", 5 | "content" : "HTML content here", 6 | "tags" : [ { 7 | "id" : 1, 8 | "name" : "JSON" 9 | }, { 10 | "id" : 2, 11 | "name" : "Java" 12 | }, { 13 | "id" : 3, 14 | "name" : "Jackson" 15 | } ] 16 | } -------------------------------------------------------------------------------- /src/main/java/net/javaguides/jackson/JacksonJsonToList.java: -------------------------------------------------------------------------------- 1 | package net.javaguides.jackson; 2 | 3 | import java.io.IOException; 4 | import java.util.ArrayList; 5 | import java.util.Iterator; 6 | import java.util.List; 7 | 8 | import com.fasterxml.jackson.core.JsonParseException; 9 | import com.fasterxml.jackson.databind.JsonMappingException; 10 | import com.fasterxml.jackson.databind.ObjectMapper; 11 | import com.fasterxml.jackson.databind.SerializationFeature; 12 | 13 | public class JacksonJsonToList { 14 | 15 | @SuppressWarnings("unchecked") 16 | public static void main(String[] args) throws JsonParseException, JsonMappingException, IOException { 17 | 18 | // Create ObjectMapper object. 19 | ObjectMapper mapper = new ObjectMapper(); 20 | mapper.enable(SerializationFeature.INDENT_OUTPUT); 21 | 22 | String json = "[ \"C\", \"C++\", \"Java\", \"Java EE\", \"Python\", \"Scala\", \"JavaScript\" ]"; 23 | 24 | List progLangs = new ArrayList<>(); 25 | progLangs = mapper.readValue(json, List.class); 26 | 27 | for (Iterator iterator = progLangs.iterator(); iterator.hasNext();) { 28 | String progLang = (String) iterator.next(); 29 | System.out.println(progLang); 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/net/javaguides/jackson/JacksonJsonToMap.java: -------------------------------------------------------------------------------- 1 | package net.javaguides.jackson; 2 | 3 | import java.io.IOException; 4 | import java.util.Map; 5 | import java.util.Map.Entry; 6 | 7 | import com.fasterxml.jackson.databind.ObjectMapper; 8 | import com.fasterxml.jackson.databind.SerializationFeature; 9 | 10 | public class JacksonJsonToMap { 11 | public static void main(String[] args) throws IOException { 12 | 13 | ObjectMapper mapper=new ObjectMapper(); 14 | mapper.enable(SerializationFeature.INDENT_OUTPUT); 15 | 16 | String json="{" + 17 | " \"THU\" : 5," + 18 | " \"TUE\" : 3," + 19 | " \"WED\" : 4," + 20 | " \"SAT\" : 7," + 21 | " \"FRI\" : 6," + 22 | " \"MON\" : 2," + 23 | " \"SUN\" : 1" + 24 | "}"; 25 | 26 | @SuppressWarnings("unchecked") 27 | Map days = mapper.readValue(json, Map.class); 28 | 29 | for (Entry day : days.entrySet()) { 30 | System.out.println(day.getKey() + "=" + day.getValue()); 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/net/javaguides/jackson/JacksonJsonToSet.java: -------------------------------------------------------------------------------- 1 | package net.javaguides.jackson; 2 | 3 | import java.io.IOException; 4 | import java.util.HashSet; 5 | import java.util.Iterator; 6 | import java.util.Set; 7 | 8 | import com.fasterxml.jackson.core.JsonParseException; 9 | import com.fasterxml.jackson.databind.JsonMappingException; 10 | import com.fasterxml.jackson.databind.ObjectMapper; 11 | import com.fasterxml.jackson.databind.SerializationFeature; 12 | 13 | public class JacksonJsonToSet { 14 | 15 | @SuppressWarnings("unchecked") 16 | public static void main(String[] args) throws JsonParseException, JsonMappingException, IOException { 17 | 18 | // Create ObjectMapper object. 19 | ObjectMapper mapper = new ObjectMapper(); 20 | mapper.enable(SerializationFeature.INDENT_OUTPUT); 21 | 22 | String json = "[ \"C\", \"C++\", \"Java\", \"Java EE\", \"Python\", \"Scala\", \"JavaScript\" ]"; 23 | 24 | Set progLangs = new HashSet<>(); 25 | progLangs = mapper.readValue(json, Set.class); 26 | 27 | for (Iterator iterator = progLangs.iterator(); iterator.hasNext();) { 28 | String progLang = (String) iterator.next(); 29 | System.out.println(progLang); 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/net/javaguides/jackson/JacksonListToJson.java: -------------------------------------------------------------------------------- 1 | package net.javaguides.jackson; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | import com.fasterxml.jackson.core.JsonProcessingException; 7 | import com.fasterxml.jackson.databind.ObjectMapper; 8 | import com.fasterxml.jackson.databind.SerializationFeature; 9 | 10 | /** 11 | * Using Jackson API for list serialization and deserialization 12 | * @author ramesh fadatare 13 | * 14 | */ 15 | public class JacksonListToJson { 16 | public static void main(String[] args) throws JsonProcessingException { 17 | 18 | // Create ObjectMapper object. 19 | ObjectMapper mapper = new ObjectMapper(); 20 | mapper.enable(SerializationFeature.INDENT_OUTPUT); 21 | 22 | List progLangs = new ArrayList<>(); 23 | progLangs.add("C"); 24 | progLangs.add("C++"); 25 | progLangs.add("Java"); 26 | progLangs.add("Java EE"); 27 | progLangs.add("Python"); 28 | progLangs.add("Scala"); 29 | progLangs.add("JavaScript"); 30 | // Serialize Object to JSON. 31 | String json = mapper.writeValueAsString(progLangs); 32 | 33 | // Print json 34 | System.out.println(json); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/main/java/net/javaguides/jackson/JacksonMapToJson.java: -------------------------------------------------------------------------------- 1 | package net.javaguides.jackson; 2 | 3 | import java.util.Calendar; 4 | import java.util.HashMap; 5 | import java.util.Map; 6 | 7 | import com.fasterxml.jackson.core.JsonProcessingException; 8 | import com.fasterxml.jackson.databind.ObjectMapper; 9 | import com.fasterxml.jackson.databind.SerializationFeature; 10 | 11 | public class JacksonMapToJson { 12 | 13 | public static void main(String[] args) throws JsonProcessingException { 14 | 15 | ObjectMapper mapper = new ObjectMapper(); 16 | mapper.enable(SerializationFeature.INDENT_OUTPUT); 17 | 18 | Map days = new HashMap<>(); 19 | days.put("MON", Calendar.MONDAY); 20 | days.put("TUE", Calendar.TUESDAY); 21 | days.put("WED", Calendar.WEDNESDAY); 22 | days.put("THU", Calendar.THURSDAY); 23 | days.put("FRI", Calendar.FRIDAY); 24 | days.put("SAT", Calendar.SATURDAY); 25 | days.put("SUN", Calendar.SUNDAY); 26 | 27 | String json = mapper.writeValueAsString(days); 28 | System.out.println(json); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/net/javaguides/jackson/JacksonSetToJson.java: -------------------------------------------------------------------------------- 1 | package net.javaguides.jackson; 2 | 3 | import java.util.HashSet; 4 | import java.util.Set; 5 | 6 | import com.fasterxml.jackson.core.JsonProcessingException; 7 | import com.fasterxml.jackson.databind.ObjectMapper; 8 | import com.fasterxml.jackson.databind.SerializationFeature; 9 | 10 | public class JacksonSetToJson { 11 | public static void main(String[] args) throws JsonProcessingException { 12 | // Create ObjectMapper object. 13 | ObjectMapper mapper = new ObjectMapper(); 14 | mapper.enable(SerializationFeature.INDENT_OUTPUT); 15 | 16 | Set progLangs = new HashSet<>(); 17 | progLangs.add("C"); 18 | progLangs.add("C++"); 19 | progLangs.add("Java"); 20 | progLangs.add("Java EE"); 21 | progLangs.add("Python"); 22 | progLangs.add("Scala"); 23 | progLangs.add("JavaScript"); 24 | // Serialize Object to JSON. 25 | String json = mapper.writeValueAsString(progLangs); 26 | 27 | // Print json 28 | System.out.println(json); 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/net/javaguides/jackson/ReadJsonUsingJsonParser.java: -------------------------------------------------------------------------------- 1 | package net.javaguides.jackson; 2 | 3 | import java.io.File; 4 | import java.io.IOException; 5 | 6 | import com.fasterxml.jackson.core.JsonFactory; 7 | import com.fasterxml.jackson.core.JsonParser; 8 | import com.fasterxml.jackson.core.JsonToken; 9 | 10 | /** 11 | * Read JSON from file using JsonParser 12 | * @author Ramesh Fadatare 13 | * 14 | */ 15 | public class ReadJsonUsingJsonParser { 16 | 17 | public static void main(String[] args) throws IOException { 18 | JsonFactory factory = new JsonFactory(); 19 | 20 | // Create Reader/InputStream/File 21 | File file = new File("post.json"); 22 | 23 | // Create JsonParser 24 | JsonParser parser = factory.createParser(file); 25 | 26 | JsonToken token = parser.nextToken(); // Read first object i.e. { 27 | 28 | // Read JSON object 29 | token = parser.nextToken(); 30 | if (token == JsonToken.FIELD_NAME && "id".equals(parser.getCurrentName())) { 31 | token = parser.nextToken(); 32 | if (token == JsonToken.VALUE_STRING) { 33 | System.out.println("ID : " + parser.getText()); 34 | } 35 | } 36 | token = parser.nextToken(); 37 | if (token == JsonToken.FIELD_NAME && "title".equals(parser.getCurrentName())) { 38 | token = parser.nextToken(); 39 | if (token == JsonToken.VALUE_STRING) { 40 | System.out.println("title : " + parser.getText()); 41 | } 42 | } 43 | token = parser.nextToken(); 44 | if (token == JsonToken.FIELD_NAME && "description".equals(parser.getCurrentName())) { 45 | token = parser.nextToken(); 46 | if (token == JsonToken.VALUE_STRING) { 47 | System.out.println("description : " + parser.getText()); 48 | } 49 | } 50 | 51 | token = parser.nextToken(); 52 | if (token == JsonToken.FIELD_NAME && "content".equals(parser.getCurrentName())) { 53 | token = parser.nextToken(); 54 | if (token == JsonToken.VALUE_STRING) { 55 | System.out.println("content : " + parser.getText()); 56 | } 57 | } 58 | 59 | // Reading JSON Array 60 | token = parser.nextToken(); 61 | if (token == JsonToken.FIELD_NAME && "tags".equals(parser.getCurrentName())) { 62 | 63 | System.out.println("Post tags are - "); 64 | token = parser.nextToken(); // // Read left bracket i.e. [ 65 | // Loop to print array elements until right bracket i.e ] 66 | while (token != JsonToken.END_ARRAY) { 67 | token = parser.nextToken(); 68 | 69 | if (token == JsonToken.VALUE_STRING) { 70 | System.out.println(parser.getText()); 71 | } 72 | } 73 | System.out.println(); 74 | } 75 | parser.close(); 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /src/main/java/net/javaguides/jackson/WriteJsonUsingJsonGenerator.java: -------------------------------------------------------------------------------- 1 | package net.javaguides.jackson; 2 | 3 | import java.io.File; 4 | import java.io.IOException; 5 | 6 | import com.fasterxml.jackson.core.JsonEncoding; 7 | import com.fasterxml.jackson.core.JsonFactory; 8 | import com.fasterxml.jackson.core.JsonGenerator; 9 | 10 | /** 11 | * Write JSON from file using JsonGenerator 12 | * @author Ramesh Fadatare 13 | * 14 | */ 15 | public class WriteJsonUsingJsonGenerator { 16 | 17 | public static void main(String[] args) throws IOException { 18 | JsonFactory factory = new JsonFactory(); 19 | 20 | // Create JsonGenerator 21 | JsonGenerator generator = factory.createGenerator(new File("post.json"), JsonEncoding.UTF8); 22 | 23 | // Creating JSON Oject 24 | generator.writeStartObject(); // Start with left brace i.e. { 25 | 26 | // Add string field 27 | generator.writeNumberField("id", 100); 28 | generator.writeStringField("title", "Jackson JSON API Guide"); 29 | generator.writeStringField("description", "Post about Jackson JSON API"); 30 | generator.writeStringField("content", "HTML content here"); 31 | 32 | // Creating JSON Array within JSON Object 33 | generator.writeFieldName("tags"); 34 | 35 | // Array of Tag objects 36 | generator.writeStartArray(); // Start with left bracket i.e. [ 37 | 38 | // first tag object 39 | generator.writeStartObject(); // Start with left brace i.e. { 40 | generator.writeNumberField("id", 1); 41 | generator.writeStringField("name", "JSON"); 42 | generator.writeEndObject(); // End with right brace i.e } 43 | 44 | // second tag object 45 | generator.writeStartObject(); // Start with left brace i.e. { 46 | generator.writeNumberField("id", 2); 47 | generator.writeStringField("name", "Java"); 48 | generator.writeEndObject(); // End with right brace i.e } 49 | 50 | // second tag object 51 | generator.writeStartObject(); // Start with left brace i.e. { 52 | generator.writeNumberField("id", 3); 53 | generator.writeStringField("name", "Jackson"); 54 | generator.writeEndObject(); // End with right brace i.e } 55 | 56 | generator.writeEndArray(); // End with right bracket i.e. ] 57 | 58 | generator.writeEndObject(); // End with right brace i.e } 59 | 60 | // Close JSON generator 61 | generator.close(); 62 | // Print JSON Object 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /src/main/java/net/javaguides/jackson/annotations/Demo.java: -------------------------------------------------------------------------------- 1 | package net.javaguides.jackson.annotations; 2 | 3 | import java.io.IOException; 4 | 5 | import com.fasterxml.jackson.databind.ObjectMapper; 6 | import com.fasterxml.jackson.databind.SerializationFeature; 7 | 8 | public class Demo { 9 | public static void main(String[] args) throws IOException { 10 | 11 | // Create ObjectMapper object. 12 | ObjectMapper mapper = new ObjectMapper(); 13 | mapper.enable(SerializationFeature.INDENT_OUTPUT); 14 | User bean = new User(1, "Ramesh", "Fadatare", "Ramesh Fadatare"); 15 | String result = mapper.writeValueAsString(bean); 16 | 17 | System.out.println(result); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/net/javaguides/jackson/annotations/Employee.java: -------------------------------------------------------------------------------- 1 | package net.javaguides.jackson.annotations; 2 | 3 | public class Employee { 4 | 5 | private int id; 6 | 7 | private String firstName; 8 | 9 | private String lastName; 10 | 11 | public Employee(int id, String firstName, String lastName) { 12 | super(); 13 | this.id = id; 14 | this.firstName = firstName; 15 | this.lastName = lastName; 16 | } 17 | public int getId() { 18 | return id; 19 | } 20 | public void setId(int id) { 21 | this.id = id; 22 | } 23 | public String getFirstName() { 24 | return firstName; 25 | } 26 | public void setFirstName(String firstName) { 27 | this.firstName = firstName; 28 | } 29 | public String getLastName() { 30 | return lastName; 31 | } 32 | public void setLastName(String lastName) { 33 | this.lastName = lastName; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/main/java/net/javaguides/jackson/annotations/JsonIncludeAnnotationTest.java: -------------------------------------------------------------------------------- 1 | package net.javaguides.jackson.annotations; 2 | 3 | import com.fasterxml.jackson.annotation.JsonInclude.Include; 4 | import com.fasterxml.jackson.core.JsonProcessingException; 5 | import com.fasterxml.jackson.databind.ObjectMapper; 6 | import com.fasterxml.jackson.databind.SerializationFeature; 7 | 8 | public class JsonIncludeAnnotationTest { 9 | public static void main(String[] args) throws JsonProcessingException { 10 | 11 | // Create ObjectMapper object. 12 | ObjectMapper mapper = new ObjectMapper(); 13 | mapper.setSerializationInclusion(Include.NON_NULL); 14 | mapper.setSerializationInclusion(Include.NON_EMPTY); 15 | 16 | mapper.enable(SerializationFeature.INDENT_OUTPUT); 17 | 18 | Employee employee = new Employee(10, null, ""); 19 | String result = mapper.writeValueAsString(employee); 20 | 21 | System.out.println(result); 22 | 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/net/javaguides/jackson/annotations/User.java: -------------------------------------------------------------------------------- 1 | package net.javaguides.jackson.annotations; 2 | 3 | import com.fasterxml.jackson.annotation.JsonPropertyOrder; 4 | 5 | @JsonPropertyOrder(alphabetic=true) 6 | public class User { 7 | 8 | public int id; 9 | 10 | private String firstName; 11 | 12 | private String lastName; 13 | 14 | private String fullName; 15 | 16 | public User(int id, String firstName, String lastName, String fullName) { 17 | super(); 18 | this.id = id; 19 | this.firstName = firstName; 20 | this.lastName = lastName; 21 | this.fullName = fullName; 22 | } 23 | 24 | public int getId() { 25 | return id; 26 | } 27 | public void setId(int id) { 28 | this.id = id; 29 | } 30 | public String getFirstName() { 31 | return firstName; 32 | } 33 | public void setFirstName(String firstName) { 34 | this.firstName = firstName; 35 | } 36 | public String getLastName() { 37 | return lastName; 38 | } 39 | public void setLastName(String lastName) { 40 | this.lastName = lastName; 41 | } 42 | public String getFullName() { 43 | return fullName; 44 | } 45 | public void setFullName(String fullName) { 46 | this.fullName = fullName; 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/main/java/net/javaguides/jackson/ignore/CustomerDTO.java: -------------------------------------------------------------------------------- 1 | package net.javaguides.jackson.ignore; 2 | 3 | import com.fasterxml.jackson.annotation.JsonIgnore; 4 | 5 | public class CustomerDTO { 6 | 7 | @JsonIgnore 8 | private final String id; 9 | 10 | @JsonIgnore 11 | private final String firstName; 12 | private final String lastName; 13 | 14 | public CustomerDTO(String id, String firstName, String lastName) { 15 | this.id = id; 16 | this.firstName = firstName; 17 | this.lastName = lastName; 18 | } 19 | 20 | public String getId() { 21 | return id; 22 | } 23 | 24 | public String getFirstName() { 25 | return firstName; 26 | } 27 | 28 | public String getLastName() { 29 | return lastName; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/net/javaguides/jackson/ignore/IgnoreFieldTest.java: -------------------------------------------------------------------------------- 1 | package net.javaguides.jackson.ignore; 2 | 3 | import com.fasterxml.jackson.core.JsonProcessingException; 4 | import com.fasterxml.jackson.databind.ObjectMapper; 5 | 6 | public class IgnoreFieldTest { 7 | public static void main(String[] args) throws JsonProcessingException { 8 | ObjectMapper mapper = new ObjectMapper(); 9 | 10 | CustomerDTO dtoObject = new CustomerDTO("CUST100", "Tony", "Stark"); 11 | 12 | String dtoAsString = mapper.writeValueAsString(dtoObject); 13 | 14 | System.out.println(dtoAsString); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/net/javaguides/jackson/ignore/JsonIgnoreTypeTest.java: -------------------------------------------------------------------------------- 1 | package net.javaguides.jackson.ignore; 2 | 3 | import com.fasterxml.jackson.core.JsonProcessingException; 4 | import com.fasterxml.jackson.databind.ObjectMapper; 5 | 6 | public class JsonIgnoreTypeTest { 7 | 8 | public static void main(String[] args) throws JsonProcessingException { 9 | UserDTO.Name name = new UserDTO.Name("John", "Doe"); 10 | UserDTO user = new UserDTO(1, name); 11 | 12 | String result = new ObjectMapper() 13 | .writeValueAsString(user); 14 | 15 | System.out.println(result); 16 | } 17 | } 18 | 19 | -------------------------------------------------------------------------------- /src/main/java/net/javaguides/jackson/ignore/UserDTO.java: -------------------------------------------------------------------------------- 1 | package net.javaguides.jackson.ignore; 2 | 3 | import com.fasterxml.jackson.annotation.JsonIgnoreType; 4 | 5 | public class UserDTO { 6 | 7 | public int id; 8 | public Name name; 9 | 10 | 11 | public UserDTO(int id, Name name) { 12 | super(); 13 | this.id = id; 14 | this.name = name; 15 | } 16 | 17 | @JsonIgnoreType 18 | public static class Name { 19 | public String firstName; 20 | public String lastName; 21 | public Name(String firstName, String lastName) { 22 | super(); 23 | this.firstName = firstName; 24 | this.lastName = lastName; 25 | } 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/net/javaguides/jackson/jsontopojo/JacksonJsonToPojo.java: -------------------------------------------------------------------------------- 1 | package net.javaguides.jackson.jsontopojo; 2 | 3 | import java.io.FileInputStream; 4 | import java.io.IOException; 5 | import java.io.InputStream; 6 | import java.util.Iterator; 7 | 8 | import com.fasterxml.jackson.core.JsonParseException; 9 | import com.fasterxml.jackson.databind.JsonMappingException; 10 | import com.fasterxml.jackson.databind.ObjectMapper; 11 | 12 | import net.javaguides.jackson.pojotojson.Post; 13 | import net.javaguides.jackson.pojotojson.Tag; 14 | 15 | /** 16 | * Convert JSON to Java object using Jackson 17 | * @author Ramesh Fadatare 18 | * 19 | */ 20 | public class JacksonJsonToPojo { 21 | 22 | public static void main(String[] args) throws JsonParseException, JsonMappingException, IOException { 23 | ObjectMapper mapper = new ObjectMapper(); 24 | 25 | // Read JSON file and convert to java object 26 | InputStream fileInputStream = new FileInputStream("post.json"); 27 | Post post = mapper.readValue(fileInputStream, Post.class); 28 | fileInputStream.close(); 29 | 30 | // print post object 31 | System.out.println("Printing post details"); 32 | System.out.println(post.getId()); 33 | System.out.println(post.getTitle()); 34 | System.out.println(post.getDescription()); 35 | System.out.println(post.getContent()); 36 | System.out.println(post.getLastUpdatedAt()); 37 | System.out.println(post.getPostedAt()); 38 | 39 | // print tags of this post 40 | System.out.println("Printing tag details of post :: " + post.getTitle()); 41 | for (Iterator iterator = post.getTags().iterator(); iterator.hasNext();) { 42 | Tag tag = (Tag) iterator.next(); 43 | 44 | System.out.println(tag.getId()); 45 | System.out.println(tag.getName()); 46 | 47 | } 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /src/main/java/net/javaguides/jackson/pojotojson/JacksonPojoToJson.java: -------------------------------------------------------------------------------- 1 | package net.javaguides.jackson.pojotojson; 2 | 3 | import java.io.FileOutputStream; 4 | import java.io.IOException; 5 | import java.util.Date; 6 | import java.util.HashSet; 7 | import java.util.Set; 8 | 9 | import com.fasterxml.jackson.databind.ObjectMapper; 10 | import com.fasterxml.jackson.databind.SerializationFeature; 11 | 12 | /** 13 | * Convert Java to Json using Jackson API 14 | * @author Ramesh Fadatare 15 | * 16 | */ 17 | public class JacksonPojoToJson { 18 | public static void main(String[] args) throws IOException { 19 | // Create ObjectMapper 20 | ObjectMapper mapper = new ObjectMapper(); 21 | mapper.enable(SerializationFeature.INDENT_OUTPUT); 22 | 23 | // create a post 24 | Post post = new Post(); 25 | post.setTitle("Jackson JSON API Guide"); 26 | post.setId(100L); 27 | post.setDescription("Post about Jackson JSON API"); 28 | post.setContent("HTML content here"); 29 | post.setLastUpdatedAt(new Date()); 30 | post.setPostedAt(new Date()); 31 | 32 | // create some predefined tags 33 | Set tags = new HashSet<>(); 34 | Tag java = new Tag(1L, "Java"); 35 | Tag jackson = new Tag(2L, "Jackson"); 36 | Tag json = new Tag(3L, "JSON"); 37 | tags.add(java); 38 | tags.add(jackson); 39 | tags.add(json); 40 | 41 | // set tags to post 42 | post.setTags(tags); 43 | 44 | // Convert object to JSON string 45 | String postJson = mapper.writeValueAsString(post); 46 | System.out.println(postJson); 47 | 48 | // Save JSON string to file 49 | FileOutputStream fileOutputStream = new FileOutputStream("post.json"); 50 | mapper.writeValue(fileOutputStream, post); 51 | fileOutputStream.close(); 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /src/main/java/net/javaguides/jackson/pojotojson/Post.java: -------------------------------------------------------------------------------- 1 | package net.javaguides.jackson.pojotojson; 2 | 3 | import java.util.Date; 4 | import java.util.HashSet; 5 | import java.util.Set; 6 | 7 | public class Post { 8 | private Long id; 9 | private String title; 10 | private String description; 11 | private String content; 12 | private Date postedAt = new Date(); 13 | private Date lastUpdatedAt = new Date(); 14 | private Set tags = new HashSet<>(); 15 | 16 | public Post() { 17 | 18 | } 19 | 20 | public Post(String title, String description, String content) { 21 | this.title = title; 22 | this.description = description; 23 | this.content = content; 24 | } 25 | 26 | public Long getId() { 27 | return id; 28 | } 29 | 30 | public void setId(Long id) { 31 | this.id = id; 32 | } 33 | 34 | public String getTitle() { 35 | return title; 36 | } 37 | 38 | public void setTitle(String title) { 39 | this.title = title; 40 | } 41 | 42 | public String getDescription() { 43 | return description; 44 | } 45 | 46 | public void setDescription(String description) { 47 | this.description = description; 48 | } 49 | 50 | public String getContent() { 51 | return content; 52 | } 53 | 54 | public void setContent(String content) { 55 | this.content = content; 56 | } 57 | 58 | public Date getPostedAt() { 59 | return postedAt; 60 | } 61 | 62 | public void setPostedAt(Date postedAt) { 63 | this.postedAt = postedAt; 64 | } 65 | 66 | public Date getLastUpdatedAt() { 67 | return lastUpdatedAt; 68 | } 69 | 70 | public void setLastUpdatedAt(Date lastUpdatedAt) { 71 | this.lastUpdatedAt = lastUpdatedAt; 72 | } 73 | 74 | public Set getTags() { 75 | return tags; 76 | } 77 | 78 | public void setTags(Set tags) { 79 | this.tags = tags; 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /src/main/java/net/javaguides/jackson/pojotojson/Tag.java: -------------------------------------------------------------------------------- 1 | package net.javaguides.jackson.pojotojson; 2 | 3 | public class Tag { 4 | private Long id; 5 | private String name; 6 | 7 | public Tag() { 8 | 9 | } 10 | 11 | public Tag(Long id, String name) { 12 | super(); 13 | this.id = id; 14 | this.name = name; 15 | } 16 | 17 | 18 | public Tag(String name) { 19 | this.name = name; 20 | } 21 | 22 | public Long getId() { 23 | return id; 24 | } 25 | 26 | public void setId(Long id) { 27 | this.id = id; 28 | } 29 | 30 | public String getName() { 31 | return name; 32 | } 33 | 34 | public void setName(String name) { 35 | this.name = name; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /target/Jackson-json-tutorial-0.0.1-SNAPSHOT.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RameshMF/jackson-json-tutorial/fbfdbde44e28039e71fd82256fd44dd747ffb8b8/target/Jackson-json-tutorial-0.0.1-SNAPSHOT.jar -------------------------------------------------------------------------------- /target/classes/net/javaguides/jackson/JacksonJsonToList.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RameshMF/jackson-json-tutorial/fbfdbde44e28039e71fd82256fd44dd747ffb8b8/target/classes/net/javaguides/jackson/JacksonJsonToList.class -------------------------------------------------------------------------------- /target/classes/net/javaguides/jackson/JacksonJsonToMap.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RameshMF/jackson-json-tutorial/fbfdbde44e28039e71fd82256fd44dd747ffb8b8/target/classes/net/javaguides/jackson/JacksonJsonToMap.class -------------------------------------------------------------------------------- /target/classes/net/javaguides/jackson/JacksonJsonToSet.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RameshMF/jackson-json-tutorial/fbfdbde44e28039e71fd82256fd44dd747ffb8b8/target/classes/net/javaguides/jackson/JacksonJsonToSet.class -------------------------------------------------------------------------------- /target/classes/net/javaguides/jackson/JacksonListToJson.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RameshMF/jackson-json-tutorial/fbfdbde44e28039e71fd82256fd44dd747ffb8b8/target/classes/net/javaguides/jackson/JacksonListToJson.class -------------------------------------------------------------------------------- /target/classes/net/javaguides/jackson/JacksonMapToJson.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RameshMF/jackson-json-tutorial/fbfdbde44e28039e71fd82256fd44dd747ffb8b8/target/classes/net/javaguides/jackson/JacksonMapToJson.class -------------------------------------------------------------------------------- /target/classes/net/javaguides/jackson/JacksonSetToJson.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RameshMF/jackson-json-tutorial/fbfdbde44e28039e71fd82256fd44dd747ffb8b8/target/classes/net/javaguides/jackson/JacksonSetToJson.class -------------------------------------------------------------------------------- /target/classes/net/javaguides/jackson/ReadJsonUsingJsonParser.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RameshMF/jackson-json-tutorial/fbfdbde44e28039e71fd82256fd44dd747ffb8b8/target/classes/net/javaguides/jackson/ReadJsonUsingJsonParser.class -------------------------------------------------------------------------------- /target/classes/net/javaguides/jackson/WriteJsonUsingJsonGenerator.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RameshMF/jackson-json-tutorial/fbfdbde44e28039e71fd82256fd44dd747ffb8b8/target/classes/net/javaguides/jackson/WriteJsonUsingJsonGenerator.class -------------------------------------------------------------------------------- /target/classes/net/javaguides/jackson/annotations/Demo.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RameshMF/jackson-json-tutorial/fbfdbde44e28039e71fd82256fd44dd747ffb8b8/target/classes/net/javaguides/jackson/annotations/Demo.class -------------------------------------------------------------------------------- /target/classes/net/javaguides/jackson/annotations/Employee.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RameshMF/jackson-json-tutorial/fbfdbde44e28039e71fd82256fd44dd747ffb8b8/target/classes/net/javaguides/jackson/annotations/Employee.class -------------------------------------------------------------------------------- /target/classes/net/javaguides/jackson/annotations/JsonIncludeAnnotationTest.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RameshMF/jackson-json-tutorial/fbfdbde44e28039e71fd82256fd44dd747ffb8b8/target/classes/net/javaguides/jackson/annotations/JsonIncludeAnnotationTest.class -------------------------------------------------------------------------------- /target/classes/net/javaguides/jackson/annotations/User.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RameshMF/jackson-json-tutorial/fbfdbde44e28039e71fd82256fd44dd747ffb8b8/target/classes/net/javaguides/jackson/annotations/User.class -------------------------------------------------------------------------------- /target/classes/net/javaguides/jackson/ignore/CustomerDTO.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RameshMF/jackson-json-tutorial/fbfdbde44e28039e71fd82256fd44dd747ffb8b8/target/classes/net/javaguides/jackson/ignore/CustomerDTO.class -------------------------------------------------------------------------------- /target/classes/net/javaguides/jackson/ignore/IgnoreFieldTest.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RameshMF/jackson-json-tutorial/fbfdbde44e28039e71fd82256fd44dd747ffb8b8/target/classes/net/javaguides/jackson/ignore/IgnoreFieldTest.class -------------------------------------------------------------------------------- /target/classes/net/javaguides/jackson/ignore/JsonIgnoreTypeTest.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RameshMF/jackson-json-tutorial/fbfdbde44e28039e71fd82256fd44dd747ffb8b8/target/classes/net/javaguides/jackson/ignore/JsonIgnoreTypeTest.class -------------------------------------------------------------------------------- /target/classes/net/javaguides/jackson/ignore/UserDTO$Name.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RameshMF/jackson-json-tutorial/fbfdbde44e28039e71fd82256fd44dd747ffb8b8/target/classes/net/javaguides/jackson/ignore/UserDTO$Name.class -------------------------------------------------------------------------------- /target/classes/net/javaguides/jackson/ignore/UserDTO.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RameshMF/jackson-json-tutorial/fbfdbde44e28039e71fd82256fd44dd747ffb8b8/target/classes/net/javaguides/jackson/ignore/UserDTO.class -------------------------------------------------------------------------------- /target/classes/net/javaguides/jackson/jsontopojo/JacksonJsonToPojo.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RameshMF/jackson-json-tutorial/fbfdbde44e28039e71fd82256fd44dd747ffb8b8/target/classes/net/javaguides/jackson/jsontopojo/JacksonJsonToPojo.class -------------------------------------------------------------------------------- /target/classes/net/javaguides/jackson/pojotojson/JacksonPojoToJson.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RameshMF/jackson-json-tutorial/fbfdbde44e28039e71fd82256fd44dd747ffb8b8/target/classes/net/javaguides/jackson/pojotojson/JacksonPojoToJson.class -------------------------------------------------------------------------------- /target/classes/net/javaguides/jackson/pojotojson/Post.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RameshMF/jackson-json-tutorial/fbfdbde44e28039e71fd82256fd44dd747ffb8b8/target/classes/net/javaguides/jackson/pojotojson/Post.class -------------------------------------------------------------------------------- /target/classes/net/javaguides/jackson/pojotojson/Tag.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RameshMF/jackson-json-tutorial/fbfdbde44e28039e71fd82256fd44dd747ffb8b8/target/classes/net/javaguides/jackson/pojotojson/Tag.class -------------------------------------------------------------------------------- /target/maven-archiver/pom.properties: -------------------------------------------------------------------------------- 1 | #Generated by Maven 2 | #Tue Apr 23 22:36:10 IST 2019 3 | version=0.0.1-SNAPSHOT 4 | groupId=javaguides 5 | artifactId=Jackson-json-tutorial 6 | -------------------------------------------------------------------------------- /target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst: -------------------------------------------------------------------------------- 1 | net\javaguides\jackson\JacksonListToJson.class 2 | net\javaguides\jackson\JacksonMapToJson.class 3 | net\javaguides\jackson\pojotojson\Tag.class 4 | net\javaguides\jackson\ignore\IgnoreFieldTest.class 5 | net\javaguides\jackson\JacksonSetToJson.class 6 | net\javaguides\jackson\pojotojson\Post.class 7 | net\javaguides\jackson\annotations\JsonIncludeAnnotationTest.class 8 | net\javaguides\jackson\annotations\User.class 9 | net\javaguides\jackson\ignore\CustomerDTO.class 10 | net\javaguides\jackson\ignore\UserDTO.class 11 | net\javaguides\jackson\annotations\Employee.class 12 | net\javaguides\jackson\ignore\JsonIgnoreTypeTest.class 13 | net\javaguides\jackson\JacksonJsonToList.class 14 | net\javaguides\jackson\pojotojson\JacksonPojoToJson.class 15 | net\javaguides\jackson\JacksonJsonToSet.class 16 | net\javaguides\jackson\annotations\JsonPropertyAnnotationTest.class 17 | net\javaguides\jackson\JacksonJsonToMap.class 18 | net\javaguides\jackson\ignore\UserDTO$Name.class 19 | net\javaguides\jackson\jsontopojo\JacksonJsonToPojo.class 20 | -------------------------------------------------------------------------------- /target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst: -------------------------------------------------------------------------------- 1 | C:\Ramesh_Study\spring-boot\spring-boot-articles\Jackson-json-tutorial\src\main\java\net\javaguides\jackson\annotations\User.java 2 | C:\Ramesh_Study\spring-boot\spring-boot-articles\Jackson-json-tutorial\src\main\java\net\javaguides\jackson\annotations\JsonPropertyAnnotationTest.java 3 | C:\Ramesh_Study\spring-boot\spring-boot-articles\Jackson-json-tutorial\src\main\java\net\javaguides\jackson\JacksonMapToJson.java 4 | C:\Ramesh_Study\spring-boot\spring-boot-articles\Jackson-json-tutorial\src\main\java\net\javaguides\jackson\annotations\Employee.java 5 | C:\Ramesh_Study\spring-boot\spring-boot-articles\Jackson-json-tutorial\src\main\java\net\javaguides\jackson\JacksonJsonToList.java 6 | C:\Ramesh_Study\spring-boot\spring-boot-articles\Jackson-json-tutorial\src\main\java\net\javaguides\jackson\pojotojson\JacksonPojoToJson.java 7 | C:\Ramesh_Study\spring-boot\spring-boot-articles\Jackson-json-tutorial\src\main\java\net\javaguides\jackson\JacksonJsonToMap.java 8 | C:\Ramesh_Study\spring-boot\spring-boot-articles\Jackson-json-tutorial\src\main\java\net\javaguides\jackson\pojotojson\Tag.java 9 | C:\Ramesh_Study\spring-boot\spring-boot-articles\Jackson-json-tutorial\src\main\java\net\javaguides\jackson\annotations\JsonIncludeAnnotationTest.java 10 | C:\Ramesh_Study\spring-boot\spring-boot-articles\Jackson-json-tutorial\src\main\java\net\javaguides\jackson\jsontopojo\JacksonJsonToPojo.java 11 | C:\Ramesh_Study\spring-boot\spring-boot-articles\Jackson-json-tutorial\src\main\java\net\javaguides\jackson\ignore\UserDTO.java 12 | C:\Ramesh_Study\spring-boot\spring-boot-articles\Jackson-json-tutorial\src\main\java\net\javaguides\jackson\pojotojson\Post.java 13 | C:\Ramesh_Study\spring-boot\spring-boot-articles\Jackson-json-tutorial\src\main\java\net\javaguides\jackson\JacksonSetToJson.java 14 | C:\Ramesh_Study\spring-boot\spring-boot-articles\Jackson-json-tutorial\src\main\java\net\javaguides\jackson\ignore\IgnoreFieldTest.java 15 | C:\Ramesh_Study\spring-boot\spring-boot-articles\Jackson-json-tutorial\src\main\java\net\javaguides\jackson\ignore\CustomerDTO.java 16 | C:\Ramesh_Study\spring-boot\spring-boot-articles\Jackson-json-tutorial\src\main\java\net\javaguides\jackson\ignore\JsonIgnoreTypeTest.java 17 | C:\Ramesh_Study\spring-boot\spring-boot-articles\Jackson-json-tutorial\src\main\java\net\javaguides\jackson\JacksonListToJson.java 18 | C:\Ramesh_Study\spring-boot\spring-boot-articles\Jackson-json-tutorial\src\main\java\net\javaguides\jackson\JacksonJsonToSet.java 19 | --------------------------------------------------------------------------------