Skip to content

Commit cb67955

Browse files
Add Gson Support (#10265)
* Add Gson Support * Update dependencies and annotationProcessorPaths * Fixed annotation processor path for all updated modules, where new Feature API is used. * Trying to decrease number of modules in "others" module set to reduce duplicate invocations and shorten execution time. Added codegen, http, and builder to `core` --------- Co-authored-by: Tomas Langer <[email protected]>
1 parent 224c138 commit cb67955

File tree

19 files changed

+891
-28
lines changed

19 files changed

+891
-28
lines changed

.mvn/cache-config.xml

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<!--
33
4-
Copyright (c) 2024 Oracle and/or its affiliates.
4+
Copyright (c) 2024, 2025 Oracle and/or its affiliates.
55
66
Licensed under the Apache License, Version 2.0 (the "License");
77
you may not use this file except in compliance with the License.
@@ -117,6 +117,9 @@
117117
<moduleSets>
118118
<moduleSet name="core">
119119
<includes>
120+
<include>codegen/**</include>
121+
<include>http/**</include>
122+
<include>builder/**</include>
120123
<include>webserver/**</include>
121124
<include>webclient/**</include>
122125
<include>common/**</include>
@@ -133,6 +136,17 @@
133136
<includes>
134137
<include>**/*</include>
135138
</includes>
139+
<excludes>
140+
<exclude>codegen/**</exclude>
141+
<exclude>http/**</exclude>
142+
<exclude>builder/**</exclude>
143+
<exclude>webserver/**</exclude>
144+
<exclude>webclient/**</exclude>
145+
<exclude>common/**</exclude>
146+
<exclude>config/**</exclude>
147+
<exclude>security/**</exclude>
148+
<exclude>integrations/**</exclude>
149+
</excludes>
136150
</moduleSet>
137151
</moduleSets>
138152
</reactorRule>

http/media/gson/pom.xml

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
Copyright (c) 2023, 2025 Oracle and/or its affiliates.
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
-->
17+
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
18+
xmlns="http://maven.apache.org/POM/4.0.0"
19+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
20+
<modelVersion>4.0.0</modelVersion>
21+
<parent>
22+
<groupId>io.helidon.http.media</groupId>
23+
<artifactId>helidon-http-media-project</artifactId>
24+
<version>4.3.0-SNAPSHOT</version>
25+
</parent>
26+
27+
<artifactId>helidon-http-media-gson</artifactId>
28+
<name>Helidon HTTP Media Gson</name>
29+
30+
<properties>
31+
<!-- this must be disabled, as jars contain module-info.java, but Javadoc is generated without modules,
32+
so generated links were invalid -->
33+
<javadoc.fail-on-warnings>false</javadoc.fail-on-warnings>
34+
</properties>
35+
36+
<dependencies>
37+
<dependency>
38+
<groupId>io.helidon.common</groupId>
39+
<artifactId>helidon-common</artifactId>
40+
</dependency>
41+
<dependency>
42+
<groupId>io.helidon.common</groupId>
43+
<artifactId>helidon-common-config</artifactId>
44+
</dependency>
45+
<dependency>
46+
<groupId>io.helidon.http</groupId>
47+
<artifactId>helidon-http</artifactId>
48+
</dependency>
49+
<dependency>
50+
<groupId>io.helidon.common</groupId>
51+
<artifactId>helidon-common-media-type</artifactId>
52+
</dependency>
53+
<dependency>
54+
<groupId>io.helidon.http.media</groupId>
55+
<artifactId>helidon-http-media</artifactId>
56+
</dependency>
57+
<dependency>
58+
<groupId>com.google.code.gson</groupId>
59+
<artifactId>gson</artifactId>
60+
</dependency>
61+
<dependency>
62+
<groupId>io.helidon.common.features</groupId>
63+
<artifactId>helidon-common-features-api</artifactId>
64+
<optional>true</optional>
65+
</dependency>
66+
<dependency>
67+
<groupId>org.junit.jupiter</groupId>
68+
<artifactId>junit-jupiter-api</artifactId>
69+
<scope>test</scope>
70+
</dependency>
71+
<dependency>
72+
<groupId>org.hamcrest</groupId>
73+
<artifactId>hamcrest-all</artifactId>
74+
<scope>test</scope>
75+
</dependency>
76+
<dependency>
77+
<groupId>io.helidon.common.testing</groupId>
78+
<artifactId>helidon-common-testing-http-junit5</artifactId>
79+
<scope>test</scope>
80+
</dependency>
81+
</dependencies>
82+
83+
<build>
84+
<plugins>
85+
<plugin>
86+
<groupId>org.apache.maven.plugins</groupId>
87+
<artifactId>maven-compiler-plugin</artifactId>
88+
<dependencies>
89+
<dependency>
90+
<groupId>io.helidon.codegen</groupId>
91+
<artifactId>helidon-codegen-apt</artifactId>
92+
<version>${helidon.version}</version>
93+
</dependency>
94+
<dependency>
95+
<groupId>io.helidon.common.features</groupId>
96+
<artifactId>helidon-common-features-codegen</artifactId>
97+
<version>${helidon.version}</version>
98+
</dependency>
99+
</dependencies>
100+
<configuration>
101+
<annotationProcessorPaths>
102+
<path>
103+
<groupId>io.helidon.codegen</groupId>
104+
<artifactId>helidon-codegen-apt</artifactId>
105+
<version>${helidon.version}</version>
106+
</path>
107+
<path>
108+
<groupId>io.helidon.common.features</groupId>
109+
<artifactId>helidon-common-features-codegen</artifactId>
110+
<version>${helidon.version}</version>
111+
</path>
112+
</annotationProcessorPaths>
113+
</configuration>
114+
</plugin>
115+
</plugins>
116+
</build>
117+
</project>
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright (c) 2025 Oracle and/or its affiliates.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.helidon.http.media.gson;
17+
18+
import io.helidon.common.Weighted;
19+
import io.helidon.common.config.Config;
20+
import io.helidon.http.media.MediaSupport;
21+
import io.helidon.http.media.spi.MediaSupportProvider;
22+
23+
/**
24+
* {@link java.util.ServiceLoader} provider implementation for Gson media support.
25+
*/
26+
public class GsonMediaSupportProvider implements MediaSupportProvider, Weighted {
27+
28+
/**
29+
* This class should be only instantiated as part of java {@link java.util.ServiceLoader}.
30+
*/
31+
@Deprecated
32+
public GsonMediaSupportProvider() {
33+
super();
34+
}
35+
36+
@Override
37+
public String configKey() {
38+
return "gson";
39+
}
40+
41+
@Override
42+
public MediaSupport create(Config config, String name) {
43+
return GsonSupport.create(config, name);
44+
}
45+
46+
@Override
47+
public double weight() {
48+
// very low weight, as this covers all, lower than other JSON libraries
49+
return 5;
50+
}
51+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright (c) 2025 Oracle and/or its affiliates.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.helidon.http.media.gson;
17+
18+
19+
import java.io.InputStream;
20+
import java.io.InputStreamReader;
21+
import java.nio.charset.Charset;
22+
import java.nio.charset.StandardCharsets;
23+
24+
import io.helidon.common.GenericType;
25+
import io.helidon.http.Headers;
26+
import io.helidon.http.HttpMediaType;
27+
import io.helidon.http.media.EntityReader;
28+
29+
import com.google.gson.Gson;
30+
31+
class GsonReader<T> implements EntityReader<T> {
32+
private final Gson gson;
33+
34+
GsonReader(Gson gson) {
35+
this.gson = gson;
36+
}
37+
38+
@Override
39+
public T read(GenericType<T> type, InputStream stream, Headers headers) {
40+
return gson.fromJson(new InputStreamReader(stream, contentTypeCharset(headers)), type.type());
41+
}
42+
43+
@Override
44+
public T read(GenericType<T> type, InputStream stream, Headers requestHeaders, Headers responseHeaders) {
45+
return gson.fromJson(new InputStreamReader(stream, contentTypeCharset(responseHeaders)), type.type());
46+
}
47+
48+
private Charset contentTypeCharset(Headers headers) {
49+
return headers.contentType()
50+
.flatMap(HttpMediaType::charset)
51+
.map(Charset::forName)
52+
.orElse(StandardCharsets.UTF_8);
53+
}
54+
}

0 commit comments

Comments
 (0)