FhirEx.JSON (fhir_ex v0.3.0)

Copy Markdown View Source

JSON encoding and decoding for FHIR R5 resources and data types.

Each FHIR struct module exposes to_map/1 (for encoding) and from_map/1 (for decoding). This module provides the top-level encode/decode API.

Encoding

FhirEx.JSON.encode!(%Patient{id: "p1", ...})
#=> ~s({"resourceType":"Patient","id":"p1",...})

Decoding

FhirEx.JSON.decode!(json_string, FhirEx.Resources.Patient)
#=> %Patient{...}

Summary

Functions

Converts an atom field name to FHIR camelCase JSON key string.

Recursively removes nil values from a map (or list of maps).

Decodes a JSON string into the given FHIR resource or data type module.

Encodes any FHIR struct to a JSON string, dropping nil fields.

Encodes a list of FHIR structs or maps, dropping nil fields in each.

Converts a FHIR struct to a JSON-compatible map with string keys. Delegates to the module's own to_map/1 if defined, otherwise does a best-effort field-name camelCasing from the struct.

Functions

camelize(atom)

@spec camelize(atom()) :: String.t()

Converts an atom field name to FHIR camelCase JSON key string.

compact(map)

@spec compact(any()) :: any()

Recursively removes nil values from a map (or list of maps).

decode!(json, module)

@spec decode!(String.t(), module()) :: struct()

Decodes a JSON string into the given FHIR resource or data type module.

encode!(struct)

@spec encode!(struct()) :: String.t()

Encodes any FHIR struct to a JSON string, dropping nil fields.

encode_list!(list)

@spec encode_list!([struct()]) :: String.t()

Encodes a list of FHIR structs or maps, dropping nil fields in each.

to_json_map(struct)

@spec to_json_map(any()) :: any()

Converts a FHIR struct to a JSON-compatible map with string keys. Delegates to the module's own to_map/1 if defined, otherwise does a best-effort field-name camelCasing from the struct.