Skip to content

Commit 4d69891

Browse files
committed
Update examples
1 parent 1715833 commit 4d69891

File tree

4 files changed

+240
-82
lines changed

4 files changed

+240
-82
lines changed

Java-JsonPojo-Example/README.md

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,35 @@
11
# Java JsonPojo Examples
22

33

4-
## 1. Import source code into Eclipse
4+
### 1. Import source code into Eclipse
55

66
Menu **File –> Import –> Maven –> Existing Maven Projects**
77

88
Browse to your source code location
99

1010
Click **Finish** button to finish the importing
1111

12-
## 2. Run the example
12+
### 2. Run the example
1313

14-
Open the **JsonPojoUtils.java**
14+
Open the GsonTest.java and JacksonTest.java
1515

16-
**Right click -> Run As -> Java Application** or use the shortcut: **Alt+Shift+x, j** to start the main method
17-
Source code are described in **[Convert Java Objects To JSON And Vice Versa](http://howtoprogram.xyz/2016/07/01/convert-java-objects-json-vice-versa/)**
16+
**Right click -> Run As -> Java Application** or use the shortcut: **Alt+Shift+x, t** to start the JUnit test methods.
17+
Source code are described in [Convert Java Objects To JSON And Vice Versa With Jackson and Gson](http://howtoprogram.xyz/2016/07/01/convert-java-objects-json-vice-versa/)
1818

19-
## Related articles
19+
### Related articles
2020

21-
### [Simple Java REST Client Using java.net.URL package](http://howtoprogram.xyz/2016/07/02/simple-java-rest-client-using-java-net-url-package)
21+
[Simple Java REST Client Using java.net.URL package](http://howtoprogram.xyz/2016/07/02/simple-java-rest-client-using-java-net-url-package)
2222

23-
### [Java REST Client Using Spring RestTemplate](http://howtoprogram.xyz/2016/07/03/java-rest-client-using-spring-resttemplate/)
23+
[Java REST Client Using Spring RestTemplate](http://howtoprogram.xyz/2016/07/03/java-rest-client-using-spring-resttemplate/)
2424

25-
### [Java REST Client Using Apache Httpcomponents](http://howtoprogram.xyz/2016/07/04/java-rest-client-using-apache-httpcomponents/)
25+
[Java REST Client Using Jersey Client](http://howtoprogram.xyz/2016/07/05/java-rest-client-using-jersey-client/)
2626

27-
### [Java REST Client Using Jersey Client](http://howtoprogram.xyz/2016/07/05/java-rest-client-using-jersey-client/)
27+
[Java REST Client Using Resteasy Client](http://howtoprogram.xyz/2016/07/12/java-rest-client-using-resteasy-client/)
2828

29-
### [Java REST Client Using Resteasy Client](http://howtoprogram.xyz/2016/07/12/java-rest-client-using-resteasy-client/)
29+
[Java REST Client Using Resteasy Client Proxy Framework](http://howtoprogram.xyz/2016/07/13/java-rest-client-using-resteasy-client-proxy-framework/)
3030

31-
### [Java REST Client Using Resteasy Client Proxy Framework](http://howtoprogram.xyz/2016/07/13/java-rest-client-using-resteasy-client-proxy-framework/)
31+
[Java REST Client Using Apache CXF Proxy based API](http://howtoprogram.xyz/2016/07/15/java-rest-client-using-apache-cxf-proxy-based-api/)
3232

33-
### [Java REST Client Using Apache CXF Proxy based API](http://howtoprogram.xyz/2016/07/15/java-rest-client-using-apache-cxf-proxy-based-api/)
33+
[Java REST Client Using Netflix Feign](http://howtoprogram.xyz/2016/07/18/java-rest-client-using-netflix-feign/)
3434

35-
### [Java REST Client Using Netflix Feign](http://howtoprogram.xyz/2016/07/18/java-rest-client-using-netflix-feign/)
36-
37-
### [Java REST Client Using Unirest Java API](http://howtoprogram.xyz/2016/07/27/java-rest-client-using-unirest-java-api/)
35+
[Java REST Client Using Unirest Java API](http://howtoprogram.xyz/2016/07/27/java-rest-client-using-unirest-java-api/)

Java-JsonPojo-Example/pom.xml

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,13 @@
66
<version>0.0.1-SNAPSHOT</version>
77

88
<properties>
9+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
910
<java.version>1.8</java.version>
11+
<junit.version>4.12</junit.version>
12+
<junit.jupiter.version>5.0.0</junit.jupiter.version>
13+
<junit.vintage.version>${junit.version}.0</junit.vintage.version>
14+
<junit.jupiter.version>5.0.0</junit.jupiter.version>
15+
<junit.platform.version>1.0.0</junit.platform.version>
1016
</properties>
1117

1218
<dependencies>
@@ -15,5 +21,66 @@
1521
<artifactId>jackson-databind</artifactId>
1622
<version>2.7.5</version>
1723
</dependency>
24+
<dependency>
25+
<groupId>com.google.code.gson</groupId>
26+
<artifactId>gson</artifactId>
27+
<version>2.8.1</version>
28+
<scope>compile</scope>
29+
</dependency>
30+
<dependency>
31+
<groupId>org.junit.jupiter</groupId>
32+
<artifactId>junit-jupiter-engine</artifactId>
33+
<version>${junit.jupiter.version}</version>
34+
<scope>test</scope>
35+
</dependency>
36+
<dependency>
37+
<groupId>junit</groupId>
38+
<artifactId>junit</artifactId>
39+
<version>${junit.version}</version>
40+
<scope>test</scope>
41+
</dependency>
42+
<dependency>
43+
<groupId>org.junit.platform</groupId>
44+
<artifactId>junit-platform-runner</artifactId>
45+
<version>${junit.platform.version}</version>
46+
<scope>test</scope>
47+
</dependency>
48+
<dependency>
49+
<groupId>org.junit.vintage</groupId>
50+
<artifactId>junit-vintage-engine</artifactId>
51+
<version>${junit.vintage.version}</version>
52+
<scope>test</scope>
53+
</dependency>
1854
</dependencies>
55+
<build>
56+
<plugins>
57+
<plugin>
58+
<artifactId>maven-compiler-plugin</artifactId>
59+
<version>3.7.0</version>
60+
<configuration>
61+
<source>${java.version}</source>
62+
<target>${java.version}</target>
63+
</configuration>
64+
</plugin>
65+
<plugin>
66+
<artifactId>maven-surefire-plugin</artifactId>
67+
<version>2.19.1</version>
68+
<configuration>
69+
<includes>
70+
<include>**/Test*.java</include>
71+
<include>**/*Test.java</include>
72+
<include>**/*Tests.java</include>
73+
<include>**/*TestCase.java</include>
74+
</includes>
75+
</configuration>
76+
<dependencies>
77+
<dependency>
78+
<groupId>org.junit.platform</groupId>
79+
<artifactId>junit-platform-surefire-provider</artifactId>
80+
<version>${junit.platform.version}</version>
81+
</dependency>
82+
</dependencies>
83+
</plugin>
84+
</plugins>
85+
</build>
1986
</project>
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package com.howtoprogram.gson;
2+
3+
import static org.junit.jupiter.api.Assertions.assertTrue;
4+
5+
import java.util.ArrayList;
6+
import java.util.HashMap;
7+
import java.util.HashSet;
8+
import java.util.List;
9+
import java.util.Map;
10+
import java.util.Set;
11+
12+
import org.junit.jupiter.api.Test;
13+
14+
import com.google.gson.Gson;
15+
import com.google.gson.GsonBuilder;
16+
import com.howtoprogram.jsonpojo.Book;
17+
18+
public class GsonTest {
19+
20+
@Test
21+
public void testConvertOjectToJsonTest() {
22+
Book milkHoneyBook = new Book(1000L, "Milk and Honey", "Rupi Kaur");
23+
24+
Gson gson = new GsonBuilder().setPrettyPrinting().create();
25+
String json = gson.toJson(milkHoneyBook);
26+
27+
assertTrue(json.contains("Milk and Honey"));
28+
System.out.print(json);
29+
}
30+
31+
@Test
32+
public void testConvertListToJsonTest() {
33+
Book sfBook = new Book(1001L, "StrengthsFinder 2.0", "Tom Rath");
34+
Book pHBook = new Book(1002L, "The Power of Habit", "Charles Duhigg");
35+
List<Book> books = new ArrayList<Book>();
36+
books.add(sfBook);
37+
books.add(pHBook);
38+
39+
// Converts the list to JSON
40+
Gson gson = new GsonBuilder().setPrettyPrinting().create();
41+
String json = gson.toJson(books);
42+
assertTrue(json.contains("StrengthsFinder 2.0"));
43+
assertTrue(json.contains("The Power of Habit"));
44+
45+
System.out.println(json);
46+
}
47+
48+
@Test
49+
public void testConvertSetToJsonTest() {
50+
Book litteFireBook = new Book(1001L, "Little Fires Everywhere", "Celeste Ng");
51+
Book leoDaVinciBook = new Book(1002L, "Leonardo da Vinci", "Walter Isaacson");
52+
Set<Book> books = new HashSet<Book>();
53+
books.add(litteFireBook);
54+
books.add(leoDaVinciBook);
55+
56+
// Converts the set to JSON
57+
Gson gson = new GsonBuilder().setPrettyPrinting().create();
58+
String json = gson.toJson(books);
59+
assertTrue(json.contains("Little Fires Everywhere"));
60+
assertTrue(json.contains("Leonardo da Vinci"));
61+
62+
System.out.println(json);
63+
}
64+
65+
@Test
66+
public void testConvertMapToJsonTest() {
67+
Book eoBook = new Book(1l, "Extreme Ownership", "Jocko Willink");
68+
Book lopBook = new Book(2l, "The 48 Laws of Power", "Robert Greene");
69+
Map<Long, Book> booksMap = new HashMap<Long, Book>();
70+
booksMap.put(eoBook.getId(), eoBook);
71+
booksMap.put(lopBook.getId(), lopBook);
72+
73+
Gson gson = new GsonBuilder().setPrettyPrinting().create();
74+
String json = gson.toJson(booksMap);
75+
76+
assertTrue(json.contains("Extreme Ownership"));
77+
assertTrue(json.contains("The 48 Laws of Power"));
78+
System.out.println(json);
79+
}
80+
81+
}

0 commit comments

Comments
 (0)