Skip to content

Commit

Permalink
documented the new way of creating contextes
Browse files Browse the repository at this point in the history
  • Loading branch information
goetas committed Dec 5, 2016
1 parent 2f4f860 commit c93b09e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 26 deletions.
19 changes: 13 additions & 6 deletions doc/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,21 @@ you can set a ``SerializationContextFactory`` to the Serializer.

Example using the SerializerBuilder::

class MySerializationContextFactory extends DefaultSerializationContextFactory
{
public function createSerializationContext()
{
$context = parent::createSerializationContext();
$context->setSerializeNull(true);

return $context;
}
}

use JMS\Serializer\SerializationContext;

$serializer = JMS\Serializer\SerializerBuilder::create()
->setSerializationContextFactory(function () {
return SerializationContext::create()
->setSerializeNull(true)
;
})
->setSerializationContextFactory(new MySerializationContextFactory())
->build()
;

Expand All @@ -96,5 +103,5 @@ a serialization context from your callable and use it.
.. note ::
You can also set a default DeserializationContextFactory with
``->setDeserializationContextFactory(function () { /* ... */ })``
``->setDeserializationContextFactory()``
to be used with methods ``deserialize()`` and ``fromArray()``.
39 changes: 19 additions & 20 deletions doc/cookbook/exclusion_strategies.rst
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,10 @@ versions like this::

use JMS\Serializer\SerializationContext;

$serializer->serialize(new VersionObject(), 'json', SerializationContext::create()->setVersion(1));
$context = $serializer->getSerializationContextFactory()->createSerializationContext();
$context->setVersion(1);

$serializer->serialize(new VersionObject(), 'json', $context);


Creating Different Views of Your Objects
Expand Down Expand Up @@ -116,11 +119,16 @@ You can then tell the serializer which groups to serialize in your controller::

use JMS\Serializer\SerializationContext;

$serializer->serialize(new BlogPost(), 'json', SerializationContext::create()->setGroups(array('list')));
//will output $id, $title and $nbComments.
$context = $serializer->getSerializationContextFactory()->createSerializationContext();
$context->setGroups(array('list'));

$serializer->serialize(new BlogPost(), 'json', $context);

$serializer->serialize(new BlogPost(), 'json', SerializationContext::create()->setGroups(array('Default', 'list')));
$context = $serializer->getSerializationContextFactory()->createSerializationContext();
$context->setGroups(array('Default', 'list'));

//will output $id, $title and $nbComments.
$serializer->serialize(new BlogPost(), 'json', $context);
//will output $id, $title, $nbComments and $createdAt.

Expand Down Expand Up @@ -177,8 +185,9 @@ And the following object graph::
You can override groups on specific paths::

use JMS\Serializer\SerializationContext;
$context = $serializer->getSerializationContextFactory()->createSerializationContext();

$context = SerializationContext::create()->setGroups(array(
$context->setGroups(array(
'Default', // Serialize John's name
'manager_group', // Serialize John's manager
'friends_group', // Serialize John's friends
Expand Down Expand Up @@ -263,7 +272,10 @@ You need to tell the serializer to take into account MaxDepth checks::

use JMS\Serializer\SerializationContext;

$serializer->serialize($data, 'json', SerializationContext::create()->enableMaxDepthChecks());
$context = $serializer->getSerializationContextFactory()->createSerializationContext();
$context->enableMaxDepthChecks();

$serializer->serialize($data, 'json', $context);


Dynamic exclusion strategy
Expand Down Expand Up @@ -295,17 +307,4 @@ allow a more sophisticated exclusion strategies using `@Exclude(if="expression")
``true`` is just a generic expression, you can use any expression allowed by the Symfony Expression Language
If you have annotated your objects like above, you can serializing different
versions like this::

use JMS\Serializer\SerializationContext;

$context = SerializationContext::create();

$language = new ExpressionLanguage();

$context = new SerializationContext();
$context->addExclusionStrategy(new ExpressionLanguageExclusionStrategy($language));

$serializer->serialize(new MyObject(), 'json', $context);

0 comments on commit c93b09e

Please sign in to comment.