{"id":16198139,"url":"https://github.com/xinthink/auto-data-class","last_synced_at":"2025-03-19T05:30:33.045Z","repository":{"id":145719068,"uuid":"95734920","full_name":"xinthink/auto-data-class","owner":"xinthink","description":"Generator for Kotlin data classes with Gson/Parcelable type adapter ","archived":false,"fork":false,"pushed_at":"2019-11-28T04:07:18.000Z","size":391,"stargazers_count":15,"open_issues_count":3,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-02-28T16:01:02.258Z","etag":null,"topics":["android","annotation-processing","data-class","kapt","kotlin"],"latest_commit_sha":null,"homepage":"","language":"Kotlin","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/xinthink.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":null,"patreon":null,"open_collective":null,"ko_fi":null,"tidelift":null,"community_bridge":null,"liberapay":null,"issuehunt":null,"otechie":null,"custom":"paypal.me/xinthink/5"}},"created_at":"2017-06-29T03:28:26.000Z","updated_at":"2022-03-17T18:41:53.000Z","dependencies_parsed_at":null,"dependency_job_id":"32c2ba07-056f-4583-ab20-73f41d8ea529","html_url":"https://github.com/xinthink/auto-data-class","commit_stats":null,"previous_names":[],"tags_count":11,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xinthink%2Fauto-data-class","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xinthink%2Fauto-data-class/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xinthink%2Fauto-data-class/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xinthink%2Fauto-data-class/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/xinthink","download_url":"https://codeload.github.com/xinthink/auto-data-class/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243971172,"owners_count":20376784,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["android","annotation-processing","data-class","kapt","kotlin"],"created_at":"2024-10-10T09:11:41.063Z","updated_at":"2025-03-19T05:30:33.039Z","avatar_url":"https://github.com/xinthink.png","language":"Kotlin","funding_links":["paypal.me/xinthink/5"],"categories":[],"sub_categories":[],"readme":"[![Build Status](https://travis-ci.org/xinthink/auto-data-class.svg?branch=master)](https://travis-ci.org/xinthink/auto-data-class)\n[ ![Download](https://api.bintray.com/packages/xinthink/maven/auto-data-class-processor/images/download.svg) ](https://bintray.com/xinthink/maven/auto-data-class-processor/_latestVersion)\n\n# auto-data-class\nAn annotation processor generates [Kotlin Data Classes] and the boilerplates for Parcelable \u0026 GSON TypeAdapter. Inspired by [AutoValue] and its popular extensions [auto-value-parcel] \u0026 [auto-value-gson].\n\n## Usage\nDeclare your data model as an interface/abstract class, and annotate it with `@DataClass`\n\n```kotlin\n@DataClass interface Address : Parcelable {\n    val street: String?\n    val city: String\n}\n```\n\nSince version `0.6.0`, you can make things even simpler with [`@Parcelize`][kt-1.14-release-note]. You can now define data classes directly, to make the most out of Data Class, and leave the `Parcelable` stuff to the Kotlin compiler. The `@DataClass` processor will just generate a Gson `TypeAdapter` for such a class.\n\n```kotlin\n@Parcelize @DataClass data class Address(\n    val street: String?,\n    val city: String\n) : Parcelable\n```\n\nNow build the project, a data class will be generated, with all the boilerplates needed to implement `Parcelable` \u0026 Gson `TypeAdapter`.\n\n```kotlin\ninternal data class DC_Address(\n    override val street: String?,\n    override val city: String\n) : Address {\n\n    override fun writeToParcel(dest: Parcel, flags: Int)\n    ...\n\n    class GsonTypeAdapter(gson: Gson) : TypeAdapter\u003cAddress\u003e()\n    ...\n    companion object {\n        val CREATOR: Parcelable.Creator\u003cDC_Address\u003e\n        ...\n    }\n}\n```\n\nJust like how you'll use [AutoValue], it's convenient to write factory methods or derived properties to access the generated code.\n\n```kotlin\n@DataClass interface Address {\n...\n    /** derived properties */\n    val fullAddress: String\n        get() = if (street != null) \"$street, $city\" else city\n\n    companion object {\n        /** factory method */\n        fun create(street: String?, city: String): Address = DC_Address(street, city)\n\n        /** Gson TypeAdapter factory method */\n        fun typeAdapter(gson: Gson): TypeAdapter\u003cAddress\u003e =\n            DC_Address.GsonTypeAdapter(gson)\n                .apply {\n                    // if needed, you can set default values for the omission of the json fields\n                    defaultCity = \"Beijing\"\n                    defaultStreet = \"Unknown\"\n                }\n    }\n}\n```\n\nFurthermore, you can customize the generated code with the `@DataProp` annotation.\n\n```kotlin\n@DataClass interface Address {\n    @get:DataProp(\"street\",\n        jsonFieldAlternate = arrayOf(\"street1\", \"street2\"),\n        defaultValueLiteral = \"\"\"\"string literal\"\"\"\"\n    )\n    val street: String?\n    ...\n}\n```\n\nA `TypeAdapterFactory` can also be generated, which can be used to setup the `Gson` instance. All you have to do is annotating an object/interface/class with `@GsonTypeAdapterFactory`.\n\n```kotlin\n// Using objects, you can also use interfaces or abstract classes.\n@GsonTypeAdapterFactory object MyTypeAdapterFactory {\n    fun create(): TypeAdapterFactory = DC_MyTypeAdapterFactory()\n}\n```\n\nSo that you can build a `Gson` instance like this:\n\n```kotlin\nGsonBuilder()\n    .registerTypeAdapterFactory(MyTypeAdapterFactory.create())\n    .create()\n```\n\nSee the [test cases][example-tests] for more details.\n\n## Integration\nUsing the [kotlin-kapt] plugin\n\n```gradle\nkapt 'com.fivemiles.auto:auto-data-class-processor:0.7.0'\ncompile 'com.fivemiles.auto:auto-data-class-lib:0.7.0'\n\n# for testing, optional\nkaptTest 'com.fivemiles.auto:auto-data-class-processor:0.7.0'\nkaptAndroidTest 'com.fivemiles.auto:auto-data-class-processor:0.7.0'\n```\n\n## Developing auto-data-class\nIf you forked this repo and made some changes, you can test them locally before subimit a PR, by [Publishing to Maven Local]:\n\n    ./gradlew publishToMavenLocal\n\n## Limitations\nThe lib is still at its early stage, there's some limitations you should know.\n\n### Unsupported Data Types\nThe following data types are not supported:\n- `kotlin.Array`, please consider using `List` or `Set` instead\n- Nullable type parameters, such as `List\u003cString?\u003e`\n\nNot all parcelable data types are supported, for example, `android.util.SparseArray`, `android.os.Bundle` has no built-in support for now, please use `ParcelAdapter` if these unsupported types are mandatory. You can also shoot me a PR, of cause. :beer:\n\nSee this [test case][example-parcel-types] for more details.\n\n### Overriding Built-in Methods\nIf you're using interfaces or abstract classes, because of the nature of Kotlin [interface][Kotlin Interfaces] and [data class][Kotlin Data Classes], it will be a little difficult to override built-in methods such as `toString`, `hashCode`.\n\nThere's several ways to handle such a situation.\n\nFirst, avoid using `Parcelable` if it's not required or resort to `@Parcelize`, so that you can define data classes directly, in which method overriding will not be a problem.\n\nOtherwise, prefer [extension functions][Kotlin Extensions] whenever possible.\n\n```kotlin\n@DataClass interface Address {\n...\n    companion object {\n        fun Address.alternateToString(): String = ...\n    }\n}\n```\n\nOr you will have to create a wrapper class which overrides the built-in methods, and [delegates][Kotlin Delegation] all the others to the generated data class.\n\n```kotlin\n/** The data class definition, internal usage only. */\n@DataClass internal interface PersonInternal {\n    val name: String\n    val age: Int\n}\n\n/** The public wrapper of the data class, in which you can rewrite the built-in methods. */\nclass Person\nprivate constructor(p: PersonInternal) : PersonInternal by p {\n\n    override fun toString(): String {\n        return \"My name is $name, I'm $age years old.\"\n    }\n\n    companion object {\n        fun create(name: String, age: Int): Person = Person(DC_PersonInternal(name, age))\n    }\n}\n```\n\nSee this [test case][example-overriding] for more details.\n\n## License\n\n    Copyright 2017 yingxinwu.g@gmail.com.\n\n    Licensed under the Apache License, Version 2.0 (the \"License\");\n    you may not use this file except in compliance with the License.\n    You may obtain a copy of the License at\n\n       http://www.apache.org/licenses/LICENSE-2.0\n\n    Unless required by applicable law or agreed to in writing, software\n    distributed under the License is distributed on an \"AS IS\" BASIS,\n    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n    See the License for the specific language governing permissions and\n    limitations under the License.\n\n[Kotlin Data Classes]: https://kotlinlang.org/docs/reference/data-classes.html\n[Kotlin Interfaces]: https://kotlinlang.org/docs/reference/interfaces.html\n[Kotlin Delegation]: https://kotlinlang.org/docs/reference/delegation.html\n[Kotlin Extensions]: https://kotlinlang.org/docs/reference/extensions.html\n[kotlin-kapt]: https://kotlinlang.org/docs/reference/kapt.html\n[AutoValue]: https://github.com/google/auto\n[auto-value-parcel]: https://github.com/rharter/auto-value-parcel\n[auto-value-gson]: https://github.com/rharter/auto-value-gson\n[example-tests]: https://github.com/xinthink/auto-data-class/tree/master/example/src/test/java/com/fivemiles/auto/dataclass\n[example-parcel-types]: https://github.com/xinthink/auto-data-class/blob/master/example/src/test/java/com/fivemiles/auto/dataclass/parcel/ParcelableTypesTest.kt\n[example-overriding]: https://github.com/xinthink/auto-data-class/blob/master/example/src/test/java/com/fivemiles/auto/dataclass/OverridingTest.kt\n[kt-1.14-release-note]: https://blog.jetbrains.com/kotlin/2017/08/kotlin-1-1-4-is-out/\n[Publishing to Maven Local]: https://docs.gradle.org/current/userguide/publishing_maven.html#publishing_maven:install\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxinthink%2Fauto-data-class","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fxinthink%2Fauto-data-class","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxinthink%2Fauto-data-class/lists"}