Kest

Search

Search the documentation

Application

Serialization

Control exactly what leaves your application as JSON.

A controller usually returns an object, and Kest turns it into JSON. But the object your database holds is not always the one you want to show: it has a password hash, internal notes, fields only admins should see. Serialization is the step that decides what goes out. It is the Kest version of class-transformer, in json-jackson.

The idea: mark the output class with what to hide, rename or rewrite, and let one interceptor apply it to every response.

data class UserOut(
    val id: String,
    @Exclude val password: String,                                   // never in the JSON, in or out
    @Expose(name = "full_name") val name: String,                    // renamed in the JSON
    @Transform(MaskEmail::class) val email: String,                  // rewritten: "a***@example.com"
    @Expose(groups = [AdminGroup::class]) val internalNotes: String, // only shown for admin responses
)
 
class MaskEmail : ValueTransformer {
    override fun transform(value: Any?) = (value as String).let { it.take(1) + "***@" + it.substringAfter('@') }
}
 
@Controller("/users") @UseInterceptors(ClassSerializerInterceptor::class)
class Users {
    @Get("/me") fun me() = user                                                              // no group named: internalNotes is hidden
    @SerializeGroups(AdminGroup::class) @Get("/{id}") fun one(@Param id: String) = user      // internalNotes is shown
}

What a client gets from GET /users/me:

{ "id": "u1", "full_name": "Ann Lee", "email": "a***@example.com" }

and from the admin route GET /users/u1, additionally "internalNotes": "VIP".

AnnotationEffect
@Excludethe property never appears
@Expose(name = "...")use another name in the JSON
@Expose(groups = [G::class])the property appears only when the handler names group G
@Transform(T::class)rewrite the value with a ValueTransformer
@SerializeGroups(G::class, ...)on a handler: which groups apply to this response (several allowed)

ClassSerializerInterceptor applies it to what a handler returns. Use it on a controller, or for the whole application with useInterceptors(ClassSerializerInterceptor::class).

Why not just exclude it in the controller?

You could copy fields into a response class by hand every time. Annotations put the rule on the class, once, so a new endpoint that returns UserOut cannot forget to hide the password. Still, the safest design is a separate output class (UserOut) for responses, different from the database entity (User): a field that is not in UserOut cannot leak.

Good practice

  • Never return a database entity directly when it has sensitive fields; map to an output class.
  • Use groups for role-based views (public, owner, admin), and name the group in the handler, not by checking roles inside the transformer.
  • A transformer should be small and pure (masking, formatting). Do not call the database in it.
  • Request bodies are read by the codec and checked by validation; serialization applies to responses.

For server-rendered pages see Views (MVC); to send files and streams see Files, streaming and SSE.

Read more