jsonSchema

inline fun <T : Any> jsonSchema(): Schema

Returns a JSON Schema representation of the specified type T.

This function analyzes the serialization metadata of the given @Serializable class T and produces a corresponding Schema. The returned schema describes the structure and constraints of T in terms of JSON Schema types.

Example:

@Serializable
data class Person(
val name: String,
val age: Double?,
val emails: List<String> = emptyList(),
)

val schema = jsonSchema<Person>()
println(schema.jsonObject)

Output:

{
"type": "object",
"properties": {
"name": {
"type": "string"
},
"age": {
"type": "number"
},
"emails": {
"type": "array",
"items": {
"type": "string"
}
}
},
"required": ["name"]
}

Here, name is required because it is non-nullable and has no default value, while age is optional (because it is nullable) and so is 'emails' (because it has a default value).

T must be annotated with @Serializable. Otherwise, this function will fail at runtime.