Skip to content

Commit 0d987a3

Browse files
committed
gson module fix #1590
1 parent 189d5b4 commit 0d987a3

File tree

6 files changed

+311
-3
lines changed

6 files changed

+311
-3
lines changed

docs/asciidoc/modules/gson.adoc

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
== Gson
2+
3+
JSON support using https://github.com/google/gson[Gson] library.
4+
5+
=== Usage
6+
7+
1) Add the dependency:
8+
9+
[dependency, artifactId="jooby-gson"]
10+
.
11+
12+
2) Install and encode/decode JSON
13+
14+
.Java
15+
[source, java, role="primary"]
16+
----
17+
import io.jooby.json.GsonModule;
18+
19+
{
20+
install(new GsonModule()); <1>
21+
22+
get("/", ctx -> {
23+
MyObject myObject = ...;
24+
return myObject; <2>
25+
});
26+
27+
post("/", ctx -> {
28+
MyObject myObject = ctx.body(MyObject.class); <3>
29+
...
30+
});
31+
}
32+
----
33+
34+
.Kotlin
35+
[source, kt, role="secondary"]
36+
----
37+
import io.jooby.json.GsonModule;
38+
39+
{
40+
install(GsonModule()) <1>
41+
42+
get("/") {
43+
val myObject = ...;
44+
myObject <2>
45+
}
46+
47+
post("/") {
48+
val myObject = ctx.body<MyObject>() <3>
49+
...
50+
}
51+
}
52+
----
53+
54+
<1> Install Gson
55+
<2> Use Jackson to encode arbitrary object as JSON
56+
<3> Use Jackson to decode JSON to Java object. Client must specify the `Content-Type: application/json` header
57+
58+
=== Working with Gson
59+
60+
Access to default object mapper is available via require call:
61+
62+
.Default object mapper
63+
[source, java, role="primary"]
64+
----
65+
import io.jooby.json.GsonModule;
66+
67+
{
68+
install(new GsonModule());
69+
70+
Gson gson = require(Gson.class);
71+
72+
...
73+
}
74+
----
75+
76+
.Kotlin
77+
[source, kt, role="secondary"]
78+
----
79+
import io.jooby.json.GsonModule
80+
81+
{
82+
install(GsonModule())
83+
84+
val mapper = require<Gson>()
85+
}
86+
----
87+
88+
You can provide your own `Gson`:
89+
90+
.Custom ObjectMapper
91+
[source, java, role="primary"]
92+
----
93+
import io.jooby.json.GsonModule;
94+
95+
{
96+
Gson gson = new GsonBuilder().create();
97+
98+
install(new GsonModule(gson));
99+
}
100+
----
101+
102+
.Kotlin
103+
[source, kt, role="secondary"]
104+
----
105+
import io.jooby.json.GsonModule
106+
107+
{
108+
val gson = GsonBuilder().create()
109+
110+
install(GsonModule(gson))
111+
}
112+
----

modules/jooby-bom/pom.xml

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
<!-- THIS FILE IS AUTO GENERATED. DON'T EDIT -->
1717

