JMS Serializer - Convert objects to and from JSON, XML, YAML, etc

September 27 2015, 3:53pm

Say you're building an API, you need to convert the objects you send in the response to JSON representation (removing certain properties like passwords, and modifying others like dates) you can use JSMSerializer to define how that happens automatically. But where JSMSerializer really shines is how it can deserialize data into PHP objects. Maybe you you are consuming a JSON feed, you can use this to convert each object in the JSON feed to an object. You do this by registering "handlers" for each object type. You can do all this configuration via annotations, XML or YAML (which you'll be familiar with if you've used Doctrine). It also includes an event system, which you can use to hook into modifying the data before serialization or right afterward it happens.