diff --git a/doc/configuration.rst b/doc/configuration.rst index 3e1106f82..325f8217f 100644 --- a/doc/configuration.rst +++ b/doc/configuration.rst @@ -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() ; @@ -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()``. diff --git a/doc/cookbook/exclusion_strategies.rst b/doc/cookbook/exclusion_strategies.rst index 742d3f955..42e874c83 100644 --- a/doc/cookbook/exclusion_strategies.rst +++ b/doc/cookbook/exclusion_strategies.rst @@ -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 @@ -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. @@ -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 @@ -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 @@ -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);