Skip to content

Commit fb1d0c6

Browse files
Merge pull request #3 from Project-Env/feature/gson
added base GsonBuilder factory
2 parents 36753f4 + db12b8d commit fb1d0c6

File tree

8 files changed

+221
-6
lines changed

8 files changed

+221
-6
lines changed

.github/workflows/build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ jobs:
1919
path: ~/.m2
2020
key: ${{ runner.os }}-m2-${{ hashFiles('pom.xml') }}
2121
restore-keys: ${{ runner.os }}-m2
22-
- uses: Project-Env/project-env-github-action@v1.0.0
22+
- uses: Project-Env/project-env-github-action@v1.1.0
2323
with:
2424
cli-version: '3.4.0'
2525
- run: mvn -B -Psonar verify

code/gson/pom.xml

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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+
6+
<modelVersion>4.0.0</modelVersion>
7+
8+
<parent>
9+
<groupId>io.projectenv.commons</groupId>
10+
<artifactId>commons-root</artifactId>
11+
<version>${revision}</version>
12+
</parent>
13+
14+
<artifactId>gson</artifactId>
15+
16+
<dependencies>
17+
<dependency>
18+
<groupId>com.google.code.gson</groupId>
19+
<artifactId>gson</artifactId>
20+
</dependency>
21+
22+
<dependency>
23+
<groupId>org.junit.jupiter</groupId>
24+
<artifactId>junit-jupiter-engine</artifactId>
25+
<scope>test</scope>
26+
</dependency>
27+
<dependency>
28+
<groupId>org.assertj</groupId>
29+
<artifactId>assertj-core</artifactId>
30+
<scope>test</scope>
31+
</dependency>
32+
<dependency>
33+
<groupId>org.apache.commons</groupId>
34+
<artifactId>commons-lang3</artifactId>
35+
</dependency>
36+
</dependencies>
37+
38+
<build>
39+
<plugins>
40+
<plugin>
41+
<groupId>org.apache.maven.plugins</groupId>
42+
<artifactId>maven-deploy-plugin</artifactId>
43+
<configuration>
44+
<skip>false</skip>
45+
</configuration>
46+
</plugin>
47+
</plugins>
48+
</build>
49+
50+
</project>
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package io.projectenv.commons.gson;
2+
3+
import com.google.gson.Gson;
4+
import com.google.gson.GsonBuilder;
5+
import com.google.gson.TypeAdapter;
6+
import com.google.gson.TypeAdapterFactory;
7+
import com.google.gson.stream.JsonReader;
8+
import com.google.gson.stream.JsonWriter;
9+
10+
import java.io.File;
11+
import java.io.IOException;
12+
import java.util.ServiceLoader;
13+
14+
public final class GsonFactory {
15+
16+
private GsonFactory() {
17+
// noop
18+
}
19+
20+
public static Gson createGson() {
21+
return createGsonBuilder().create();
22+
}
23+
24+
public static GsonBuilder createGsonBuilder() {
25+
GsonBuilder gsonBuilder = new GsonBuilder();
26+
for (TypeAdapterFactory factory : ServiceLoader.load(TypeAdapterFactory.class, TypeAdapterFactory.class.getClassLoader())) {
27+
gsonBuilder.registerTypeAdapterFactory(factory);
28+
}
29+
gsonBuilder.registerTypeAdapter(File.class, new FileTypeAdapter());
30+
31+
return gsonBuilder;
32+
}
33+
34+
private static class FileTypeAdapter extends TypeAdapter<File> {
35+
36+
@Override
37+
public void write(JsonWriter out, File value) throws IOException {
38+
if (value != null) {
39+
out.value(value.getCanonicalPath());
40+
} else {
41+
out.nullValue();
42+
}
43+
}
44+
45+
@Override
46+
public File read(JsonReader in) throws IOException {
47+
return new File(in.nextString());
48+
}
49+
50+
}
51+
52+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package io.projectenv.commons.gson;
2+
3+
import com.google.gson.Gson;
4+
import org.junit.jupiter.api.Test;
5+
6+
import java.io.File;
7+
8+
import static org.assertj.core.api.Assertions.assertThat;
9+
10+
class GsonFactoryTest {
11+
12+
@Test
13+
void testFileConversion() {
14+
Gson gson = GsonFactory.createGson();
15+
16+
File nonCanonicalFile = new File("folder/../file.txt");
17+
File canonicalFile = new File("file.txt");
18+
19+
assertThat(gson.toJson(nonCanonicalFile))
20+
.isEqualTo(getWrappedAbsolutePath(canonicalFile));
21+
22+
assertThat(gson.fromJson(getWrappedAbsolutePath(canonicalFile), File.class).getAbsolutePath())
23+
.isEqualTo(canonicalFile.getAbsolutePath());
24+
}
25+
26+
@Test
27+
void testNullFile() {
28+
Gson gson = GsonFactory.createGson();
29+
30+
assertThat(gson.toJson(new FileWrapper(null))).isEqualTo("{}");
31+
}
32+
33+
private String getWrappedAbsolutePath(File value) {
34+
return "\"" + value.getAbsolutePath() + "\"";
35+
}
36+
37+
private static class FileWrapper {
38+
39+
public final File file;
40+
41+
private FileWrapper(File file) {
42+
this.file = file;
43+
}
44+
}
45+
46+
}

code/pom.xml

Lines changed: 65 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,78 @@
1414
<artifactId>commons-root</artifactId>
1515
<packaging>pom</packaging>
1616

17-
<properties>
18-
<maven.compiler.release>8</maven.compiler.release>
19-
</properties>
20-
2117
<modules>
2218
<module>native-image</module>
2319
<module>process</module>
2420
<module>string-substitutor</module>
2521
<module>archive</module>
2622
<module>system</module>
2723
<module>system-test</module>
24+
<module>gson</module>
2825
</modules>
2926

27+
<build>
28+
<plugins>
29+
<plugin>
30+
<groupId>org.apache.maven.plugins</groupId>
31+
<artifactId>maven-source-plugin</artifactId>
32+
<executions>
33+
<execution>
34+
<id>attach-sources</id>
35+
<phase>verify</phase>
36+
<goals>
37+
<goal>jar-no-fork</goal>
38+
</goals>
39+
</execution>
40+
</executions>
41+
</plugin>
42+
<plugin>
43+
<groupId>org.apache.maven.plugins</groupId>
44+
<artifactId>maven-failsafe-plugin</artifactId>
45+
<executions>
46+
<execution>
47+
<goals>
48+
<goal>integration-test</goal>
49+
<goal>verify</goal>
50+
</goals>
51+
</execution>
52+
</executions>
53+
</plugin>
54+
</plugins>
55+
</build>
56+
57+
<profiles>
58+
<profile>
59+
<id>sonar</id>
60+
<build>
61+
<plugins>
62+
<plugin>
63+
<groupId>org.jacoco</groupId>
64+
<artifactId>jacoco-maven-plugin</artifactId>
65+
<executions>
66+
<execution>
67+
<id>prepare-agent</id>
68+
<goals>
69+
<goal>prepare-agent</goal>
70+
</goals>
71+
</execution>
72+
</executions>
73+
</plugin>
74+
<plugin>
75+
<groupId>org.sonarsource.scanner.maven</groupId>
76+
<artifactId>sonar-maven-plugin</artifactId>
77+
<executions>
78+
<execution>
79+
<goals>
80+
<goal>sonar</goal>
81+
</goals>
82+
<phase>verify</phase>
83+
</execution>
84+
</executions>
85+
</plugin>
86+
</plugins>
87+
</build>
88+
</profile>
89+
</profiles>
90+
3091
</project>

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
<reflections.version>0.10.2</reflections.version>
4040
<log4j.version>2.14.1</log4j.version>
4141
<mockito-inline.version>4.0.0</mockito-inline.version>
42+
<junit-jupiter.version>5.8.1</junit-jupiter.version>
4243

4344
<flatten-maven-plugin.version>1.2.7</flatten-maven-plugin.version>
4445
<maven-compiler-plugin.version>3.8.1</maven-compiler-plugin.version>

project-env.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ tools_directory = ".tools"
22

33
[jdk]
44
distribution = "Temurin"
5-
distribution_version = "11.0.13+8"
5+
distribution_version = "17.0.1+12"
66

77
[maven]
88
version = "3.8.3"

tools/sonar-aggregator/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@
4444
<artifactId>system-test</artifactId>
4545
<version>${project.version}</version>
4646
</dependency>
47+
<dependency>
48+
<groupId>io.projectenv.commons</groupId>
49+
<artifactId>gson</artifactId>
50+
<version>${project.version}</version>
51+
</dependency>
4752
</dependencies>
4853

4954
<build>

0 commit comments

Comments
 (0)