Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/rharter/ktor-moshi
Ktor feature that adds Moshi JSON serialization support
https://github.com/rharter/ktor-moshi
Last synced: 7 days ago
JSON representation
Ktor feature that adds Moshi JSON serialization support
- Host: GitHub
- URL: https://github.com/rharter/ktor-moshi
- Owner: rharter
- License: apache-2.0
- Created: 2018-09-12T18:02:58.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2023-05-22T01:47:29.000Z (over 1 year ago)
- Last Synced: 2024-08-02T16:49:06.077Z (3 months ago)
- Language: Kotlin
- Size: 68.4 KB
- Stars: 82
- Watchers: 3
- Forks: 6
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# Ktor: Moshi
The Moshi feature allows you to handle JSON content in your application easily using the [Moshi](https://github.com/square/moshi/) library.
This feature provides a [ContentNegotiation](http://ktor.io/servers/features/content-negotiation.html) converter.
## Usage
Install the feature by registering a JSON content converter using Moshi:
```kotlin
install(ContentNegotiation) {
moshi {
// Configure the Moshi.Builder here.
add(Date::class.java, Rfc3339DateJsonAdapter())
}
}
```Inside the `moshi` block you have access to the [Moshi.Builder](http://square.github.io/moshi/1.x/moshi/com/squareup/moshi/Moshi.Builder.html), which you can configure as needed for your application.
If you already have an instance of Moshi you can simply provide that and it will be used, instead of creating a new one.
```kotlin
install(ContentNegotiation) {
moshi(myInjectedMoshi)
}
```Once the Moshi converter is installed you use it like you would any other ContentNegotiation converter, by using `call.respond(myObject)` and `call.receive()`.
```kotlin
routing {
get("/") {
// Simply pass an object to `call.respond` and it will be
// converted to JSON if the client accepts `application/json`
val myResponseObject = ...
call.respond(myResponseObject)
}
post("/") {
// Use `call.receive` to get the JSON request as a
// deserialized object.
val request = call.receive()
...
}
}
```## Download
Add a gradle dependency to your project:
```groovy
compile 'com.ryanharter.ktor:ktor-moshi:1.0.1'
```Snapshots of the latest development version are available in [Sonatype's `snapshots` repository](https://oss.sonatype.org/content/repositories/snapshots/).
## License
```
Copyright 2018 Ryan Harter.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 athttp://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.
```