-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature Encoder/Decoder supports Fastjson2 (#2368)
* feature Encoder/Decoder supports Fastjson2 * add Fastjson2 to the mind map
- Loading branch information
1 parent
4853754
commit fc45959
Showing
9 changed files
with
472 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
Fastjson2 Codec | ||
=================== | ||
|
||
This module adds support for encoding and decoding JSON via Fastjson2. | ||
|
||
Add `Fastjson2Encoder` and/or `Fastjson2Decoder` to your `Feign.Builder` like so: | ||
|
||
```java | ||
GitHub github = Feign.builder() | ||
.encoder(new Fastjson2Encoder()) | ||
.decoder(new Fastjson2Decoder()) | ||
.target(GitHub.class, "https://api.github.com"); | ||
``` | ||
|
||
If you want to customize, provide it to the `Fastjson2Encoder` and `Fastjson2Decoder`: | ||
|
||
```java | ||
GitHub github = Feign.builder() | ||
.encoder(new Fastjson2Encoder(new JSONWriter.Feature[]{JSONWriter.Feature.WriteNonStringValueAsString}) | ||
.decoder(new Fastjson2Decoder(new JSONReader.Feature[]{JSONReader.Feature.EmptyStringAsNull})) | ||
.target(GitHub.class, "https://api.github.com"); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- | ||
Copyright 2012-2024 The Feign Authors | ||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except | ||
in compliance with the License. You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software distributed under the License | ||
is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express | ||
or implied. See the License for the specific language governing permissions and limitations under | ||
the License. | ||
--> | ||
<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"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<parent> | ||
<groupId>io.github.openfeign</groupId> | ||
<artifactId>parent</artifactId> | ||
<version>13.3-SNAPSHOT</version> | ||
</parent> | ||
|
||
<artifactId>feign-fastjson2</artifactId> | ||
<name>Feign Fastjson2</name> | ||
<description>Feign Fastjson2</description> | ||
|
||
<properties> | ||
<main.basedir>${project.basedir}/..</main.basedir> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>${project.groupId}</groupId> | ||
<artifactId>feign-core</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>com.alibaba.fastjson2</groupId> | ||
<artifactId>fastjson2</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>${project.groupId}</groupId> | ||
<artifactId>feign-core</artifactId> | ||
<type>test-jar</type> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
</project> |
60 changes: 60 additions & 0 deletions
60
fastjson2/src/main/java/feign/fastjson2/Fastjson2Decoder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* Copyright 2012-2024 The Feign Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except | ||
* in compliance with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License | ||
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express | ||
* or implied. See the License for the specific language governing permissions and limitations under | ||
* the License. | ||
*/ | ||
package feign.fastjson2; | ||
|
||
import static feign.Util.ensureClosed; | ||
|
||
import com.alibaba.fastjson2.JSON; | ||
import com.alibaba.fastjson2.JSONException; | ||
import com.alibaba.fastjson2.JSONReader; | ||
import feign.FeignException; | ||
import feign.Response; | ||
import feign.Util; | ||
import feign.codec.Decoder; | ||
import java.io.IOException; | ||
import java.io.Reader; | ||
import java.lang.reflect.Type; | ||
|
||
/** | ||
* @author changjin wei(魏昌进) | ||
*/ | ||
public class Fastjson2Decoder implements Decoder { | ||
|
||
private final JSONReader.Feature[] features; | ||
|
||
public Fastjson2Decoder() { | ||
this(new JSONReader.Feature[0]); | ||
} | ||
|
||
public Fastjson2Decoder(JSONReader.Feature[] features) { | ||
this.features = features; | ||
} | ||
|
||
@Override | ||
public Object decode(Response response, Type type) throws IOException, FeignException { | ||
if (response.status() == 404 || response.status() == 204) return Util.emptyValueOf(type); | ||
if (response.body() == null) return null; | ||
Reader reader = response.body().asReader(response.charset()); | ||
try { | ||
return JSON.parseObject(reader, type, features); | ||
} catch (JSONException e) { | ||
if (e.getCause() != null && e.getCause() instanceof IOException) { | ||
throw IOException.class.cast(e.getCause()); | ||
} | ||
throw e; | ||
} finally { | ||
ensureClosed(reader); | ||
} | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
fastjson2/src/main/java/feign/fastjson2/Fastjson2Encoder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* Copyright 2012-2024 The Feign Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except | ||
* in compliance with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License | ||
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express | ||
* or implied. See the License for the specific language governing permissions and limitations under | ||
* the License. | ||
*/ | ||
package feign.fastjson2; | ||
|
||
import com.alibaba.fastjson2.JSON; | ||
import com.alibaba.fastjson2.JSONWriter; | ||
import feign.RequestTemplate; | ||
import feign.Util; | ||
import feign.codec.EncodeException; | ||
import feign.codec.Encoder; | ||
import java.lang.reflect.Type; | ||
|
||
/** | ||
* @author changjin wei(魏昌进) | ||
*/ | ||
public class Fastjson2Encoder implements Encoder { | ||
|
||
private final JSONWriter.Feature[] features; | ||
|
||
public Fastjson2Encoder() { | ||
this(new JSONWriter.Feature[0]); | ||
} | ||
|
||
public Fastjson2Encoder(JSONWriter.Feature[] features) { | ||
this.features = features; | ||
} | ||
|
||
@Override | ||
public void encode(Object object, Type bodyType, RequestTemplate template) | ||
throws EncodeException { | ||
template.body(JSON.toJSONBytes(object, features), Util.UTF_8); | ||
} | ||
} |
Oops, something went wrong.