1818
<properties>
19-
<jooby.version>2.7.1</jooby.version>
19+
<jooby.version>2.7.2-SNAPSHOT</jooby.version>
2020
<HikariCP.version>3.4.2</HikariCP.version>
2121
<archetype-packaging.version>3.1.2</archetype-packaging.version>
2222
<asm.version>7.3.1</asm.version>
@@ -38,6 +38,7 @@
3838
<gradle-plugins.version>0.9-rc-1</gradle-plugins.version>
3939
<gradle-tooling-api.version>2.6</gradle-tooling-api.version>
4040
<graphql-java.version>14.0</graphql-java.version>
41+
<gson.version>2.8.6</gson.version>
4142
<guava.version>28.2-jre</guava.version>
4243
<guice.version>4.2.2</guice.version>
4344
<h2.version>1.4.200</h2.version>
@@ -53,8 +54,8 @@
5354
<jetty.version>9.4.27.v20200227</jetty.version>
5455
<jfiglet.version>0.0.8</jfiglet.version>
5556
<jmespath-java.version>1.11.748</jmespath-java.version>
56-
<jooby-maven-plugin.version>2.7.1</jooby-maven-plugin.version>
57-
<jooby.version>2.7.1</jooby.version>
57+
<jooby-maven-plugin.version>2.7.2-SNAPSHOT</jooby-maven-plugin.version>
58+
<jooby.version>2.7.2-SNAPSHOT</jooby.version>
5859
<json.version>20190722</json.version>
5960
<jsonwebtoken.version>0.11.1</jsonwebtoken.version>
6061
<jsr305.version>3.0.2</jsr305.version>
@@ -152,6 +153,12 @@
152153
<version>${jooby.version}</version>
153154
<type>jar</type>
154155
</dependency>
156+
<dependency>
157+
<groupId>io.jooby</groupId>
158+
<artifactId>jooby-gson</artifactId>
159+
<version>${jooby.version}</version>
160+
<type>jar</type>
161+
</dependency>
155162
<dependency>
156163
<groupId>io.jooby</groupId>
157164
<artifactId>jooby-openapi</artifactId>
@@ -356,6 +363,12 @@
356363
<version>${config.version}</version>
357364
<type>jar</type>
358365
</dependency>
366+
<dependency>
367+
<groupId>com.google.code.gson</groupId>
368+
<artifactId>gson</artifactId>
369+
<version>${gson.version}</version>
370+
<type>jar</type>
371+
</dependency>
359372
<dependency>
360373
<groupId>com.fasterxml.jackson.core</groupId>
361374
<artifactId>jackson-core</artifactId>

modules/jooby-gson/pom.xml

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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/maven-v4_0_0.xsd">
5+
6+
<parent>
7+
<groupId>io.jooby</groupId>
8+
<artifactId>modules</artifactId>
9+
<version>2.7.2-SNAPSHOT</version>
10+
</parent>
11+
12+
<modelVersion>4.0.0</modelVersion>
13+
<artifactId>jooby-gson</artifactId>
14+
15+
<dependencies>
16+
<dependency>
17+
<groupId>com.google.code.findbugs</groupId>
18+
<artifactId>jsr305</artifactId>
19+
<scope>provided</scope>
20+
</dependency>
21+
22+
<dependency>
23+
<groupId>io.jooby</groupId>
24+
<artifactId>jooby</artifactId>
25+
<version>${jooby.version}</version>
26+
</dependency>
27+
28+
<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
29+
<dependency>
30+
<groupId>com.google.code.gson</groupId>
31+
<artifactId>gson</artifactId>
32+
</dependency>
33+
34+
<!-- Test dependencies -->
35+
<dependency>
36+
<groupId>org.junit.jupiter</groupId>
37+
<artifactId>junit-jupiter-engine</artifactId>
38+
<scope>test</scope>
39+
</dependency>
40+
41+
<dependency>
42+
<groupId>org.jacoco</groupId>
43+
<artifactId>org.jacoco.agent</artifactId>
44+
<classifier>runtime</classifier>
45+
<scope>test</scope>
46+
</dependency>
47+
48+
<dependency>
49+
<groupId>org.mockito</groupId>
50+
<artifactId>mockito-core</artifactId>
51+
<scope>test</scope>
52+
</dependency>
53+
</dependencies>
54+
</project>
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/**
2+
* Jooby https://jooby.io
3+
* Apache License Version 2.0 https://jooby.io/LICENSE.txt
4+
* Copyright 2014 Edgar Espina
5+
*/
6+
package io.jooby.json;
7+
8+
import com.google.gson.Gson;
9+
import com.google.gson.GsonBuilder;
10+
import io.jooby.Body;
11+
import io.jooby.Context;
12+
import io.jooby.Extension;
13+
import io.jooby.Jooby;
14+
import io.jooby.MediaType;
15+
import io.jooby.MessageDecoder;
16+
import io.jooby.MessageEncoder;
17+
import io.jooby.ServiceRegistry;
18+
19+
import javax.annotation.Nonnull;
20+
import java.io.ByteArrayInputStream;
21+
import java.io.InputStream;
22+
import java.io.InputStreamReader;
23+
import java.lang.reflect.Type;
24+
25+
import static java.nio.charset.StandardCharsets.UTF_8;
26+
27+
/**
28+
* JSON module using Gson: https://github.com/google/gson.
29+
*
30+
* Usage:
31+
*
32+
* <pre>{@code
33+
* {
34+
*
35+
* install(new GsonModule());
36+
*
37+
* get("/", ctx -> {
38+
* MyObject myObject = ...;
39+
* // send json
40+
* return myObject;
41+
* });
42+
*
43+
* post("/", ctx -> {
44+
* // read json
45+
* MyObject myObject = ctx.body(MyObject.class);
46+
* // send json
47+
* return myObject;
48+
* });
49+
* }
50+
* }</pre>
51+
*
52+
* For body decoding the client must specify the <code>Content-Type</code> header set to
53+
* <code>application/json</code>.
54+
*
55+
* You can retrieve the {@link Gson} object via require call:
56+
*
57+
* <pre>{@code
58+
* {
59+
*
60+
* Gson gson = require(Gson.class);
61+
*
62+
* }
63+
* }</pre>
64+
*
65+
* Complete documentation is available at: https://jooby.io/modules/gson.
66+
*
67+
* @author edgar
68+
* @since 2.7.2
69+
*/
70+
public class GsonModule implements Extension, MessageDecoder, MessageEncoder {
71+
72+
private Gson gson;
73+
74+
/**
75+
* Creates a new module and use a Gson instance.
76+
*
77+
* @param gson Gson to use.
78+
*/
79+
public GsonModule(@Nonnull Gson gson) {
80+
this.gson = gson;
81+
}
82+
83+
/**
84+
* Creates a new Gson module.
85+
*/
86+
public GsonModule() {
87+
this(new GsonBuilder().create());
88+
}
89+
90+
@Override public void install(@Nonnull Jooby application) throws Exception {
91+
application.decoder(MediaType.json, this);
92+
application.encoder(MediaType.json, this);
93+
94+
ServiceRegistry services = application.getServices();
95+
services.put(Gson.class, gson);
96+
}
97+
98+
@Nonnull @Override public Object decode(@Nonnull Context ctx, @Nonnull Type type)
99+
throws Exception {
100+
Body body = ctx.body();
101+
if (body.isInMemory()) {
102+
return gson.fromJson(new InputStreamReader(new ByteArrayInputStream(body.bytes())), type);
103+
} else {
104+
try (InputStream stream = body.stream()) {
105+
return gson.fromJson(new InputStreamReader(stream), type);
106+
}
107+
}
108+
}
109+
110+
@Nonnull @Override public byte[] encode(@Nonnull Context ctx, @Nonnull Object value) {
111+
ctx.setDefaultResponseType(MediaType.json);
112+
return gson.toJson(value).getBytes(UTF_8);
113+
}
114+
}

