Skip to content

Serialization

Daniel Carbone edited this page Feb 23, 2025 · 2 revisions

This library supports serialize resources to and from JSON and XML. Other serialization formats may be supported inthe future, but there are currently no plans on this.

Each serialization method has its own set of configurable options that may be specified per-version. See Configuration for more details.

Unserializing

Each Resource model generated by this type contains the following static methods:

class FHIRPatient extends FHIRDomainResource implements VersionContainedTypeInterface
{
    public static function jsonUnserialize(string|\stdClass $json,
                                           null|UnserializeConfig $config = null,
                                           null|ResourceTypeInterface $type = null): self
    {
        /* ... */
    }
    
    public static function xmlUnserialize(string|\SimpleXMLElement $element,
                                          null|UnserializeConfig $config = null,
                                          null|ResourceTypeInterface $type = null): self
    {
        /* ... */
    }
}

Each of the above methods is responsible for the entire construction of its parent Resource instance from the provided serialized input.

Unserialization Configuration

The class UnserializeConfig contains any / all unserialization configuration options for all supported encodings.

You do not have to create an UnserializeConfig instance as one with usually-sane defaults will be created automatically if left undefined.

Unserializing JSON

This library makes use of the standard PHP json extension for serializing resources to and from JSON.

<?php

use DCarbone\PHPFHIRGenerated\Versions\R4\Types\FHIRResource\FHIRDomainResource\FHIRPatient;

$json = <<<JSON
{
    "resourceType": "Patient",
    "name": [
        {
            "family": "Human",
            "given": [
                "Real"
            ]
        }
    ]
}
JSON;

$patient = FHIRPatient::jsonUnserialize($json);

JSON Decoding Large Numbers:

To prevent buffer overflow or mutation of data during unserialization, the JSON_BIGINT_AS_STRING flag is used by default when decoding JSON. You may change this by setting the jsonDecodeOpts field in an UnserializeConfig instance.

JSON Decoding Strings for Numeric Types:

Some FHIR servers encode all numeric types (integers and decimals) as strings rather than a typical JSON number. This library automaticaly handles this for all non-string primitive types:

  • bool
  • integer
  • integer64
  • positiveInt
  • negativeInt
  • unsignedInt

Unserializing XML

This library utilizes SimpleXML extension for decoding XML into resources, and the XMLWriter for encoding resources to XML.

<?php

use DCarbone\PHPFHIRGenerated\Versions\R4\Types\FHIRResource\FHIRDomainResource\FHIRPatient;

$xml = <<<XML
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Patient>
    <name>
        <family value="Human"/>
        <given value="Real"/>
    </name>
</Patient>
XML;

$patient = FHIRPatient::xmlUnserialize($xml);
Clone this wiki locally