Skip to content

Commit c4734ab

Browse files
committed
Convert String to JsonObject
1 parent d9bce38 commit c4734ab

8 files changed

Lines changed: 137 additions & 15 deletions

File tree

.gitignore

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,26 @@
1-
okhttp-examples/.settings/org.eclipse.core.resources.prefs
2-
okhttp-examples/.settings/org.eclipse.m2e.core.prefs
1+
# Eclipse
2+
.project
3+
.classpath
4+
.settings/
5+
6+
# Idea
7+
.idea/
8+
*.iml
9+
*.iws
10+
*.ipr
11+
12+
# OS
13+
Thumbs.db
14+
.DS_Store
15+
16+
# Maven
17+
target/
18+
19+
# Build
20+
out/
21+
build/
22+
bin/
23+
24+
25+
# Others
26+
*.log

Book-RESTful-Service/.gitignore

Lines changed: 0 additions & 1 deletion
This file was deleted.

Java-JsonPojo-Example/.gitignore

Lines changed: 0 additions & 1 deletion
This file was deleted.

Java-RESTful-Client-Example/.gitignore

Lines changed: 0 additions & 1 deletion
This file was deleted.

gson-examples/pom.xml

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>com.howtoprogram</groupId>
5+
<artifactId>gson-examples</artifactId>
6+
<version>0.0.1-SNAPSHOT</version>
7+
8+
<properties>
9+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
10+
<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>
16+
</properties>
17+
18+
<dependencies>
19+
<dependency>
20+
<groupId>com.google.code.gson</groupId>
21+
<artifactId>gson</artifactId>
22+
<version>2.8.2</version>
23+
<scope>compile</scope>
24+
</dependency>
25+
<dependency>
26+
<groupId>org.junit.jupiter</groupId>
27+
<artifactId>junit-jupiter-engine</artifactId>
28+
<version>${junit.jupiter.version}</version>
29+
<scope>test</scope>
30+
</dependency>
31+
<!-- To run tests on IDE such as Eclipse, Intellij -->
32+
<dependency>
33+
<groupId>junit</groupId>
34+
<artifactId>junit</artifactId>
35+
<version>${junit.version}</version>
36+
<scope>test</scope>
37+
</dependency>
38+
<dependency>
39+
<groupId>org.junit.platform</groupId>
40+
<artifactId>junit-platform-runner</artifactId>
41+
<version>${junit.platform.version}</version>
42+
<scope>test</scope>
43+
</dependency>
44+
<dependency>
45+
<groupId>org.junit.vintage</groupId>
46+
<artifactId>junit-vintage-engine</artifactId>
47+
<version>${junit.vintage.version}</version>
48+
<scope>test</scope>
49+
</dependency>
50+
</dependencies>
51+
52+
<build>
53+
<plugins>
54+
<plugin>
55+
<artifactId>maven-compiler-plugin</artifactId>
56+
<version>3.1</version>
57+
<configuration>
58+
<source>${java.version}</source>
59+
<target>${java.version}</target>
60+
</configuration>
61+
</plugin>
62+
<plugin>
63+
<artifactId>maven-surefire-plugin</artifactId>
64+
<version>2.19</version>
65+
<dependencies>
66+
<dependency>
67+
<groupId>org.junit.platform</groupId>
68+
<artifactId>junit-platform-surefire-provider</artifactId>
69+
<version>${junit.platform.version}</version>
70+
</dependency>
71+
</dependencies>
72+
</plugin>
73+
</plugins>
74+
</build>
75+
</project>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.howtoprogram.gson;
2+
3+
import static org.junit.Assert.*;
4+
5+
import com.google.gson.*;
6+
import org.junit.Test;
7+
8+
public class StringToJsonTest {
9+
10+
@Test
11+
public void testStringToJsonObject() {
12+
13+
String json = "{\"id\":1,\"name\":\"Thinking in Java\",\"author\":\"Bruce Eckel\"}";
14+
JsonObject jsonObj = new JsonParser().parse(json).getAsJsonObject();
15+
16+
assertEquals(1, jsonObj.get("id").getAsInt());
17+
assertEquals("Thinking in Java", jsonObj.get("name").getAsString());
18+
assertEquals("Bruce Eckel", jsonObj.get("author").getAsString());
19+
20+
}
21+
22+
@Test
23+
public void testStringToJsonObjectWithJsonElement() {
24+
25+
String json = "{\"id\":2,\"name\":\"Effective Java\",\"author\":\"Joshua Bloch\"}";
26+
27+
Gson gson = new Gson();
28+
JsonElement element = gson.fromJson(json, JsonElement.class);
29+
JsonObject jsonObject = element.getAsJsonObject();
30+
31+
assertEquals(2, jsonObject.get("id").getAsInt());
32+
assertEquals("Effective Java", jsonObject.get("name").getAsString());
33+
assertEquals("Joshua Bloch", jsonObject.get("author").getAsString());
34+
35+
}
36+
}

okhttp-examples/.gitignore

Lines changed: 0 additions & 4 deletions
This file was deleted.

open-feign-example/.gitignore

Lines changed: 0 additions & 6 deletions
This file was deleted.

0 commit comments

Comments
 (0)