Table of contents
Generate JSON schemas at compile time with zero runtime overhead using the kotlinx-schema KSP processor.
Configure the KSP processor directly in your Gradle build script, Maven pom.xml, or use the dedicated Gradle plugin.
Add the Google KSP plugin and processor dependency to your project.
plugins {
kotlin("multiplatform")
id("com.google.devtools.ksp") version "<ksp-version>"
}
dependencies {
add("kspCommonMainMetadata", "org.jetbrains.kotlinx:kotlinx-schema-ksp:<version>")
implementation("org.jetbrains.kotlinx:kotlinx-schema-annotations:<version>")
}
kotlin {
sourceSets.commonMain.kotlin.srcDir("build/generated/ksp/metadata/commonMain/kotlin")
}
// Ensure KSP runs before compilation
tasks.withType<org.jetbrains.kotlin.gradle.dsl.KotlinCompile<*>>().all {
if (name != "kspCommonMainKotlinMetadata") dependsOn("kspCommonMainKotlinMetadata")
}
ksp {
arg("kotlinx.schema.rootPackage", "com.example")
}Check out an example project.
plugins {
kotlin("jvm")
id("com.google.devtools.ksp") version "<ksp-version>"
}
dependencies {
ksp("org.jetbrains.kotlinx:kotlinx-schema-ksp:<version>")
implementation("org.jetbrains.kotlinx:kotlinx-schema-annotations:<version>")
}
sourceSets.main.kotlin.srcDir("build/generated/ksp/main/kotlin")The plugin automatically handles KSP configuration, source set registration, and task dependencies, and provides additional configuration options (DSL) for schema generation.
-
Register the plugin in
settings.gradle.kts:pluginManagement { repositories { google() mavenCentral() } resolutionStrategy { eachPlugin { if (requested.id.id == "org.jetbrains.kotlinx.schema.ksp") { useModule("org.jetbrains.kotlinx:kotlinx-schema-gradle-plugin:<version>") } } } } -
Apply the plugin and dependencies in your
build.gradle.kts:
plugins {
kotlin("multiplatform")
id("org.jetbrains.kotlinx.schema.ksp")
}
kotlin {
sourceSets.commonMain.dependencies {
implementation("org.jetbrains.kotlinx:kotlinx-schema-annotations:<version>")
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:<version>") // Required for withSchemaObject
}
}
kotlinxSchema {
rootPackage.set("com.example")
}plugins {
kotlin("jvm")
id("org.jetbrains.kotlinx.schema.ksp")
}
dependencies {
implementation("org.jetbrains.kotlinx:kotlinx-schema-annotations:<version>")
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:<version>")
}
kotlinxSchema {
rootPackage.set("com.example")
}Notes:
- You do NOT need to apply the KSP plugin yourself — the Gradle plugin does it.
- You do NOT need to add generated source directories — the plugin does it.
- For an example project, see gradle-plugin-integration-tests.
You may also run schema generation with KSP in your Maven projects.
Add the ksp-maven-plugin with the processor dependency
and include the annotations library in your project.
<plugin>
<groupId>me.kpavlov.ksp.maven</groupId>
<artifactId>ksp-maven-plugin</artifactId>
<version>0.3.0</version>
<extensions>true</extensions>
<dependencies>
<dependency>
<groupId>org.jetbrains.kotlinx</groupId>
<artifactId>kotlinx-schema-ksp</artifactId>
<version>${kotlinx-schema.version}</version>
</dependency>
</dependencies>
<configuration>
<options>
<kotlinx.schema.rootPackage>com.example</kotlinx.schema.rootPackage>
</options>
</configuration>
</plugin>
<!-- In <dependencies> -->
<dependencies>
<dependency>
<groupId>org.jetbrains.kotlinx</groupId>
<artifactId>kotlinx-schema-annotations-jvm</artifactId>
<version>${kotlinx-schema.version}</version>
</dependency>
</dependencies>
<properties>
<!-- check latest version: https://central.sonatype.com/artifact/org.jetbrains.kotlinx/kotlinx-schema-ksp -->
<kotlinx-schema.version>0.0.5</kotlinx-schema.version>
</properties>Check out an example project.
Options can be set globally in your build configuration or overridden per-class via @Schema.
| Option | Type | Default | Description |
|---|---|---|---|
enabled |
Boolean |
true |
Enable or disable schema generation. |
rootPackage |
String |
null |
Limit processing to this package and its subpackages. Improves build performance. |
withSchemaObject |
Boolean |
false |
Generate jsonSchema: JsonObject property. Requires kotlinx-serialization-json. |
visibility |
String |
"" |
Visibility modifier for generated extensions (public, internal, etc.). |
- Annotation Parameter (highest) —
@Schema(withSchemaObject = true) - KSP Argument — Global processor options (e.g.,
arg()in Gradle or<options>in Maven) - Gradle Option (Plugin only) —
kotlinxSchema { withSchemaObject.set(true) } - Default Value (lowest)
Tip
Use an empty string visibility.set("") (default) for Multiplatform projects targeting Native
to avoid "redundant visibility modifier" warnings.
For each @Schema-annotated class, the processor generates extension properties:
@Schema(withSchemaObject = true)
data class User(val name: String)Access generated extensions
val jsonString: String = User::class.jsonSchemaString
val jsonObject: JsonObject = User::class.jsonSchemaFor each @Schema-annotated function, the processor generates additional top-level or extension function:
@Schema(withSchemaObject = true)
internal fun calculateArea(shape: Shape): Double = TODO("only signature matters")Access generated functions:
val functionCallSchemaString: String = calculateAreaJsonSchemaString() // <function name> + "jsonSchemaString()"
val functionCallSchema: JsonObject = calculateAreaJsonSchema() // <function name> + "jsonSchema()" - Serialization-Based Generator — Runtime generation for any
@Serializableclass (no KSP required) - Annotation Reference —
@Schemaand@Descriptionusage - Runtime Schema Generation — Alternative using Reflection (JVM only)
- Function Calling Schemas — Generate LLM function schemas
- JSON Schema DSL — Manual schema construction