Skip to content

Commit fc45959

Browse files
authored
feature Encoder/Decoder supports Fastjson2 (#2368)
* feature Encoder/Decoder supports Fastjson2 * add Fastjson2 to the mind map
1 parent 4853754 commit fc45959

File tree

9 files changed

+472
-0
lines changed

9 files changed

+472
-0
lines changed

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,23 @@ public class Example {
474474
475475
NB: you may also need to add `SOAPErrorDecoder` if SOAP Faults are returned in response with error http codes (4xx, 5xx, ...)
476476
477+
#### Fastjson2
478+
479+
[fastjson2](./fastjson2) includes an encoder and decoder you can use with a JSON API.
480+
481+
Add `Fastjson2Encoder` and/or `Fastjson2Decoder` to your `Feign.Builder` like so:
482+
483+
```java
484+
public class Example {
485+
public static void main(String[] args) {
486+
GitHub github = Feign.builder()
487+
.encoder(new Fastjson2Encoder())
488+
.decoder(new Fastjson2Decoder())
489+
.target(GitHub.class, "https://api.github.com");
490+
}
491+
}
492+
```
493+
477494
### Contract
478495
479496
#### JAX-RS

fastjson2/README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
Fastjson2 Codec
2+
===================
3+
4+
This module adds support for encoding and decoding JSON via Fastjson2.
5+
6+
Add `Fastjson2Encoder` and/or `Fastjson2Decoder` to your `Feign.Builder` like so:
7+
8+
```java
9+
GitHub github = Feign.builder()
10+
.encoder(new Fastjson2Encoder())
11+
.decoder(new Fastjson2Decoder())
12+
.target(GitHub.class, "https://api.github.com");
13+
```
14+
15+
If you want to customize, provide it to the `Fastjson2Encoder` and `Fastjson2Decoder`:
16+
17+
```java
18+
GitHub github = Feign.builder()
19+
.encoder(new Fastjson2Encoder(new JSONWriter.Feature[]{JSONWriter.Feature.WriteNonStringValueAsString})
20+
.decoder(new Fastjson2Decoder(new JSONReader.Feature[]{JSONReader.Feature.EmptyStringAsNull}))
21+
.target(GitHub.class, "https://api.github.com");
22+
```

fastjson2/pom.xml

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
4+
Copyright 2012-2024 The Feign Authors
5+
6+
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
7+
in compliance with the License. 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 distributed under the License
12+
is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
13+
or implied. See the License for the specific language governing permissions and limitations under
14+
the License.
15+
16+
-->
17+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
18+
<modelVersion>4.0.0</modelVersion>
19+
20+
<parent>
21+
<groupId>io.github.openfeign</groupId>
22+
<artifactId>parent</artifactId>
23+
<version>13.3-SNAPSHOT</version>
24+
</parent>
25+
26+
<artifactId>feign-fastjson2</artifactId>
27+
<name>Feign Fastjson2</name>
28+
<description>Feign Fastjson2</description>
29+
30+
<properties>
31+
<main.basedir>${project.basedir}/..</main.basedir>
32+
</properties>
33+
34+
<dependencies>
35+
<dependency>
36+
<groupId>${project.groupId}</groupId>
37+
<artifactId>feign-core</artifactId>
38+
</dependency>
39+
40+
<dependency>
41+
<groupId>com.alibaba.fastjson2</groupId>
42+
<artifactId>fastjson2</artifactId>
43+
</dependency>
44+
45+
<dependency>
46+
<groupId>${project.groupId}</groupId>
47+
<artifactId>feign-core</artifactId>
48+
<type>test-jar</type>
49+
<scope>test</scope>
50+
</dependency>
51+
</dependencies>
52+
</project>
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Copyright 2012-2024 The Feign Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5+
* in compliance with the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License
10+
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11+
* or implied. See the License for the specific language governing permissions and limitations under
12+
* the License.
13+
*/
14+
package feign.fastjson2;
15+
16+
import static feign.Util.ensureClosed;
17+
18+
import com.alibaba.fastjson2.JSON;
19+
import com.alibaba.fastjson2.JSONException;
20+
import com.alibaba.fastjson2.JSONReader;
21+
import feign.FeignException;
22+
import feign.Response;
23+
import feign.Util;
24+
import feign.codec.Decoder;
25+
import java.io.IOException;
26+
import java.io.Reader;
27+
import java.lang.reflect.Type;
28+
29+
/**
30+
* @author changjin wei(魏昌进)
31+
*/
32+
public class Fastjson2Decoder implements Decoder {
33+
34+
private final JSONReader.Feature[] features;
35+
36+
public Fastjson2Decoder() {
37+
this(new JSONReader.Feature[0]);
38+
}
39+
40+
public Fastjson2Decoder(JSONReader.Feature[] features) {
41+
this.features = features;
42+
}
43+
44+
@Override
45+
public Object decode(Response response, Type type) throws IOException, FeignException {
46+
if (response.status() == 404 || response.status() == 204) return Util.emptyValueOf(type);
47+
if (response.body() == null) return null;
48+
Reader reader = response.body().asReader(response.charset());
49+
try {
50+
return JSON.parseObject(reader, type, features);
51+
} catch (JSONException e) {
52+
if (e.getCause() != null && e.getCause() instanceof IOException) {
53+
throw IOException.class.cast(e.getCause());
54+
}
55+
throw e;
56+
} finally {
57+
ensureClosed(reader);
58+
}
59+
}
60+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright 2012-2024 The Feign Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5+
* in compliance with the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License
10+
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11+
* or implied. See the License for the specific language governing permissions and limitations under
12+
* the License.
13+
*/
14+
package feign.fastjson2;
15+
16+
import com.alibaba.fastjson2.JSON;
17+
import com.alibaba.fastjson2.JSONWriter;
18+
import feign.RequestTemplate;
19+
import feign.Util;
20+
import feign.codec.EncodeException;
21+
import feign.codec.Encoder;
22+
import java.lang.reflect.Type;
23+
24+
/**
25+
* @author changjin wei(魏昌进)
26+
*/
27+
public class Fastjson2Encoder implements Encoder {
28+
29+
private final JSONWriter.Feature[] features;
30+
31+
public Fastjson2Encoder() {
32+
this(new JSONWriter.Feature[0]);
33+
}
34+
35+
public Fastjson2Encoder(JSONWriter.Feature[] features) {
36+
this.features = features;
37+
}
38+
39+
@Override
40+
public void encode(Object object, Type bodyType, RequestTemplate template)
41+
throws EncodeException {
42+
template.body(JSON.toJSONBytes(object, features), Util.UTF_8);
43+
}
44+
}

0 commit comments

Comments
 (0)