symfony/serializer
Symfony Serializer component for converting object graphs and data structures to/from arrays and formats like JSON or XML. Supports powerful normalizers/encoders, metadata, naming and type handling—ideal for APIs, messaging, and data interchange.
@Groups for granular field control).Serializer integration remains unaffected.Normalizer methods) provide a 6–12 month window to refactor Laravel-specific normalizers (e.g., ModelNormalizer, CollectionNormalizer).Normalizer classes to use attributes (e.g., [SerializedName] instead of annotations).MetadataFactory methods with newer APIs.ObjectNormalizer::normalize())?Validator) or supplement them?json_encode())?[SerializedName], [Groups].Normalizer configuration.// Future-proof for Symfony 8.1 attributes
$this->app->singleton(SerializerInterface::class, function ($app) {
return new Serializer(
[new ObjectNormalizer(), new ModelNormalizer()],
[new JsonEncoder(), new XmlEncoder()],
[], // No annotations in 8.1; use attributes
new MetadataFactory(new CacheDirMetadataFactory(__DIR__.'/cache/serializer'))
);
});
ObjectNormalizer to handle:
attributes array via __get() magic.Denormalizer interfaces for lazy-loaded relations.DateTimeNormalizer to output ISO strings by default.composer.json to track symfony/serializer:^6.4 (LTS) or ^8.1.0-BETA3 for testing.Normalizer methods.normalize() with attribute-based logic).[Groups]).Serializer.fn() in normalizers).CollectionNormalizer may not handle Illuminate\Support\Collection by default—extend it.DateTimeNormalizer respects Laravel’s timezone settings (e.g., config('app.timezone')).CircularReferenceHandler for Eloquent relations with self-referencing tables.| Step | Task | Dependencies | Risk | Symfony Version |
|---|---|---|---|---|
| 1 | Add symfony/serializer:^6.4 to composer.json |
None | Low | 6.4 LTS |
| 2 | Register Serializer in Laravel container |
Step 1 | Low | 6.4 |
| 3 | Replace manual json_encode() in pilot endpoints |
Step 2 | Medium | 6.4 |
| 4 | Implement ModelNormalizer for Eloquent |
Step 3 | High | 6.4 |
| 5 | Run deprecation audit with PHPStan | Step 4 | Medium | 6.4/8.1 |
| 6 | Update custom normalizers for Symfony 8.1 attributes | Step 5 | High | 8.1 |
| 7 | Test form request deserialization with new error handling | Step 6 | Medium | 8.1 |
| 8 | Deprecate legacy serialization in favor of Serializer |
Step 7 | Medium | 8.1 |
| 9 | Update CI to enforce Symfony 8.1 | Step 8 | Low | 8.1 |
[Groups] may need IDE updates (e.g., PHPStorm).Normalizer methods early.How can I help you explore Laravel packages today?