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+ }
0 commit comments