-
-
Notifications
You must be signed in to change notification settings - Fork 586
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(union): Add deserialisation of Union types from JSON
- Loading branch information
Marcin Czarnecki
committed
Jul 30, 2023
1 parent
363a140
commit 6532bda
Showing
7 changed files
with
173 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace JMS\Serializer\Handler; | ||
|
||
use JMS\Serializer\Context; | ||
use JMS\Serializer\DeserializationContext; | ||
use JMS\Serializer\Exception\RuntimeException; | ||
use JMS\Serializer\GraphNavigatorInterface; | ||
use JMS\Serializer\SerializationContext; | ||
use JMS\Serializer\Visitor\DeserializationVisitorInterface; | ||
use JMS\Serializer\Visitor\SerializationVisitorInterface; | ||
|
||
final class UnionHandler implements SubscribingHandlerInterface | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public static function getSubscribingMethods() | ||
{ | ||
return [ | ||
[ | ||
'type' => 'union', | ||
'direction' => GraphNavigatorInterface::DIRECTION_DESERIALIZATION, | ||
'format' => 'json', | ||
'method' => 'deserializeUnion', | ||
], | ||
[ | ||
'type' => 'union', | ||
'direction' => GraphNavigatorInterface::DIRECTION_DESERIALIZATION, | ||
'format' => 'xml', | ||
'method' => 'deserializeUnion', | ||
], | ||
[ | ||
'type' => 'union', | ||
'format' => 'json', | ||
'direction' => GraphNavigatorInterface::DIRECTION_SERIALIZATION, | ||
'method' => 'serializeUnion', | ||
], | ||
[ | ||
'type' => 'union', | ||
'format' => 'xml', | ||
'direction' => GraphNavigatorInterface::DIRECTION_SERIALIZATION, | ||
'method' => 'serializeUnion', | ||
], | ||
]; | ||
} | ||
|
||
public function serializeUnion( | ||
SerializationVisitorInterface $visitor, | ||
$data, | ||
array $type, | ||
SerializationContext $context | ||
) { | ||
return $this->matchSimpleType($data, $type, $context); | ||
} | ||
|
||
/** | ||
* @param int|string|\SimpleXMLElement $data | ||
* @param array $type | ||
*/ | ||
public function deserializeUnion(DeserializationVisitorInterface $visitor, $data, array $type, DeserializationContext $context) | ||
{ | ||
if ($data instanceof \SimpleXMLElement) { | ||
throw new RuntimeException('XML deserialisation into union types is not supported yet.'); | ||
} | ||
|
||
return $this->matchSimpleType($data, $type, $context); | ||
} | ||
|
||
private function matchSimpleType($data, array $type, Context $context) | ||
{ | ||
$dataType = gettype($data); | ||
$alternativeName = null; | ||
switch ($dataType) { | ||
case 'boolean': | ||
$alternativeName = 'bool'; | ||
break; | ||
case 'integer': | ||
$alternativeName = 'int'; | ||
break; | ||
case 'double': | ||
$alternativeName = 'float'; | ||
break; | ||
case 'array': | ||
case 'string': | ||
break; | ||
default: | ||
throw new RuntimeException(); | ||
} | ||
|
||
foreach ($type['params'] as $possibleType) { | ||
if ($possibleType['name'] === $dataType || $possibleType['name'] === $alternativeName) { | ||
return $context->getNavigator()->accept($data, $possibleType); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters