-
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.
* Add Moshi * Update README.md --------- Co-authored-by: Vikramaditya Chhapwale <cvikramad@gmail.com>
- Loading branch information
1 parent
fb813e0
commit 215639f
Showing
13 changed files
with
714 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,13 @@ | ||
Moshi Codec | ||
=================== | ||
|
||
This module adds support for encoding and decoding JSON via the Moshi library. | ||
|
||
Add `MoshiEncoder` and/or `MoshiDecoder` to your `Feign.Builder` like so: | ||
|
||
```java | ||
GitHub github = Feign.builder() | ||
.encoder(new MoshiEncoder()) | ||
.decoder(new MoshiDecoder()) | ||
.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,58 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- | ||
Copyright 2012-2023 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.0-SNAPSHOT</version> | ||
</parent> | ||
|
||
<artifactId>feign-moshi</artifactId> | ||
<name>Feign Moshi</name> | ||
<description>Feign Moshi</description> | ||
|
||
<properties> | ||
<main.basedir>${project.basedir}/..</main.basedir> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>${project.groupId}</groupId> | ||
<artifactId>feign-core</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>com.squareup.moshi</groupId> | ||
<artifactId>moshi</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>com.google.guava</groupId> | ||
<artifactId>guava</artifactId> | ||
<version>${guava.version}</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>${project.groupId}</groupId> | ||
<artifactId>feign-core</artifactId> | ||
<type>test-jar</type> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
</project> |
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,70 @@ | ||
/* | ||
* Copyright 2012-2023 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.moshi; | ||
|
||
import static feign.Util.UTF_8; | ||
import static feign.Util.ensureClosed; | ||
|
||
import com.google.common.io.CharStreams; | ||
import com.squareup.moshi.JsonAdapter; | ||
import com.squareup.moshi.JsonDataException; | ||
import com.squareup.moshi.Moshi; | ||
import feign.Response; | ||
import feign.Util; | ||
import feign.codec.Decoder; | ||
import java.io.IOException; | ||
import java.io.Reader; | ||
import java.lang.reflect.Type; | ||
|
||
public class MoshiDecoder implements Decoder { | ||
private final Moshi moshi; | ||
|
||
public MoshiDecoder(Moshi moshi) { | ||
this.moshi = moshi; | ||
} | ||
|
||
public MoshiDecoder() { | ||
this.moshi = new Moshi.Builder().build(); | ||
} | ||
|
||
public MoshiDecoder(Iterable<JsonAdapter<?>> adapters) { | ||
this(MoshiFactory.create(adapters)); | ||
} | ||
|
||
@Override | ||
public Object decode(Response response, Type type) throws IOException { | ||
JsonAdapter<Object> jsonAdapter = moshi.adapter(type); | ||
|
||
if (response.status() == 404 || response.status() == 204) return Util.emptyValueOf(type); | ||
if (response.body() == null) return null; | ||
|
||
Reader reader = response.body().asReader(UTF_8); | ||
|
||
try { | ||
return parseJson(jsonAdapter, reader); | ||
} catch (JsonDataException e) { | ||
if (e.getCause() != null && e.getCause() instanceof IOException) { | ||
throw (IOException) e.getCause(); | ||
} | ||
throw e; | ||
} finally { | ||
ensureClosed(reader); | ||
} | ||
} | ||
|
||
private Object parseJson(JsonAdapter<Object> jsonAdapter, Reader reader) throws IOException { | ||
String targetString = CharStreams.toString(reader); | ||
return jsonAdapter.fromJson(targetString); | ||
} | ||
} |
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,43 @@ | ||
/* | ||
* Copyright 2012-2023 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.moshi; | ||
|
||
import com.squareup.moshi.JsonAdapter; | ||
import com.squareup.moshi.Moshi; | ||
import feign.RequestTemplate; | ||
import feign.codec.Encoder; | ||
import java.lang.reflect.Type; | ||
|
||
public class MoshiEncoder implements Encoder { | ||
|
||
private final Moshi moshi; | ||
|
||
public MoshiEncoder() { | ||
this.moshi = new Moshi.Builder().build(); | ||
} | ||
|
||
public MoshiEncoder(Moshi moshi) { | ||
this.moshi = moshi; | ||
} | ||
|
||
public MoshiEncoder(Iterable<JsonAdapter<?>> adapters) { | ||
this(MoshiFactory.create(adapters)); | ||
} | ||
|
||
@Override | ||
public void encode(Object object, Type bodyType, RequestTemplate template) { | ||
JsonAdapter<Object> jsonAdapter = moshi.adapter(bodyType).indent(" "); | ||
template.body(jsonAdapter.toJson(object)); | ||
} | ||
} |
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,35 @@ | ||
/* | ||
* Copyright 2012-2023 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.moshi; | ||
|
||
import com.squareup.moshi.JsonAdapter; | ||
import com.squareup.moshi.Moshi; | ||
|
||
public class MoshiFactory { | ||
private MoshiFactory() {} | ||
|
||
/** | ||
* Registers JsonAdapter by implicit type. Adds one to read numbers in a {@code Map<String, | ||
* Object>} as Integers. | ||
*/ | ||
static Moshi create(Iterable<JsonAdapter<?>> adapters) { | ||
Moshi.Builder builder = new Moshi.Builder(); | ||
|
||
for (JsonAdapter<?> adapter : adapters) { | ||
builder.add(adapter.getClass(), adapter); | ||
} | ||
|
||
return builder.build(); | ||
} | ||
} |
Oops, something went wrong.