Ktor Server ↔ Ktor Client: Content Negotiation Interoperability
0 reputation · 19 Aug 2020, 23:09 UTC
0 reputation · 19 Aug 2020, 23:09 UTC
When a Ktor Server and a Ktor Client are deployed together, both sides must agree on how JSON payloads are serialized. The current ContentNegotiation plug‑in allows separate registration of kotlinx.serialization or gson, but the API does not provide a single, unified configuration path. This fragmentation creates uncertainty for libraries that need a stable contract for JSON handling, especially when the server uses kotlinx.serialization while the client relies on gson.
Given that the Ktor community has not yet decided whether to merge these plug‑ins into a single json(...) call that accepts a serializer strategy, developers face a design dilemma: choose a specific serializer per component or risk runtime incompatibilities.
What are the trade‑offs of introducing a unified JSON serializer API? How will backward compatibility be maintained for existing projects that register separate plug‑ins? Will the new API enforce identical serializer configurations across server and client to guarantee round‑trip correctness?
33275 reputation · 20 Aug 2020, 05:44 UTC
As of current Ktor releases, there is no single unified json(...) call that abstracts over kotlinx.serialization and Gson. ContentNegotiation remains a per-engine plugin on server and client. A unified serializer API is discussed in the community but not implemented, so any claims about its design are speculative and marked for review.
Content-Type and Accept headers.@Serializable on data classes, is type-safe and multiplatform. Gson relies on reflection and does not require annotations.ignoreUnknownKeys = true is set. Naming strategies, null handling and default values must be matched manually.A unified JSON serializer API would reduce boilerplate and make cross-team contracts easier to document. The likely cost is an abstraction layer that must normalize serializer-specific options. That risks loss of fine-grained control, added indirection, and harder debugging when one engine needs an option the other does not support. Backward compatibility would most plausibly be preserved by keeping existing install(ContentNegotiation) { json(...) } registrations per engine and adding a new additive helper, not replacing the current API.
Enforcing identical serializer configurations across server and client cannot be guaranteed by Ktor. Ktor runs in separate processes; it can only negotiate media type. Round-trip correctness depends on the application ensuring the same JSON schema, naming policy, null handling and unknown-key policy on both sides.
Accept: application/json from client matches Content-Type: application/json from server.One missing diagnostic detail that changes the recommendation: which exact serializer and naming policy is currently configured on each side, server vs client. Without that, specific configuration parity cannot be prescribed.
Uncertainty note: The existence and design of a future unified API, and any official backward-compatibility plan, is not confirmed in released documentation and should be verified against current Ktor release notes before planning.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.