Skip to main content
Winter Boot provides first-class support for mapping JSON and XML payloads to strongly typed PHP objects using PHP 8 attributes. Annotate your model properties with #[JsonProperty] or the PAXB attributes (#[XmlElement], #[XmlAttribute], and friends), then let ObjectCreator or your REST controller deserialize the incoming data automatically. No boilerplate mapping code is required — the framework inspects attribute metadata at runtime and populates each field, including nested objects and typed lists. The same DTO class can carry both JSON and XML annotations, so a single object handles either format depending on the request’s Content-Type header.

JSON Serialization with #[JsonProperty]

Apply #[JsonProperty] to any property you want to bind from a JSON payload. The framework maps the external JSON key to the PHP property and performs type coercion automatically.
src/Model/Product.php

#[JsonProperty] Options

string | array
default:"property name"
The external JSON key name(s) to bind. Accepts a single string or an array of aliases — the first matching alias in the payload wins.
bool
default:"false"
When true, an InvalidSyntaxException is thrown if the key is absent from the payload. Automatically set to true when the property has no default value and its type does not allow null.
bool
default:"false"
Allows null values to be set on the property. Automatically set to true for nullable types (?string, etc.).
string
default:"\"\""
Fully qualified class name for the items when the property is a typed array. Each array element in the JSON payload is deserialized into the given class.
array
default:"[]"
An array of validator names or [validatorName, ...options] tuples applied to the property value after binding.

Explicit Deserialization with ObjectCreator

Use ObjectCreator::createObject() to deserialize an associative array (e.g. the result of json_decode(..., true)) into a typed object:

Automatic Binding in REST Controllers

When a controller method parameter is annotated with #[RequestBody] and the request carries Content-Type: application/json, the framework calls ObjectCreator::createObject() for you:
src/Controller/ProductController.php

XML Serialization with PAXB Attributes

Winter Boot’s PAXB (PHP Architecture for XML Binding) mirrors Java’s JAXB, providing a set of attributes that describe how a class maps to an XML document. Annotate your class with #[XmlRootElement] and its properties with the appropriate XML attributes.
src/Model/Product.php
The class above maps to the following XML document:

XML Attribute Reference

string
default:"class short name"
The XML root element tag name. Place this attribute on the class itself.

Additional XML Attributes

Applied to a property, #[XmlAnyElement] captures any child elements not matched by other bindings. When lax is true, unknown elements are silently skipped rather than raising an error.
Applied to a property, #[XmlAnyAttribute] captures any XML attributes not matched by other bindings into an array on the property.
Applied to a class, #[XmlPropertyOrder] controls the serialisation order of child elements. The order parameter lists property names in the desired output order. Set ignoreUnknown to true to silently drop unrecognised elements during deserialization.

Explicit XML Deserialization

Use ObjectCreator::createObjectXml() to parse an XML string directly into a typed object:
When you need finer control over parser properties, use XmlObjectMapper directly:

Complete Example — JSON + XML Model

The same model class can carry both JSON and XML annotations, letting a single DTO deserialize from either format based on the incoming Content-Type.
src/Model/Product.php
Declare both content types in your controller’s consumes list and the framework handles the rest:
src/Controller/ProductController.php
#[JsonProperty] and #[XmlElement] can coexist on the same property. The framework selects the correct deserializer based on the request’s Content-Type header, so you only need one DTO class to serve both formats.