All Projects → ansman → Kotshi

ansman / Kotshi

Licence: apache-2.0
An annotation processor that generates Moshi adapters from immutable Kotlin data classes.

Programming Languages

kotlin
9241 projects

Projects that are alternatives of or similar to Kotshi

WinAnalytics
A light-weight android library that can be quickly integrated into any app to use analytics tools.
Stars: ✭ 23 (-96.49%)
Mutual labels:  annotation-processor
Vscode Todo Highlight
a vscode extension to highlighting todos, fixmes, and any annotations...
Stars: ✭ 305 (-53.51%)
Mutual labels:  annotation-processor
Mapstruct
An annotation processor for generating type-safe bean mappers
Stars: ✭ 4,710 (+617.99%)
Mutual labels:  annotation-processor
therapi-runtime-javadoc
Read Javadoc comments at run time.
Stars: ✭ 50 (-92.38%)
Mutual labels:  annotation-processor
Immutables
Annotation processor to create immutable objects and builders. Feels like Guava's immutable collections but for regular value objects. JSON, Jackson, Gson, JAX-RS integrations included
Stars: ✭ 3,031 (+362.04%)
Mutual labels:  annotation-processor
Pojobuilder
A Java Code Generator for Pojo Builders
Stars: ✭ 326 (-50.3%)
Mutual labels:  annotation-processor
dagger2-ktx
Kotlin extension bridge library for Dagger2 (proof-of-concept)
Stars: ✭ 41 (-93.75%)
Mutual labels:  annotation-processor
Paperparcel
Auto-generate the fastest possible Parcelable implementations for Java and Kotlin
Stars: ✭ 494 (-24.7%)
Mutual labels:  annotation-processor
Blade
Android library for boilerplate destruction
Stars: ✭ 278 (-57.62%)
Mutual labels:  annotation-processor
Android Api Securekeys
Store data in a simple and secure way
Stars: ✭ 372 (-43.29%)
Mutual labels:  annotation-processor
Dexter
Manage multidexing using simple annotations and gradle tasks.
Stars: ✭ 41 (-93.75%)
Mutual labels:  annotation-processor
Doma
DAO oriented database mapping framework for Java 8+
Stars: ✭ 257 (-60.82%)
Mutual labels:  annotation-processor
Preferenceroom
🚚 Android processing library for managing SharedPreferences persistence efficiently and structurally.
Stars: ✭ 341 (-48.02%)
Mutual labels:  annotation-processor
kotlin-cursor
Kotlin Annotation Processor to generate fromCursor and toContentValues of data classes.
Stars: ✭ 34 (-94.82%)
Mutual labels:  annotation-processor
Deeplinkdispatch
A simple, annotation-based library for making deep link handling better on Android
Stars: ✭ 4,108 (+526.22%)
Mutual labels:  annotation-processor
simple-annotation-processor
Simple annotation processor example. Inspired by the idea of "How ButterKnife works?"
Stars: ✭ 54 (-91.77%)
Mutual labels:  annotation-processor
Jackdaw
Java Annotation Processor which allows to simplify development
Stars: ✭ 306 (-53.35%)
Mutual labels:  annotation-processor
Derive4j
Java 8 annotation processor and framework for deriving algebraic data types constructors, pattern-matching, folds, optics and typeclasses.
Stars: ✭ 511 (-22.1%)
Mutual labels:  annotation-processor
Onactivityresult
OnActivityResult annotation compiler for Android
Stars: ✭ 470 (-28.35%)
Mutual labels:  annotation-processor
Parceler
📦 Android Parcelables made easy through code generation.
Stars: ✭ 3,589 (+447.1%)
Mutual labels:  annotation-processor

Kotshi Build status

An annotation processor that generates Moshi adapters from Kotlin classes.

There is a reflective adapter for Kotlin but that requires the kotlin reflection library which adds a lot of methods and increase the binary size which in a constrained environment such as Android is something is not preferable.

This is where Kotshi comes in, it generates fast and optimized adapters for your Kotlin data classes, just as if you'd hand written them yourself. It will automatically regenerate the adapters when you modify your class.

It's made to work with minimal setup, through there are limitations. Most of the limitations will be addressed as the support for Kotlin annotation processors improves.

Usage

First you must annotate your objects with the @JsonSerializable annotation:

@JsonSerializable
data class Person(
    val name: String,
    val email: String?,
    val hasVerifiedAccount: Boolean,
    // This property has a different name in the Json than here so @Json must be applied.
    @Json(name = "created_at")
    val signUpDate: Date,
    // This field has a default value which will be used if the field is missing.
    val jobTitle: String? = null
)

The following types are supported:

  • object (serialized as an empty JSON object)
  • data class
  • enum class
  • sealed class

Then create a class that will be your factory:

@KotshiJsonAdapterFactory
object ApplicationJsonAdapterFactory : JsonAdapter.Factory by KotshiApplicationJsonAdapterFactory

Lastly just add the factory to your Moshi instance and you're all set:

val moshi = Moshi.Builder()
    .add(ApplicationJsonAdapterFactory.INSTANCE)
    .build()

By default adapters aren't requested for primitive types (even boxed primitive types) since it is worse for performance and most people will not have custom adapters anyway. If you need to use custom adapters you can enable it per module be passing the useAdaptersForPrimitives to @KotshiJsonAdapterFactory or on a per adapter by passing the same argument to @JsonSerializable (the default is to follow the module wide setting).

Annotations

  • @JsonSerializable is the annotation used to generate JsonAdapter's. Should only be placed on data classes, enums, sealed classes and objects.
  • @KotshiJsonAdapterFactory makes Kotshi generate a JsonAdapter factory. Should be placed on an abstract class that implements JsonAdapter.Factory.
  • @JsonDefaultValue can be used to annotated a fallback for enums or sealed classes when an unknown entry is encountered. The default is to thrown an exception.

Default Values

You can use default values just like you normally would in Kotlin.

Due to limitations in Kotlin two instances of the object will be created when a class uses default values (youtrack issue). This also means that composite default values are not supported (for example a fullName property that is "$firstName $lastName").

For enum entries and sealed classes you may annotate a single type with @JsonDefaultValue to indicate that the entry should be used when an unknown value is encountered (by default an exception is thrown).

Transient Values

Properties marked with @Transient are not serialized. All transient properties must have a default value.

Only properties declared in the constructor needs to be annotated since other properties are ignores.

Custom Names

By default the property or enum entry name is used when reading and writing JSON. To change the name used you may use the regular @Json annotation from Moshi to annotate the property or enum entry.

Json Qualifiers

Kotshi has full support for @JsonQualifier, both plain and those with arguments. Simply annotate a property with the desired qualifiers and Kotshi will pick them up.

Limitations

  • Kotshi only processes files written in Kotlin, types written in Java are not supported.
  • Only data classes, enums, sealed classes and objects are supported.
    • Only constructor properties will be serialized.
    • Qualifiers whose arguments are named as a Java keyword cannot be seen by annotations processors and cannot be used.
  • Due to limitation in KAPT, properties with a java keyword as a name cannot be marked as transient.
  • Default values that depend on other constructor properties is not supported (youtrack issue).
  • Due to a KAPT bug/limitation you cannot add qualifiers to parameters that are inline classes (youtrack issue).

Download

implementation "se.ansman.kotshi:api:2.4.0"
kapt "se.ansman.kotshi:compiler:2.4.0"

Snapshots of the development version are available in jfrogs's snapshots repository.

License

Copyright 2017-2020 Nicklas Ansman Giertz.

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.
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].