-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
80 additions
and
0 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,46 @@ | ||
<?php | ||
|
||
namespace Thruway\Serializer; | ||
|
||
use Thruway\Exception\DeserializationException; | ||
use Thruway\Message\Message; | ||
|
||
/** | ||
* Class JsonSerializer | ||
* Serialize and deserialize using JSON methods | ||
* | ||
* @package Thruway\Serializer | ||
*/ | ||
class JsonSerializer implements SerializerInterface | ||
{ | ||
|
||
/** | ||
* Serialize message | ||
* | ||
* @param \Thruway\Message\Message $msg | ||
* @return string | ||
*/ | ||
public function serialize(Message $msg) | ||
{ | ||
return json_encode($msg); | ||
} | ||
|
||
/** | ||
* Deserialize message | ||
* | ||
* @param string $serializedData | ||
* @return \Thruway\Message\Message | ||
* @throws \Thruway\Exception\DeserializationException | ||
*/ | ||
public function deserialize($serializedData) | ||
{ | ||
if (null === ($data = @json_decode($serializedData))) { | ||
throw new DeserializationException("Error decoding json \"" . $serializedData . "\""); | ||
} | ||
|
||
$msg = Message::createMessageFromArray($data); | ||
|
||
return $msg; | ||
} | ||
|
||
} |
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,34 @@ | ||
<?php | ||
|
||
namespace Thruway\Serializer; | ||
|
||
use Thruway\Message\Message; | ||
|
||
/** | ||
* Interface SerializerInterface | ||
* | ||
* The serializer is responsible for converting input (specific to the serialization) | ||
* into an array and also taking an array and converting it to output | ||
* | ||
* @package Thruway\Serializer | ||
*/ | ||
interface SerializerInterface | ||
{ | ||
|
||
/** | ||
* Serialize message | ||
* | ||
* @param \Thruway\Message\Message $msg | ||
* @return mixed | ||
*/ | ||
public function serialize(Message $msg); | ||
|
||
/** | ||
* Deserialize message | ||
* | ||
* @param mixed $serializedData | ||
* @return \Thruway\Message\Message | ||
*/ | ||
public function deserialize($serializedData); | ||
|
||
} |