Symfony Serializer

Converts PHP objects to JSON, XML, CSV, and YAML and back again

Library
Composer
vv7.4.16
2,536stars
MIT License

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum.How we score it →
87/100Excellent
Development Activity96
Maintenance96
Community56
Maturity60
Momentum40

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation.How we score it →
82/100Excellent
Architecture88
Code Quality87
Innovation82
Learning Curve72

Symfony Serializer handles the two-way transformation between PHP object graphs and array/string representations like JSON, XML, CSV, and YAML. It splits the job into normalizers (object <-> array) and encoders (array <-> string format), so each concern can be swapped or extended independently — you can write a custom normalizer for a specific class without touching how JSON gets encoded.

Beyond basic (de)serialization, it supports attribute-based configuration (#[Groups], #[SerializedName], #[Context]), circular reference handling, denormalization into typed objects using constructor promotion and property types, and integrates with Symfony’s Validator and PropertyInfo components for schema-aware mapping. It’s the serialization layer used throughout Symfony’s HttpFoundation/HttpKernel stack for API request/response bodies.

What You Get

  • A pluggable Serializer combining normalizers (object <-> array) and encoders (array <-> string format: JSON, XML, CSV, YAML)
  • PHP attributes for per-property control: #[Groups], #[SerializedName], #[SerializedPath], #[Ignore], #[Context]
  • ObjectNormalizer that uses reflection plus PropertyInfo/PropertyAccess to (de)normalize arbitrary classes without manual mapping
  • Built-in normalizers for common types: DateTimeNormalizer, DateIntervalNormalizer, BackedEnumNormalizer, UidNormalizer, ConstraintViolationListNormalizer
  • Circular reference and object-graph depth handling to avoid infinite loops when serializing nested/related entities

Common Use Cases

  • Converting API request bodies (JSON) into typed DTOs/entities and validating them, and converting response entities back to JSON
  • Exporting application data to CSV or YAML for reports, imports, or configuration files
  • Building framework-agnostic API layers that need consistent, attribute-driven control over which fields serialize under which “group” (e.g. public vs. admin views)
  • Denormalizing third-party API responses into internal PHP value objects with strict typing

Under The Hood

Architecture - The Serializer class (413 lines) is a thin coordinator: it iterates a configured list of normalizer/encoder services, asking each supportsNormalization()/supportsEncoding() in turn and delegating to the first match. Heavy lifting lives in AbstractObjectNormalizer, which uses symfony/property-info and reflection to map object properties to array keys and back, and in the Encoder/ directory (JsonEncoder, XmlEncoder, CsvEncoder, YamlEncoder) which handle only format-level concerns.

Tech Stack - Pure PHP 8.4+ core with a required-only dependency on deprecation-contracts and ctype polyfill; nearly two dozen optional dev/peer integrations (Validator, PropertyAccess, PropertyInfo, TypeInfo, Cache, Console, DependencyInjection, Messenger, Uid, VarExporter) light up extra normalizers and Symfony DI wiring when present.

Code Quality - Extensive test suite under Tests/ with dedicated test classes per normalizer/encoder plus integration tests like DeserializeNestedArrayOfObjectsTest; the Attribute/ directory shows disciplined use of typed PHP attributes rather than annotations. Exception classes are specific (NotEncodableValueException, CircularReferenceException, ExtraAttributesException) enabling precise error handling by callers.

API Design - The public API centers on two calls, serialize()/deserialize(), with behavior tuned via a $context array and attributes rather than constructor arguments, keeping the surface small while still exposing deep configurability (groups, circular reference handlers, max depth) for complex object graphs.

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers

Search