Free & open source — no account required

v1.2.5-PRICING-19
Web & Frontend • Engineering Documentation

Kotlin DTO Generation Tool

This technical guide provides an in-depth analysis of the json to kotlin dto engine, best practices for implementation, and data security standards.

JSON to Kotlin Data Class: Generating DTOs for Android and Spring Boot

Kotlin's data class is the idiomatic type for holding structured data: it auto-generates equals(), hashCode(), toString(), and copy() from the constructor parameters. When your app consumes a JSON API, generating the data class from a sample payload gives you the field declarations and correct nullability instantly — ready to plug into Retrofit, Spring Boot's @RequestBody, or kotlinx.serialization.

Live Example: User Profile API Response

// Input JSON (API response)
{
  "user_id": "usr_4421",
  "display_name": "Kenji Tanaka",
  "email": "kenji@example.com",
  "follower_count": 1840,
  "is_verified": true,
  "avatar_url": "https://cdn.example.com/avatars/4421.jpg",
  "bio": null
}

// Generated Kotlin Data Class
data class Root(
    val user_id: String,
    val display_name: String,
    val email: String,
    val follower_count: Double,
    val is_verified: Boolean,
    val avatar_url: String,
    val bio: String?,
)

Refining the Generated Class: kotlinx.serialization

The generator produces the field structure. For production, add serialization annotations and rename fields to idiomatic camelCase:

import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@Serializable
data class UserProfile(
    @SerialName("user_id")      val userId: String,
    @SerialName("display_name") val displayName: String,
    val email: String,
    @SerialName("follower_count") val followerCount: Int,   // Int not Double for counts
    @SerialName("is_verified")  val isVerified: Boolean,
    @SerialName("avatar_url")   val avatarUrl: String,
    val bio: String? = null,    // default null for optional fields
)

// Deserialize from JSON string
val json = Json { ignoreUnknownKeys = true }
val profile: UserProfile = json.decodeFromString(jsonString)

// Serialize to JSON
val jsonString: String = json.encodeToString(profile)

Using with Retrofit (Android)

// build.gradle.kts
dependencies {
    implementation("com.squareup.retrofit2:retrofit:2.11.0")
    implementation("com.jakewharton.retrofit:retrofit2-kotlinx-serialization-converter:1.0.0")
    implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.7.0")
}

// ApiService.kt
import retrofit2.http.GET
import retrofit2.http.Path

interface UserApiService {
    @GET("users/{id}/profile")
    suspend fun getUserProfile(@Path("id") userId: String): UserProfile
}

// In a ViewModel
class ProfileViewModel(private val api: UserApiService) : ViewModel() {
    val profile = MutableStateFlow<UserProfile?>(null)

    fun loadProfile(userId: String) = viewModelScope.launch {
        profile.value = api.getUserProfile(userId)
    }
}

Using with Spring Boot (@RequestBody)

// build.gradle.kts (Spring Boot)
dependencies {
    implementation("org.springframework.boot:spring-boot-starter-web")
    implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
}

// UserController.kt
import org.springframework.web.bind.annotation.*

data class CreateUserRequest(
    val displayName: String,
    val email: String,
    val bio: String? = null,
)

data class UserResponse(
    val userId: String,
    val displayName: String,
    val email: String,
)

@RestController
@RequestMapping("/api/users")
class UserController(private val userService: UserService) {

    @PostMapping
    fun createUser(@RequestBody request: CreateUserRequest): UserResponse {
        return userService.create(request)
    }
}

data class vs Regular class

  • data class: Generates equals()/hashCode() based on constructor properties, toString() with field values, and copy() for immutable updates. Ideal for DTOs and value objects. All constructor params must be val or var.
  • Regular class: No auto-generated methods. More control over behavior, mutable state. Use for service classes, repositories, and objects with complex business logic.
  • Type correction: The generator uses Double for all JSON numbers. Change count/quantity fields (followerCount, age, rank) to Int. Use Long for Unix timestamps.

Frequently Asked Questions

Why does the generator use Double for integer fields? JSON numbers are untyped — the generator conservatively uses Double to avoid truncation. For fields you know are whole numbers, replace with Int or Long after generation. kotlinx.serialization will throw if the JSON has 1840 and you declare the field as Int with Gson, but kotlinx.serialization handles the coercion correctly.

Should I use kotlinx.serialization or Gson/Moshi? kotlinx.serialization is the Kotlin-native choice: it's compile-time safe, works with multiplatform (KMP), and has no reflection overhead. Gson requires no annotations but uses reflection and has Kotlin null-safety gotchas. Moshi is a middle ground — reflection or codegen, Kotlin-aware. For new Android projects, prefer kotlinx.serialization.

Can I use data classes with Room (SQLite)? Yes, with @Entity. Add @Entity(tableName = "users") above the class and @PrimaryKey on the ID field. Note that Room requires either a primary constructor with @ColumnInfo on each param, or secondary constructor ignoring non-stored fields.

Is my JSON sent to a server? No. TypeMorph runs entirely in your browser — none of your data leaves your machine.

Developer FAQ

Is the processing local-only?

Absolutely. TypeMorph operates entirely within your browser's sandbox. We use Web Workers for high-performance computation without ever transmitting your JSON, SQL, or API data to a remote server.

Can I use this for enterprise projects?

Yes. The tool is designed for professional software engineers who require GDPR compliance and data privacy. It is trusted by developers at top-tier startups and financial institutions.