Skip to content

Commit

Permalink
Merge pull request #273 from jockri/replace_data
Browse files Browse the repository at this point in the history
Allow Post Serialize Event to overwrite existing data
  • Loading branch information
goetas authored Sep 9, 2016
2 parents b359a1b + 6314d9f commit 447c7d3
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
18 changes: 15 additions & 3 deletions src/JMS/Serializer/GenericSerializationVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,8 @@ public function visitProperty(PropertyMetadata $metadata, $data, Context $contex
* Allows you to add additional data to the current object/root element.
*
* @param string $key
* @param integer|float|boolean|string|array $value This value must either be a regular scalar, or an array.
* It must not contain any objects anymore.
* @param integer|float|boolean|string|array|null $value This value must either be a regular scalar, or an array.
* It must not contain any objects anymore.
*/
public function addData($key, $value)
{
Expand All @@ -174,7 +174,7 @@ public function addData($key, $value)

$this->data[$key] = $value;
}

/**
* Checks if some data key exists.
*
Expand All @@ -186,6 +186,18 @@ public function hasData($key)
return isset($this->data[$key]);
}

/**
* Allows you to replace existing data on the current object/root element.
*
* @param string $key
* @param integer|float|boolean|string|array|null $value This value must either be a regular scalar, or an array.
* It must not contain any objects anymore.
*/
public function replaceData($key, $value)

This comment has been minimized.

Copy link
@schmittjoh

schmittjoh Sep 10, 2016

Owner

This sounds as if it would require the data to actually exist, but since it does not throw an exception, a better name is probably just setData and also to deprecate the addData method.

The limitation in addData was only useful as long as it was enforced throughout the class to allow streaming output (which I never implemented), but as soon as there is a method that can overwrite data, I have no use case for addData anymore.

{
$this->data[$key] = $value;
}

public function getRoot()
{
return $this->root;
Expand Down
30 changes: 30 additions & 0 deletions tests/JMS/Serializer/Tests/Serializer/JsonSerializationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,21 @@ function(VisitorInterface $visitor, AuthorList $data, array $type, Context $cont
$this->assertEquals('[{"full_name":"foo","_links":{"details":"http:\/\/foo.bar\/details\/foo","comments":"http:\/\/foo.bar\/details\/foo\/comments"}},{"full_name":"bar","_links":{"details":"http:\/\/foo.bar\/details\/bar","comments":"http:\/\/foo.bar\/details\/bar\/comments"}}]', $this->serialize($list));
}

public function testReplaceNameInOutput()
{
$this->dispatcher->addSubscriber(new ReplaceNameSubscriber());
$this->handlerRegistry->registerHandler(GraphNavigator::DIRECTION_SERIALIZATION, 'JMS\Serializer\Tests\Fixtures\AuthorList', 'json',
function(VisitorInterface $visitor, AuthorList $data, array $type, Context $context) {
return $visitor->visitArray(iterator_to_array($data), $type, $context);
}
);

$list = new AuthorList();
$list->add(new Author('foo'));
$list->add(new Author('bar'));

$this->assertEquals('[{"full_name":"new name"},{"full_name":"new name"}]', $this->serialize($list));
}

/**
* @expectedException RuntimeException
Expand Down Expand Up @@ -268,3 +283,18 @@ public static function getSubscribedEvents()
);
}
}

class ReplaceNameSubscriber implements EventSubscriberInterface
{
public function onPostSerialize(Event $event)
{
$event->getVisitor()->replaceData('full_name', 'new name');
}

public static function getSubscribedEvents()
{
return array(
array('event' => 'serializer.post_serialize', 'method' => 'onPostSerialize', 'format' => 'json', 'class' => 'JMS\Serializer\Tests\Fixtures\Author'),
);
}
}

0 comments on commit 447c7d3

Please sign in to comment.