Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow multiple nested serializer calls #341

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion src/JMS/Serializer/GenericSerializationVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,42 @@
use JMS\Serializer\Metadata\ClassMetadata;
use JMS\Serializer\Exception\InvalidArgumentException;
use JMS\Serializer\Metadata\PropertyMetadata;
use JMS\Serializer\Naming\PropertyNamingStrategyInterface;

abstract class GenericSerializationVisitor extends AbstractVisitor
{
private $navigatorStack;
private $navigator;
private $rootStack;
private $root;
private $dataStackStack;
private $dataStack;
private $data;

public function __construct(PropertyNamingStrategyInterface $namingStrategy)
{
parent::__construct($namingStrategy);
$this->navigatorStack = new \SplStack;
$this->dataStackStack = new \SplStack;
$this->rootStack = new \SplStack;
}

public function setNavigator(GraphNavigator $navigator)
{
$this->navigatorStack->push($this->navigator);
$this->dataStackStack->push($this->dataStack);
$this->rootStack->push($this->root);

$this->navigator = $navigator;
$this->root = null;
$this->dataStack = new \SplStack;
$this->root = null;
}

public function endNavigator()
{
$this->navigator = $this->navigatorStack->pop();
$this->dataStack = $this->dataStackStack->pop();
$this->root = $this->rootStack->pop();
}

/**
Expand Down
4 changes: 3 additions & 1 deletion src/JMS/Serializer/Serializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,10 @@ public function serialize($data, $format, SerializationContext $context = null)

$visitor->setNavigator($this->navigator);
$this->navigator->accept($visitor->prepare($data), null, $context);
$result = $visitor->getResult();
$visitor->endNavigator();

return $visitor->getResult();
return $result;
}

public function deserialize($data, $type, $format, DeserializationContext $context = null)
Expand Down
31 changes: 29 additions & 2 deletions src/JMS/Serializer/XmlSerializationVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use JMS\Serializer\Exception\RuntimeException;
use JMS\Serializer\Metadata\ClassMetadata;
use JMS\Serializer\Metadata\PropertyMetadata;
use JMS\Serializer\Naming\PropertyNamingStrategyInterface;

/**
* XmlSerializationVisitor.
Expand All @@ -31,18 +32,31 @@ class XmlSerializationVisitor extends AbstractVisitor
{
public $document;

private $documentStack;
private $navigator;
private $navigatorStack;
private $defaultRootName = 'result';
private $defaultRootNamespace;
private $defaultVersion = '1.0';
private $defaultEncoding = 'UTF-8';
private $stack;
private $stackStack;
private $metadataStack;
private $metadataStackStack;
private $currentNode;
private $currentMetadata;
private $hasValue;
private $nullWasVisited;

public function __construct(PropertyNamingStrategyInterface $namingStrategy)
{
parent::__construct($namingStrategy);
$this->navigatorStack = new \SplStack;
$this->documentStack = new \SplStack;
$this->stackStack = new \SplStack;
$this->metadataStackStack = new \SplStack;
}

public function setDefaultRootName($name, $namespace = null)
{
$this->defaultRootName = $name;
Expand All @@ -69,12 +83,25 @@ public function setDefaultEncoding($encoding)

public function setNavigator(GraphNavigator $navigator)
{
$this->navigatorStack->push($this->navigator);
$this->documentStack->push($this->document);
$this->stackStack->push($this->stack);
$this->metadataStackStack->push($this->metadataStack);

$this->navigator = $navigator;
$this->document = null;
$this->stack = new \SplStack;
$this->metadataStack = new \SplStack;
}

public function endNavigator()
{
$this->navigator = $this->navigatorStack->pop();
$this->document = $this->documentStack->pop();
$this->stack = $this->stackStack->pop();
$this->metadataStack = $this->metadataStackStack->pop();
}

public function getNavigator()
{
return $this->navigator;
Expand Down Expand Up @@ -199,7 +226,7 @@ public function startVisitingObject(ClassMetadata $metadata, $data, array $type,
}
$this->document->appendChild($this->currentNode);
}

$this->addNamespaceAttributes($metadata, $this->currentNode);

$this->hasValue = false;
Expand Down Expand Up @@ -421,7 +448,7 @@ private function attachNullNamespace()
$this->nullWasVisited = true;
}
}

/**
* Adds namespace attributes to the XML root element
*
Expand Down
17 changes: 17 additions & 0 deletions src/JMS/Serializer/YamlSerializationVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,25 +35,42 @@ class YamlSerializationVisitor extends AbstractVisitor
public $writer;

private $navigator;
private $navigatorStack;
private $stack;
private $stackStack;
private $metadataStack;
private $metadataStackStack;
private $currentMetadata;

public function __construct(PropertyNamingStrategyInterface $namingStrategy)
{
parent::__construct($namingStrategy);

$this->writer = new Writer();
$this->navigatorStack = new \SplStack;
$this->stackStack = new \SplStack;
$this->metadataStackStack = new \SplStack;
}

public function setNavigator(GraphNavigator $navigator)
{
$this->navigatorStack->push($this->navigator);
$this->stackStack->push($this->stack);
$this->metadataStackStack->push($this->metadataStack);

$this->navigator = $navigator;
$this->writer->reset();
$this->stack = new \SplStack;
$this->metadataStack = new \SplStack;
}

public function endNavigator()
{
$this->navigator = $this->navigatorStack->pop();
$this->stack = $this->stackStack->pop();
$this->metadataStack = $this->metadataStackStack->pop();
}

public function visitNull($data, array $type, Context $context)
{
if ('' === $this->writer->content) {
Expand Down
Loading