Skip to content

Commit ae81e4a

Browse files
author
syshex
committed
code
1 parent 04e8a15 commit ae81e4a

File tree

7 files changed

+298
-0
lines changed

7 files changed

+298
-0
lines changed

pom.xml

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.coderfromscratch</groupId>
8+
<artifactId>json-parsing-tutorial</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
<build>
11+
<plugins>
12+
<plugin>
13+
<groupId>org.apache.maven.plugins</groupId>
14+
<artifactId>maven-compiler-plugin</artifactId>
15+
<configuration>
16+
<source>8</source>
17+
<target>8</target>
18+
</configuration>
19+
</plugin>
20+
</plugins>
21+
</build>
22+
23+
<dependencies>
24+
<!-- JACKSON -->
25+
<dependency>
26+
<groupId>com.fasterxml.jackson.core</groupId>
27+
<artifactId>jackson-core</artifactId>
28+
<version>2.9.9</version>
29+
</dependency>
30+
<dependency>
31+
<groupId>com.fasterxml.jackson.core</groupId>
32+
<artifactId>jackson-databind</artifactId>
33+
<version>2.9.9.3</version>
34+
</dependency>
35+
<dependency>
36+
<groupId>com.fasterxml.jackson.datatype</groupId>
37+
<artifactId>jackson-datatype-jsr310</artifactId>
38+
<version>2.9.9</version>
39+
</dependency>
40+
41+
<dependency>
42+
<groupId>org.junit.jupiter</groupId>
43+
<artifactId>junit-jupiter</artifactId>
44+
<version>RELEASE</version>
45+
<scope>test</scope>
46+
</dependency>
47+
</dependencies>
48+
49+
</project>
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.coderfromscratch.jsonparsing;
2+
3+
import com.fasterxml.jackson.core.JsonProcessingException;
4+
import com.fasterxml.jackson.databind.*;
5+
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
6+
7+
import java.io.IOException;
8+
9+
public class Json {
10+
11+
private static ObjectMapper objectMapper = getDefaultObjectMapper();
12+
13+
private static ObjectMapper getDefaultObjectMapper() {
14+
ObjectMapper defaultObjectMapper = new ObjectMapper();
15+
defaultObjectMapper.registerModule(new JavaTimeModule());
16+
defaultObjectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
17+
return defaultObjectMapper;
18+
}
19+
20+
public static JsonNode parse(String src) throws IOException {
21+
return objectMapper.readTree(src);
22+
}
23+
24+
public static <A> A fromJson(JsonNode node , Class<A> clazz) throws JsonProcessingException {
25+
return objectMapper.treeToValue(node, clazz);
26+
}
27+
28+
public static JsonNode toJson(Object a) {
29+
return objectMapper.valueToTree(a);
30+
}
31+
32+
public static String stingify(JsonNode node) throws JsonProcessingException {
33+
return generateString(node, false);
34+
}
35+
36+
public static String prettyPrint(JsonNode node) throws JsonProcessingException {
37+
return generateString(node, true);
38+
}
39+
40+
private static String generateString(JsonNode node, boolean pretty) throws JsonProcessingException {
41+
ObjectWriter objectWriter = objectMapper.writer();
42+
if ( pretty )
43+
objectWriter = objectWriter.with(SerializationFeature.INDENT_OUTPUT);
44+
return objectWriter.writeValueAsString(node);
45+
}
46+
}
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
package com.coderfromscratch.jsonparsing;
2+
3+
import com.coderfromscratch.jsonparsing.pojo.AuthorPOJO;
4+
import com.coderfromscratch.jsonparsing.pojo.BookPOJO;
5+
import com.coderfromscratch.jsonparsing.pojo.DayPOJO;
6+
import com.coderfromscratch.jsonparsing.pojo.SimpleTestCaseJsonPOJO;
7+
import com.fasterxml.jackson.core.JsonProcessingException;
8+
import com.fasterxml.jackson.databind.JsonNode;
9+
import org.junit.jupiter.api.Test;
10+
11+
import java.io.IOException;
12+
13+
import static org.junit.jupiter.api.Assertions.*;
14+
15+
class JsonTest {
16+
17+
private String simpleTestCaseJsonSource = "{ \n" +
18+
" \"title\": \"Coder From Scratch\", \n" +
19+
" \"author\": \"Rui\"\n" +
20+
"}";
21+
22+
private String dayScenario1 = "{\n" +
23+
" \"date\": \"2019-12-25\",\n" +
24+
" \"name\": \"Christmas Day\"\n" +
25+
"}";
26+
27+
private String authorBookScenario= "{\n" +
28+
" \"authorName\": \"Rui\",\n" +
29+
" \"books\": [\n" +
30+
" {\n" +
31+
" \"title\": \"title1\",\n" +
32+
" \"inPrint\": true,\n" +
33+
" \"publishDate\": \"2019-12-25\"\n" +
34+
" },\n" +
35+
" {\n" +
36+
" \"title\": \"title2\",\n" +
37+
" \"inPrint\": false,\n" +
38+
" \"publishDate\": \"2019-01-01\"\n" +
39+
" }\n" +
40+
" ]\n" +
41+
"}";
42+
43+
@Test
44+
void parse() throws IOException {
45+
46+
JsonNode node = Json.parse(simpleTestCaseJsonSource);
47+
assertEquals(node.get("title").asText() , "Coder From Scratch");
48+
49+
}
50+
51+
@Test
52+
void fromJson() throws IOException {
53+
54+
JsonNode node = Json.parse(simpleTestCaseJsonSource);
55+
SimpleTestCaseJsonPOJO pojo = Json.fromJson(node , SimpleTestCaseJsonPOJO.class);
56+
57+
assertEquals(pojo.getTitle() , "Coder From Scratch");
58+
59+
}
60+
61+
@Test
62+
void toJson() {
63+
SimpleTestCaseJsonPOJO pojo = new SimpleTestCaseJsonPOJO();
64+
pojo.setTitle("Testing 123");
65+
66+
JsonNode node = Json.toJson(pojo);
67+
68+
assertEquals(node.get("title").asText() , "Testing 123");
69+
}
70+
71+
@Test
72+
void stingify() throws JsonProcessingException {
73+
SimpleTestCaseJsonPOJO pojo = new SimpleTestCaseJsonPOJO();
74+
pojo.setTitle("Testing 123");
75+
76+
JsonNode node = Json.toJson(pojo);
77+
78+
System.out.println(Json.stingify(node));
79+
System.out.println(Json.prettyPrint(node));
80+
81+
}
82+
83+
@Test
84+
void dayTestScenario1() throws IOException {
85+
86+
JsonNode node = Json.parse(dayScenario1);
87+
DayPOJO pojo = Json.fromJson(node , DayPOJO.class);
88+
89+
assertEquals("2019-12-25", pojo.getDate().toString());
90+
}
91+
92+
@Test
93+
void authorBookScenario1() throws IOException {
94+
95+
JsonNode node = Json.parse(authorBookScenario);
96+
AuthorPOJO pojo = Json.fromJson(node , AuthorPOJO.class);
97+
98+
System.out.println("Author : " + pojo.getAuthorName());
99+
for (BookPOJO bP : pojo.getBooks()) {
100+
System.out.println("Book : " + bP.getTitle());
101+
System.out.println("Is In Print? " + bP.isInPrint());
102+
System.out.println("Date : " + bP.getPublishDate());
103+
}
104+
}
105+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.coderfromscratch.jsonparsing.pojo;
2+
3+
import java.util.List;
4+
5+
public class AuthorPOJO {
6+
7+
private String authorName;
8+
private List<BookPOJO> books;
9+
10+
public String getAuthorName() {
11+
return authorName;
12+
}
13+
14+
public void setAuthorName(String authorName) {
15+
this.authorName = authorName;
16+
}
17+
18+
public List<BookPOJO> getBooks() {
19+
return books;
20+
}
21+
22+
public void setBooks(List<BookPOJO> books) {
23+
this.books = books;
24+
}
25+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.coderfromscratch.jsonparsing.pojo;
2+
3+
import java.time.LocalDate;
4+
5+
public class BookPOJO {
6+
7+
private String title;
8+
private boolean inPrint;
9+
private LocalDate publishDate;
10+
11+
public String getTitle() {
12+
return title;
13+
}
14+
15+
public void setTitle(String title) {
16+
this.title = title;
17+
}
18+
19+
public boolean isInPrint() {
20+
return inPrint;
21+
}
22+
23+
public void setInPrint(boolean inPrint) {
24+
this.inPrint = inPrint;
25+
}
26+
27+
public LocalDate getPublishDate() {
28+
return publishDate;
29+
}
30+
31+
public void setPublishDate(LocalDate publishDate) {
32+
this.publishDate = publishDate;
33+
}
34+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.coderfromscratch.jsonparsing.pojo;
2+
3+
import java.time.*;
4+
5+
public class DayPOJO {
6+
7+
private String name;
8+
private LocalDate date;
9+
10+
public String getName() {
11+
return name;
12+
}
13+
14+
public void setName(String name) {
15+
this.name = name;
16+
}
17+
18+
public LocalDate getDate() {
19+
return date;
20+
}
21+
22+
public void setDate(LocalDate date) {
23+
this.date = date;
24+
}
25+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.coderfromscratch.jsonparsing.pojo;
2+
3+
public class SimpleTestCaseJsonPOJO {
4+
5+
private String title;
6+
7+
public String getTitle() {
8+
return title;
9+
}
10+
11+
public void setTitle(String title) {
12+
this.title = title;
13+
}
14+
}

0 commit comments

Comments
 (0)