Skip to content

Commit 3e021f2

Browse files
committed
[#1085] Allow using serializer groups with the Symfony serializer, update docs
1 parent 2d22776 commit 3e021f2

File tree

2 files changed

+48
-22
lines changed

2 files changed

+48
-22
lines changed

Resources/doc/serializer.md

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,17 @@ it means types do not have to be mapped.
88
A) Install and declare the serializer
99
-------------------------------------
1010

11-
Follow the installation instructions for [JMSSerializerBundle](http://jmsyst.com/bundles/JMSSerializerBundle).
11+
Both the [Symfony Serializer](http://symfony.com/doc/current/components/serializer.html) and
12+
[JMSSerializerBundle](http://jmsyst.com/bundles/JMSSerializerBundle) are supported.
1213

13-
Enable the serializer configuration for the bundle:
14+
If you want to override the default serializer service you can do so in the configuration:
1415

1516
```yaml
1617
#app/config/config.yml
1718
fos_elastica:
1819
serializer: ~
1920
```
2021
21-
The default configuration that comes with FOSElasticaBundle supports both the JMS Serializer
22-
and the Symfony Serializer. If JMSSerializerBundle is installed, additional support for
23-
serialization groups, versions and null value serialization are added to the bundle. Example:
24-
25-
```yaml
26-
fos_elastica:
27-
serializer:
28-
groups: [elastica, Default]
29-
version: '1.1'
30-
serialize_null: true
31-
```
32-
3322
B) Set up each defined type to support serialization
3423
----------------------------------------------------
3524
@@ -43,5 +32,37 @@ fos_elastica:
4332
types:
4433
user:
4534
serializer:
46-
groups: [elastica, Default]
35+
groups: [elastica]
36+
```
37+
38+
And inside the User class:
39+
40+
```php
41+
use Symfony\Component\Serializer\Annotation\Groups;
42+
43+
class User {
44+
45+
/**
46+
* @Groups({"elastica"})
47+
*
48+
* @var string
49+
*/
50+
private $username;
51+
52+
}
4753
```
54+
55+
In addition the JMS Serializer allows you to specify options for version and whether to serialize null
56+
57+
```yaml
58+
fos_elastica:
59+
60+
indexes:
61+
app:
62+
types:
63+
user:
64+
serializer:
65+
groups: [elastica]
66+
version: '1.1'
67+
serialize_null: true
68+
```

Serializer/Callback.php

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
namespace FOS\ElasticaBundle\Serializer;
44

55
use JMS\Serializer\SerializationContext;
6-
use JMS\Serializer\SerializerInterface;
6+
use Symfony\Component\Serializer\SerializerInterface;
7+
use JMS\Serializer\SerializerInterface as JMSSerializer;
78

89
class Callback
910
{
@@ -30,8 +31,8 @@ public function setGroups(array $groups)
3031
{
3132
$this->groups = $groups;
3233

33-
if (!empty($this->groups) && !$this->serializer instanceof SerializerInterface) {
34-
throw new \RuntimeException('Setting serialization groups requires using "JMS\Serializer\Serializer".');
34+
if (!empty($this->groups) && !$this->serializer instanceof SerializerInterface && !$this->serializer instanceof JMSSerializer) {
35+
throw new \RuntimeException('Setting serialization groups requires using "JMS\Serializer\Serializer" or "Symfony\Component\Serializer\Serializer"');
3536
}
3637
}
3738

@@ -42,7 +43,7 @@ public function setVersion($version)
4243
{
4344
$this->version = $version;
4445

45-
if ($this->version && !$this->serializer instanceof SerializerInterface) {
46+
if ($this->version && !$this->serializer instanceof JMSSerializer) {
4647
throw new \RuntimeException('Setting serialization version requires using "JMS\Serializer\Serializer".');
4748
}
4849
}
@@ -54,7 +55,7 @@ public function setSerializeNull($serializeNull)
5455
{
5556
$this->serializeNull = $serializeNull;
5657

57-
if (true === $this->serializeNull && !$this->serializer instanceof SerializerInterface) {
58+
if (true === $this->serializeNull && !$this->serializer instanceof JMSSerializer) {
5859
throw new \RuntimeException('Setting null value serialization option requires using "JMS\Serializer\Serializer".');
5960
}
6061
}
@@ -66,10 +67,14 @@ public function setSerializeNull($serializeNull)
6667
*/
6768
public function serialize($object)
6869
{
69-
$context = $this->serializer instanceof SerializerInterface ? SerializationContext::create()->enableMaxDepthChecks() : array();
70+
$context = $this->serializer instanceof JMSSerializer ? SerializationContext::create()->enableMaxDepthChecks() : array();
7071

7172
if (!empty($this->groups)) {
72-
$context->setGroups($this->groups);
73+
if ($context instanceof SerializationContext) {
74+
$context->setGroups($this->groups);
75+
} else {
76+
$context['groups'] = $this->groups;
77+
}
7378
}
7479

7580
if ($this->version) {

0 commit comments

Comments
 (0)