Search by

mj-zana / serialization

mjnable

Object normalization, hydration and JSON serialization for Zana applications

Package info

github.com/mjnable/zana-serialization

pkg:composer/mj-zana/serialization

Statistics

Installs: 12

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

v2.1.0 2026-10-25 20:32 UTC

This package is auto-updated.

Last update: 2026-10-25 20:43:33 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.