symfony/serializer
Symfony Serializer component for converting objects and complex data structures to/from arrays, JSON, XML and more. Supports object graphs, custom normalizers/encoders, and flexible context options for reliable serialization and deserialization.
symfony/serializer package is a battle-tested, Symfony-backed component designed for Laravel’s PHP ecosystem. It aligns with Laravel’s object-relational patterns, API payload handling, and form request validation needs.@Groups) allow fine-grained control over serialization logic, critical for complex Laravel applications (e.g., polymorphic relationships, nested resources).spatie/laravel-api-resources): Replace manual toArray() with typed serialization.Denormalizer.Validator (if used) or Laravel’s Illuminate\Validation.symfony/property-info, symfony/yaml), reducing bloat.@Groups). Mitigate via:
spatie/laravel-serializer).json_encode()/json_decode() is needed, the package may be overkill. Tradeoff: Gains scalability for complex scenarios.spatie/laravel-arrayable: Replace with Serializer for typed normalization.nesbot/carbon: Use DateTimeNormalizer for timezone handling.@Groups for API, Denormalizer for forms).password)?" → Use ObjectNormalizer with ignoredAttributes.cache_normalizer_metadata: true) and encoder selection (e.g., JsonEncoder vs. XmlEncoder).toArray() methods) to identify reusable normalizers vs. replacement candidates.SerializerTestCase or custom assertions.ObjectNormalizer over forking).Response::json($model->toArray()) with Serializer::serialize($model, 'json', ['groups' => ['api']]).json_decode() + validation with Serializer::deserialize($request->json(), Model::class, 'json').FormRequest or Symfony’s Validator for typed deserialization.Serializer::serialize().@Groups.| Current Implementation | Serializer Equivalent | Migration Steps |
|---|---|---|
Manual toArray() methods |
ObjectNormalizer + @Groups |
Extract logic into a custom normalizer; annotate model properties with @Groups. |
json_encode($model) |
Serializer::serialize($model, 'json') |
Replace direct json_encode calls; add use Symfony\Component\Serializer\Serializer. |
| Form request binding | Denormalizer::deserialize() |
Replace json_decode() + manual instantiation with typed deserialization. |
| API resource transformations | Serializer + JsonEncoder |
Replace Arrayable with Serializer-aware resources. |
| Custom JSON/XML mapping | Attribute metadata (e.g., @SerializedName) |
Migrate from manual arrays to typed objects with annotations. |
v6.4.x of the serializer.symfony/property-info). Use composer why symfony/property-info to resolve.spatie/laravel-arrayable or laravel/serializable-closures (deprecate the latter).php-xml for XML support (enabled by default in Laravel’s php.ini).Phase 1: Proof of Concept (1–2 Sprints)
toArray() method with ObjectNormalizer.Phase 2: Core Integration (2–3 Sprints)
UserNormalizer).FormRequest for typed deserialization.@Groups for API versioning (e.g., v1, v2).Phase 3: Expansion (Ongoing)
Attribute metadata.Phase 4: Deprecation (Optional)
toArray() methods in favor of @Groups.toArray() methods with declarative annotations (@Groups, @SerializedName).How can I help you explore Laravel packages today?