-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add streaming JsonConverter for retrofit
This implements for Jake Wharton's converter as streaming variant (only one direction). For now that's all we need. JakeWharton/retrofit2-kotlinx-serialization-converter#43
- Loading branch information
Showing
6 changed files
with
136 additions
and
1 deletion.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,15 @@ | ||
plugins { | ||
id("android-library-base") | ||
id("android-library-unit-test") | ||
id("android-library-release") | ||
alias(libs.plugins.kotlin.serialization.plugin) | ||
} | ||
|
||
dependencies { | ||
implementation(libs.kotlinx.serialization) | ||
implementation(libs.retrofit.serialization) | ||
implementation(libs.okhttp) | ||
|
||
testImplementation(libs.kotlinx.serialization) | ||
testImplementation(libs.okhttp.mockwebserver) | ||
} |
50 changes: 50 additions & 0 deletions
50
...en-retrofit/src/main/kotlin/de/sipgate/dachlatten/okhttp/StreamingJsonConverterFactory.kt
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,50 @@ | ||
package de.sipgate.dachlatten.okhttp | ||
|
||
import kotlinx.serialization.ExperimentalSerializationApi | ||
import kotlinx.serialization.json.Json | ||
import kotlinx.serialization.json.decodeFromStream | ||
import kotlinx.serialization.json.encodeToStream | ||
import kotlinx.serialization.serializer | ||
import okhttp3.MediaType | ||
import okhttp3.RequestBody | ||
import okhttp3.RequestBody.Companion.toRequestBody | ||
import okhttp3.ResponseBody | ||
import retrofit2.Converter | ||
import retrofit2.Retrofit | ||
import java.io.ByteArrayOutputStream | ||
import java.lang.reflect.Type | ||
|
||
@JvmName("convert") | ||
@ExperimentalSerializationApi | ||
fun Json.asConverterFactory(contentType: MediaType): Converter.Factory = | ||
object : Converter.Factory() { | ||
override fun responseBodyConverter( | ||
type: Type, | ||
annotations: Array<out Annotation>, | ||
retrofit: Retrofit, | ||
) = Converter<ResponseBody, Any> { value -> | ||
this@asConverterFactory.decodeFromStream( | ||
this@asConverterFactory.serializersModule.serializer(type), | ||
value.byteStream(), | ||
) | ||
} | ||
|
||
override fun requestBodyConverter( | ||
type: Type, | ||
parameterAnnotations: Array<out Annotation>, | ||
methodAnnotations: Array<out Annotation>, | ||
retrofit: Retrofit, | ||
) = Converter<Any, RequestBody> { value -> | ||
val stream = ByteArrayOutputStream() | ||
this@asConverterFactory.encodeToStream( | ||
this@asConverterFactory.serializersModule.serializer(type), | ||
value, | ||
stream, | ||
) | ||
stream.toByteArray().toRequestBody( | ||
contentType, | ||
0, | ||
stream.size(), | ||
) | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
...test/kotlin/de/sipgate/dachlatten/okhttp/KotlinSerializationConverterFactoryStringTest.kt
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 @@ | ||
package de.sipgate.dachlatten.okhttp | ||
|
||
import kotlinx.serialization.ExperimentalSerializationApi | ||
import kotlinx.serialization.Serializable | ||
import kotlinx.serialization.json.Json | ||
import mockwebserver3.MockResponse | ||
import mockwebserver3.MockWebServer | ||
import mockwebserver3.junit5.internal.MockWebServerExtension | ||
import okhttp3.MediaType.Companion.toMediaType | ||
import org.junit.jupiter.api.Assertions.assertEquals | ||
import org.junit.jupiter.api.BeforeEach | ||
import org.junit.jupiter.api.Test | ||
import org.junit.jupiter.api.extension.ExtendWith | ||
import retrofit2.Call | ||
import retrofit2.Retrofit | ||
import retrofit2.http.Body | ||
import retrofit2.http.GET | ||
import retrofit2.http.POST | ||
|
||
@OptIn(ExperimentalSerializationApi::class) | ||
@ExtendWith(MockWebServerExtension::class) | ||
class KotlinSerializationConverterFactoryStringTest { | ||
private lateinit var service: Service | ||
|
||
interface Service { | ||
@GET("/") | ||
fun deserialize(): Call<User> | ||
@POST("/") | ||
fun serialize(@Body user: User): Call<Void?> | ||
} | ||
|
||
@Serializable | ||
data class User(val name: String) | ||
|
||
@BeforeEach | ||
fun setUp(server: MockWebServer) { | ||
val contentType = "application/json; charset=utf-8".toMediaType() | ||
val retrofit = Retrofit.Builder() | ||
.baseUrl(server.url("/")) | ||
.addConverterFactory(Json.asConverterFactory(contentType)) | ||
.build() | ||
service = retrofit.create(Service::class.java) | ||
} | ||
|
||
@Test | ||
fun deserialize(server: MockWebServer) { | ||
server.enqueue(MockResponse().newBuilder().body("""{"name":"Bob"}""").build()) | ||
val user = service.deserialize().execute().body()!! | ||
assertEquals(User("Bob"), user) | ||
} | ||
|
||
@Test | ||
fun serialize(server: MockWebServer) { | ||
server.enqueue(MockResponse()) | ||
service.serialize(User("Bob")).execute() | ||
val request = server.takeRequest() | ||
assertEquals("""{"name":"Bob"}""", request.body.readUtf8()) | ||
assertEquals("application/json; charset=utf-8", request.headers["Content-Type"]) | ||
} | ||
} |
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