Skip to content

Commit d729dc3

Browse files
mlocatidunglas
andauthored
feat: register DateTimeNormalizer by default (#119)
* Add a note about DateTimeNormalizer in README Without it, serializing DateTimeInterface instances leads to huge serialized data. * Register the DateTimeNormalizer in the Symfony bundle * minor tweaks --------- Co-authored-by: Kévin Dunglas <[email protected]>
1 parent c73ccf7 commit d729dc3

File tree

3 files changed

+22
-14
lines changed

3 files changed

+22
-14
lines changed

README.md

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,14 @@ use Dunglas\DoctrineJsonOdm\Serializer;
4040
use Dunglas\DoctrineJsonOdm\Type\JsonDocumentType;
4141
use Symfony\Component\Serializer\Encoder\JsonEncoder;
4242
use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer;
43+
use Symfony\Component\Serializer\Normalizer\BackedEnumNormalizer;
44+
use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
4345
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
4446

4547
if (!Type::hasType('json_document')) {
4648
Type::addType('json_document', JsonDocumentType::class);
4749
Type::getType('json_document')->setSerializer(
48-
new Serializer([new ArrayDenormalizer(), new ObjectNormalizer()], [new JsonEncoder()])
50+
new Serializer([new BackedEnumNormalizer(), new DateTimeNormalizer(), new ArrayDenormalizer(), new ObjectNormalizer()], [new JsonEncoder()])
4951
);
5052
}
5153

@@ -214,7 +216,6 @@ For using the built-in type mapper:
214216
);
215217
```
216218

217-
218219
### Limitations when updating nested properties
219220

220221
Due to how Doctrine works, it will not detect changes to nested objects or properties.
@@ -291,21 +292,23 @@ class Kernel extends BaseKernel
291292

292293
**How can I add additional normalizers?**
293294

294-
The Symfony Serializer is easily extensible. This bundle registers and uses a service with ID `dunglas_doctrine_json_odm.serializer` as the serializer for the JSON type. This means we can easily override it in our `services.yaml` to use additional normalizers.
295-
As an example we use the Symfony `DateTimeNormalizer` service, so we do have support for any property that is an instance of `\DateTimeInterface`. Be aware that the order of the normalizers might be relevant depending on the normalizers you use.
295+
The Symfony Serializer is easily extensible. This bundle registers and uses a service with ID `dunglas_doctrine_json_odm.serializer` as the serializer for the JSON type.
296+
This means we can easily override it in our `services.yaml` to use additional normalizers.
297+
As an example we inject a custom normalizer service. Be aware that the order of the normalizers might be relevant depending on the normalizers you use.
296298

297299
```yaml
298300
# Add DateTime Normalizer to Dunglas' Doctrine JSON ODM Bundle
299301
dunglas_doctrine_json_odm.serializer:
300302
class: Dunglas\DoctrineJsonOdm\Serializer
301303
arguments:
302-
- ['@dunglas_doctrine_json_odm.normalizer.array', '@serializer.normalizer.datetime', '@dunglas_doctrine_json_odm.normalizer.object']
304+
- ['@App\MyCustom\Normalizer', '@?dunglas_doctrine_json_odm.normalizer.backed_enum', '@dunglas_doctrine_json_odm.normalizer.datetime', '@dunglas_doctrine_json_odm.normalizer.array', '@dunglas_doctrine_json_odm.normalizer.object']
303305
- ['@serializer.encoder.json']
306+
- '@?dunglas_doctrine_json_odm.type_mapper'
304307
public: true
308+
autowire: false
309+
autoconfigure: false
305310
```
306311

307-
As a side note: If you happen to use [Autowiring](https://symfony.com/doc/current/service_container/autowiring.html) in your `services.yaml` you might need to set `autowire: false` too. Same goes for `autoconfigure: false` in case you're using [Autoconfiguration](https://symfony.com/doc/current/service_container.html#the-autoconfigure-option).
308-
309312
**When the namespace of a used entity changes**
310313

311314
For classes without [type aliases](#using-type-aliases), because we store the `#type` along with the data in the database, you have to migrate the already existing data in your database to reflect the new namespace.
@@ -321,7 +324,7 @@ WHERE 'AppBundle\\\Entity\\\Bar' = JSON_EXTRACT(misc, '$."#type"');
321324

322325
## Credits
323326

324-
This bundle is brought to you by [Kévin Dunglas](https://dunglas.fr) and [awesome contributors](https://github.com/dunglas/doctrine-json-odm/graphs/contributors).
327+
This bundle is brought to you by [Kévin Dunglas](https://dunglas.dev) and [awesome contributors](https://github.com/dunglas/doctrine-json-odm/graphs/contributors).
325328
Sponsored by [Les-Tilleuls.coop](https://les-tilleuls.coop).
326329

327330
## Former Maintainers

src/Bundle/Resources/config/services.xml

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,15 @@
55
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
66

77
<services>
8+
9+
<service id="dunglas_doctrine_json_odm.normalizer.backed_enum" class="Symfony\Component\Serializer\Normalizer\BackedEnumNormalizer" public="false" />
10+
11+
<service id="dunglas_doctrine_json_odm.normalizer.datetime" class="Symfony\Component\Serializer\Normalizer\DateTimeNormalizer" public="false" />
12+
13+
<service id="dunglas_doctrine_json_odm.normalizer.array" class="Symfony\Component\Serializer\Normalizer\ArrayDenormalizer" public="false" />
14+
15+
<service id="dunglas_doctrine_json_odm.type_mapper" class="Dunglas\DoctrineJsonOdm\TypeMapper" public="false" />
16+
817
<service id="dunglas_doctrine_json_odm.normalizer.object" class="Symfony\Component\Serializer\Normalizer\ObjectNormalizer" public="false">
918
<argument type="service" id="serializer.mapping.class_metadata_factory" on-invalid="ignore" />
1019
<argument>null</argument><!-- name converter -->
@@ -13,15 +22,10 @@
1322
<argument type="service" id="serializer.mapping.class_discriminator_resolver" on-invalid="ignore" />
1423
</service>
1524

16-
<service id="dunglas_doctrine_json_odm.normalizer.array" class="Symfony\Component\Serializer\Normalizer\ArrayDenormalizer" public="false" />
17-
18-
<service id="dunglas_doctrine_json_odm.normalizer.backed_enum" class="Symfony\Component\Serializer\Normalizer\BackedEnumNormalizer" public="false" />
19-
20-
<service id="dunglas_doctrine_json_odm.type_mapper" class="Dunglas\DoctrineJsonOdm\TypeMapper" public="false" />
21-
2225
<service id="dunglas_doctrine_json_odm.serializer" class="Dunglas\DoctrineJsonOdm\Serializer" public="true">
2326
<argument type="collection">
2427
<argument type="service" id="dunglas_doctrine_json_odm.normalizer.backed_enum" on-invalid="ignore" />
28+
<argument type="service" id="dunglas_doctrine_json_odm.normalizer.datetime" />
2529
<argument type="service" id="dunglas_doctrine_json_odm.normalizer.array" />
2630
<argument type="service" id="dunglas_doctrine_json_odm.normalizer.object" />
2731
</argument>

tests/Fixtures/AppKernel.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ protected function configureContainer(ContainerBuilder $container, LoaderInterfa
4646
$container->loadFromExtension('framework', [
4747
'secret' => 'jsonodm',
4848
'test' => null,
49+
'http_method_override' => false,
4950
]);
5051

5152
$container->loadFromExtension('doctrine', [

0 commit comments

Comments
 (0)