{"id":21312082,"url":"https://github.com/sunlulu427/autoconverter","last_synced_at":"2026-02-13T09:52:40.849Z","repository":{"id":214791033,"uuid":"736638257","full_name":"sunlulu427/AutoConverter","owner":"sunlulu427","description":"auto conversion between json and data class","archived":false,"fork":false,"pushed_at":"2024-10-31T14:09:48.000Z","size":167,"stargazers_count":1,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"develop","last_synced_at":"2025-03-28T08:11:49.790Z","etag":null,"topics":["json","kcp","kotlin-poet","ksp"],"latest_commit_sha":null,"homepage":"","language":"Kotlin","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sunlulu427.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"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}},"created_at":"2023-12-28T12:58:20.000Z","updated_at":"2024-10-22T14:27:43.000Z","dependencies_parsed_at":"2024-04-14T13:32:09.885Z","dependency_job_id":"3901b358-0af3-49ba-8491-2a44f48f15d2","html_url":"https://github.com/sunlulu427/AutoConverter","commit_stats":null,"previous_names":["sunlulu427/stg4cpp","sunlulu427/autoconverter"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sunlulu427%2FAutoConverter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sunlulu427%2FAutoConverter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sunlulu427%2FAutoConverter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sunlulu427%2FAutoConverter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sunlulu427","download_url":"https://codeload.github.com/sunlulu427/AutoConverter/tar.gz/refs/heads/develop","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248951848,"owners_count":21188419,"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":["json","kcp","kotlin-poet","ksp"],"created_at":"2024-11-21T17:23:56.964Z","updated_at":"2026-02-13T09:52:40.790Z","avatar_url":"https://github.com/sunlulu427.png","language":"Kotlin","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)\n# AutoConverter\n\nAutoConverter is a simple tool to convert data classes to JSONObject. \nIt can be used in **Android log entity conversion**, or in any other use case where you need to convert a data class to a JSONObject.\n\nI planned two versions, one is the **ksp** version, and the other is **kcp and IDE plugin** version.\n\n- [x] serialization: toJSONObject\n- [x] deserialization: fromJSONObject, it is defined as a extended function for KClass\n- [x] nested classes\n- [x] naming strategy for properties, e.g. camel to underline\n- [x] code style config, e.g. indent\n- [x] fields marked nullable\n- [ ] conversion config: JSONArray/List, JSONObject/Map\n- [ ] kcp and idea plugin version \n- [ ] publish to maven central\n\n## KSP\n \n### Usage\n\n#### The `AutoConvert` definition\n```kotlin\n/**\n * Auto convert annotation\n\n * @property functions enabled auto convert functions, toJSONObject by default\n * @property namingStrategy naming strategy, None by default\n * @property filePostfix file postfix, Ext by default, filename is ClassName + filePostfix\n * @constructor Create empty Auto convert\n */\n@MustBeDocumented\n@Target(AnnotationTarget.CLASS)\n@Retention(AnnotationRetention.BINARY)\nannotation class AutoConvert(\n    val functions: Array\u003cACFunction\u003e = [ACFunction.ToJSONObject],\n    val namingStrategy: NamingStrategy = NamingStrategy.None,\n    val filePostfix: String = \"Ext\"\n)\n```\n\n#### Add `AutoConvert` to your classes\n```kotlin\npackage com.mato.stg4cpp\n\nimport com.mato.stg4.annotation.ACFunction\nimport com.mato.stg4.annotation.AutoConvert\nimport com.mato.stg4cpp.pkg2.Location\n\n@AutoConvert(functions = [ACFunction.FromJSONObject, ACFunction.ToJSONObject])\ndata class Restaurant(\n    val score: Float,\n    val comments: Int = 0,\n    val location: Location? = null,\n    val foods: List\u003cString\u003e = emptyList(),\n)\n```\n```kotlin\n// Location.kt, it's a nested class, too. And it has different package name with the outer.\npackage com.mato.stg4cpp.pkg2\n\nimport com.mato.stg4.annotation.ACFunction\nimport com.mato.stg4.annotation.AutoConvert\n\n@AutoConvert(functions = [ACFunction.ToJSONObject, ACFunction.FromJSONObject])\ndata class Location(\n    val longitude: Double = 0.0,\n    val latitude: Double = 0.0,\n    val address: String = \"\"\n)\n```\n\n#### The generate files:\n- `LocationExt.kt`\n```kotlin\n// Read-only, generated by AutoConverter.\npackage com.mato.stg4cpp.pkg2\n\nimport kotlin.Result\nimport kotlin.reflect.KClass\nimport org.json.JSONObject\n\npublic fun Location.toJSONObject(): Result\u003cJSONObject\u003e = kotlin.runCatching {\n    JSONObject().also {\n        it.put(\"longitude\", this.longitude)\n        it.put(\"latitude\", this.latitude)\n        it.put(\"address\", this.address)\n    }\n}\n\npublic fun KClass\u003cLocation\u003e.fromJSONObject(payload: JSONObject): Result\u003cLocation\u003e =\n    kotlin.runCatching {\n        Location(\n            longitude = payload.get(\"longitude\") as Double,\n            latitude = payload.get(\"latitude\") as Double,\n            address = payload.get(\"address\") as String\n        )\n    }\n}\n```\n- `RestaurantExt.kt`\n```kotlin\n// Read-only, generated by AutoConverter.\npackage com.mato.stg4cpp\n\nimport com.mato.stg4cpp.pkg2.Location\nimport com.mato.stg4cpp.pkg2.fromJSONObject\nimport com.mato.stg4cpp.pkg2.toJSONObject\nimport kotlin.Result\nimport kotlin.reflect.KClass\nimport org.json.JSONObject\n\npublic fun KClass\u003cRestaurant\u003e.fromJSONObject(payload: JSONObject): Result\u003cRestaurant\u003e =\n    kotlin.runCatching {\n        Restaurant(\n            score = payload.get(\"score\") as Float,\n            comments = payload.get(\"comments\") as Int,\n            location = payload.optJSONObject(\"location\")?.let {\n                Location::class.fromJSONObject(it).getOrThrow() },\n            foods=payload.get(\"foods\") as List\u003cString\u003e\n        )\n    }\n\npublic fun Restaurant.toJSONObject(): Result\u003cJSONObject\u003e = kotlin.runCatching {\n    JSONObject().also {\n        it.put(\"score\", this.score)\n        it.put(\"comments\", this.comments)\n        it.put(\"location\", this.location?.toJSONObject()?.getOrThrow())\n        it.put(\"foods\", this.foods)\n    }\n}\n```\n\n## [WIP] KCP","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsunlulu427%2Fautoconverter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsunlulu427%2Fautoconverter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsunlulu427%2Fautoconverter/lists"}