modules/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
<module>jooby-ebean</module>
3535

3636
<module>jooby-jackson</module>
37+
<module>jooby-gson</module>
3738
<module>jooby-graphql</module>
3839
<module>jooby-graphiql</module>
3940
<module>jooby-graphql-playground</module>

pom.xml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
<handlebars.version>4.1.2</handlebars.version>
2424
<pebble.version>3.1.2</pebble.version>
2525
<jackson.version>2.10.3</jackson.version>
26+
<gson.version>2.8.6</gson.version>
2627
<rocker.version>1.2.3</rocker.version>
2728

2829
<!-- data -->
@@ -239,6 +240,12 @@
239240
<version>${jooby.version}</version>
240241
</dependency>
241242

243+
<dependency>
244+
<groupId>io.jooby</groupId>
245+
<artifactId>jooby-gson</artifactId>
246+
<version>${jooby.version}</version>
247+
</dependency>
248+
242249
<dependency>
243250
<groupId>io.jooby</groupId>
244251
<artifactId>jooby-openapi</artifactId>
@@ -448,6 +455,13 @@
448455
<version>${config.version}</version>
449456
</dependency>
450457

458+
<!-- gson -->
459+
<dependency>
460+
<groupId>com.google.code.gson</groupId>
461+
<artifactId>gson</artifactId>
462+
<version>${gson.version}</version>
463+
</dependency>
464+
451465
<!-- Jackson 2.x -->
452466
<dependency>
453467
<groupId>com.fasterxml.jackson.core</groupId>

0 commit comments

Comments
 (0)