mj-zana / serialization
Object normalization, hydration and JSON serialization for Zana applications
v2.37.0
2026-11-20 20:29 UTC
Requires
- php: ^8.5
Requires (Dev)
- mj-zana/core: ^2.0
- mj-zana/http: ^2.10
- phpstan/phpstan: ^2.1
- phpunit/phpunit: ^11.5
Suggests
- mj-zana/core: To register the Serializer with SerializationServiceProvider
- mj-zana/http: To answer a DenormalizationException with a 422 and its field errors
Provides
None
Conflicts
None
Replaces
None
- dev-main
- v2.37.0
- v2.36.0
- v2.35.0
- v2.34.0
- v2.33.0
- v2.32.0
- v2.31.0
- v2.30.0
- v2.29.0
- v2.28.0
- v2.27.0
- v2.26.0
- v2.25.0
- v2.24.0
- v2.23.0
- v2.22.0
- v2.21.0
- v2.20.0
- v2.19.0
- v2.18.0
- v2.17.0
- v2.16.0
- v2.15.0
- v2.14.0
- v2.13.0
- v2.12.0
- v2.11.0
- v2.10.0
- v2.9.0
- v2.8.0
- v2.7.0
- v2.6.0
- v2.5.0
- v2.4.0
- v2.3.0
- v2.2.0
- v2.1.0
- v2.0.0
- v2.0.0-rc.1
- v2.0.0-beta.3
- v2.0.0-beta.2
- v2.0.0-beta.1
- v2.0.0-alpha.3
This package is auto-updated.
Last update: 2026-11-24 16:56:15 UTC
README
Convert objects to arrays and JSON, and hydrate typed objects from arrays and JSON through their constructors. No dependencies. Requires PHP 8.5 or later.
composer require mj-zana/serialization
Usage
use Zana\Serialization\Serializer; $serializer = new Serializer(); $json = $serializer->serialize($patient); // object → JSON $patient = $serializer->deserialize($json, Patient::class); // JSON → object $array = $serializer->normalize($patient); // object → array $dto = $serializer->denormalize($request->getParsedBody(), RegisterPatient::class);
Normalizing uses public properties, enum values, ATOM dates and
JsonSerializable. Hydrating fills constructor parameters by name:
- numeric strings become ints and floats, and
"true"/"false"become booleans; - enums are read from their value, or their name for unit enums;
- date strings become
DateTimeImmutable; - nested classes are hydrated recursively.
Attributes:
#[SerializedName('height_cm')]renames a field;#[Ignore]leaves a property out;#[ListOf(Item::class)]hydrates each element of an array.
Every problem is reported at once:
try { $serializer->denormalize($input, RegisterPatient::class); } catch (DenormalizationException $e) { $e->errors(); // ['name' => 'This field is missing.', 'items[1].quantity' => 'This value should be of type int.'] }
License
MIT — see LICENSE.