https://github.com/commit451/frenchie
Additional classes and extensions for Moshi
https://github.com/commit451/frenchie
json kotlin moshi
Last synced: 5 months ago
JSON representation
Additional classes and extensions for Moshi
- Host: GitHub
- URL: https://github.com/commit451/frenchie
- Owner: Commit451
- License: apache-2.0
- Created: 2017-07-18T22:38:56.000Z (over 8 years ago)
- Default Branch: main
- Last Pushed: 2022-06-27T04:33:04.000Z (over 3 years ago)
- Last Synced: 2025-04-26T02:52:37.500Z (11 months ago)
- Topics: json, kotlin, moshi
- Language: Kotlin
- Size: 84 KB
- Stars: 6
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# frenchie
Additional classes and extensions for [Moshi](https://github.com/square/moshi)
[](https://travis-ci.org/Commit451/frenchie) [](https://jitpack.io/#Commit451/frenchie)
## Gradle Dependency
Add the jitpack url to the project:
```groovy
allprojects {
repositories {
...
maven { url "https://jitpack.io" }
}
}
```
then, in your app `build.gradle`
```groovy
dependencies {
implementation "com.github.Commit451:frenchie:latest.version.here"
}
```
## Usage
The purpose of frenchie is to extend [Moshi](https://github.com/square/moshi) with classes and utilities that are useful, but may not make sense being included in Moshi itself.
### ObjectOrArrayAdapterFactory
Sometimes, APIs are less than great, and they will include JSON that is not so predictable, and can either be a single element, or an array of elements. For example:
```json
{
"cats": {
"name": "Sputnik"
}
}
```
and also:
```json
{
"cats": [{
"name": "Sputnik"
}]
}
```
This is a challenge to deal with with JSON parsing libraries. To overcome this, simply annotate your model that can have this field of either a list or object like so:
```kotlin
class CatResponse {
@field:ObjectOrArray
lateinit var cats: List
}
```
and when you are creating your `Moshi` instance:
```kotlin
Moshi.Builder()
.add(ObjectOrArrayAdapterFactory())
.build()
```
## Name
frenchie is named after the breed of the dog that [Moshi itself was named after](https://twitter.com/MoshiFrenchie), a French bulldog
License
--------
Copyright 2018 Commit 451
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.