On this page ⌄
Work with coded values
Set and read FHIR coded elements with the generated enums, Enumeration, and ExtensibleEnumeration.
Goal and scope
This page shows how to set and read elements that hold a code, such as Patient.gender. Examples use R4.
Only elements of the FHIR type code use enums. Elements of type CodeableConcept or Coding, such as Observation.code, keep those types. The element's binding strength decides which Kotlin type it has.
| Binding strength | Kotlin type | Example |
|---|---|---|
required | Enumeration<T> | Patient.gender is Enumeration<AdministrativeGender> |
extensible or preferred | ExtensibleEnumeration<T> | Expression.language is ExtensibleEnumeration<ExpressionLanguage> |
example | Code |
How the model maps FHIR explains these rules.
Find the enum for a code
All enums are in the terminologies package of their FHIR version, for example dev.ohs.fhir.model.r4.terminologies.AdministrativeGender. Each constant has a code, system, and display.
To get a constant from a code, use fromCode(). It throws IllegalArgumentException if the code is not in the ValueSet.
import dev.ohs.fhir.model.r4.terminologies.AdministrativeGender
val female = AdministrativeGender.fromCode("female")
female.code // "female"
female.system // "http://hl7.org/fhir/administrative-gender"
female.display // "Female"
Constant names are made from the codes, so they are not always obvious. For example, text/fhirpath becomes Text_Fhirpath and >= becomes GreaterThanOrEqualTo. Use fromCode() when you know the code, or see the enum naming rules in the repository.
Set and read a required code
Wrap the enum constant in Enumeration to set it, and read value to get it.
import dev.ohs.fhir.model.r4.Enumeration
import dev.ohs.fhir.model.r4.Patient
import dev.ohs.fhir.model.r4.terminologies.AdministrativeGender
val patient = Patient(gender = Enumeration(value = AdministrativeGender.Female))
val gender: AdministrativeGender? = patient.gender?.value
If JSON contains a code that is not in the ValueSet, decoding throws IllegalArgumentException. See Handle decoding failures.
Set and read an extensible code
An ExtensibleEnumeration<T> is one of two classes.
Predefinedholds an enum constant, for a code in the ValueSet.Customholds the code as a string, for a code not in the ValueSet.
Both have a code property with the code as a string. Use ExtensibleEnumeration.of() to create either one.
import dev.ohs.fhir.model.r4.ExtensibleEnumeration
import dev.ohs.fhir.model.r4.terminologies.ExpressionLanguage
val fhirPath = ExtensibleEnumeration.of(ExpressionLanguage.Text_Fhirpath) // Predefined
val inHouse = ExtensibleEnumeration.of("text/x-in-house-rules") // Custom
fun describe(language: ExtensibleEnumeration<ExpressionLanguage>): String =
when (language) {
is ExtensibleEnumeration.Predefined -> "Known language ${language.value.display}"
is ExtensibleEnumeration.Custom -> "Other language ${language.code}"
}
When decoding JSON, a code in the ValueSet becomes Predefined and any other code becomes Custom.
Exceptions to the rules
A few ValueSets do not follow the table above. For example, ImplementationGuide.license has a required binding but accepts new SPDX license codes, and the mimetypes and all-languages ValueSets have no enum because they have no fixed list of codes. The repository lists every exception in Excluded ValueSets and Required bindings generated as extensible.
Next step
Read Bundles and server responses shows how to handle the JSON a FHIR server returns.