On this page ⌄
Kotlin FHIR
A lean, code-generated implementation of the FHIR data model for Kotlin Multiplatform, with JSON serialization.
What it is
Kotlin FHIR is the HL7 FHIR data model as Kotlin classes. It is the base of the FHIR Foundations stack and the KMP FHIR SDK. Every resource and data type is generated from the FHIR specification, and each class comes with a serializer that reads and writes FHIR JSON. FHIR R4, R4B, and R5 each have their own artifact.
The library has no logging, networking, XML, or platform-specific dependencies. This is why it runs on Android, iOS, JVM, JavaScript, Wasm, macOS, and Linux. Its only runtime dependencies are kotlinx-serialization, kotlinx-datetime, and the bignum arbitrary-precision number library.
What you get
- A class for every resource and data type. Choice elements such as
value[x]are sealed interfaces, so the compiler only accepts the types the specification allows. - Enums for coded elements. An element of the FHIR type
codeis typed with an enum generated from its ValueSet, such asAdministrativeGender. The binding strength decides whether other codes are allowed. Elements of typeCodeableConceptorCodingkeep those types. See Work with coded values. - Special types for dates and decimals.
FhirDateandFhirDateTimesupport partial dates such as a year only.FhirDecimalkeeps the exact digits of a decimal. - JSON serializers. They handle the parts of FHIR JSON that plain Kotlin serialization cannot, such as extensions on primitive values (the
_fieldproperties), choice elements, and decoding a resource by itsresourceType. You use a standardkotlinx.serialization.Jsoninstance. - Search parameter extraction. For each resource, a
SearchParamsobject extracts search values as plain Kotlin code, without a FHIRPath engine. Each parameter also keeps its FHIRPath expression, so you can evaluate it with Kotlin FHIRPath when the generated code does not cover it. - Tests against the official examples. On every change, about 11,000 example resources from the HL7 example packages for R4, R4B, and R5 are decoded, encoded again, and compared with the original JSON.
Artifacts
| Artifact | Contents |
|---|---|
fhir-model-r4 | FHIR R4 model, package dev.ohs.fhir.model.r4 |
fhir-model-r4b | FHIR R4B model, package dev.ohs.fhir.model.r4b |
fhir-model-r5 | FHIR R5 model, package dev.ohs.fhir.model.r5 |
fhir-model | All three versions in one artifact |
Depend only on the FHIR version you use. Use fhir-model only if your code handles more than one FHIR version.
What it is not
- Not a validator. Profiles and constraints (invariants) are not checked. Decoding fails only when the JSON does not fit the model, such as a missing required element, an unknown property, or a code outside a
requiredbinding. - Not a client or a database. It has no networking or storage. Kotlin FHIR Engine provides those.
- JSON only. XML and Turtle are not supported. Other
kotlinx.serializationformats may work, but only JSON is tested. - No profiles or logical models. Only the base specification is generated. The library has classes such as
ValueSetandCodeSystem, but it does not include the ValueSets and CodeSystems published with the specification. The FHIR patternsEvent,Request, andDefinitionare not interfaces, because resources may rename their elements, which Kotlin inheritance cannot express. - Not every search parameter is extractable. Some FHIRPath expressions are not supported by the generator, and those parameters throw
NotImplementedError. See Extract search parameters.
How this documentation is organized
| If you want to | Read |
|---|---|
| Try the library for the first time | Get started |
| Understand how FHIR becomes Kotlin | How the model maps FHIR |
| Build or change resources | Create and change resources |
| Set or read codes | Work with coded values |
| Handle data from a FHIR server | Read Bundles and server responses |
| Encode and decode JSON | Read and write FHIR JSON |
| Build a search index | Extract search parameters |
| Fix an error | Troubleshooting |
| Choose versions | Versions and compatibility |
The repository documentation has the detailed lists that change with each release, such as enum naming rules and supported search expressions.