1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
<?php
namespace Gedcomx\GedcomxFile;
use Gedcomx\Extensions\FamilySearch\FamilySearchPlatform;
use Gedcomx\Gedcomx;
/**
* A class for performing JSON serialization and deserialization.
*
* Class DefaultJsonSerialization
*
* @package Gedcomx\GedcomxFile
*/
class DefaultJsonSerialization implements GedcomxEntrySerializer, GedcomxEntryDeserializer
{
/**
* Deserialize the resource from the specified input stream.
*
* @param string $incoming The text to deserialize.
*
* @return mixed the resource.
*/
public function deserialize($incoming)
{
$resources = null;
$json = json_decode($incoming);
foreach ($json as $key => $value){
if (JsonMapper::isKnownType($key)) {
$class = XmlMapper::getClassName($key);
$resources[] = new $class($value);
}
}
return $resources;
}
/**
* Serialize the resource to the specified output stream.
*
* @param mixed $resource
*
* @return void
*/
public function serialize($resource)
{
return $resource->toJson();
}
/**
* Whether the specified content type is a known content type and therefore
* does not need to be written to the entry attributes.
*
* @param string $contentType The content type.
*
* @return boolean Whether the content type is "known".
*/
public function isKnownContentType($contentType)
{
return in_array($contentType, array(
Gedcomx::JSON_MEDIA_TYPE,
FamilySearchPlatform::JSON_MEDIA_TYPE,
FamilySearchPlatform::JSON_APPLICATION_TYPE
));
}}