This repository has been archived on 2018-10-11. You can view files and clone it, but cannot push or open issues or pull requests.
sleepy/Sleepy/Type/JSON.php
2014-05-07 14:05:13 -04:00

77 lines
1.2 KiB
PHP
Executable File

<?php
/**
* Sleepy - a REST framework
*
*
* A PHP Rest Framework valuing convention over configuration,
* but aiming to be as flexible as possible
*
* @author Timothy J. Warren
* @package Sleepy/types
*/
namespace Sleepy\Type;
use Sleepy\Core\aType;
/**
* Class for JSON output
*/
class JSON extends aType {
/**
* The mime type for output
*
* @var string
*/
protected $mime = 'application/json';
/**
* Convert the data into the output format
*
* @param mixed $data
* @return string
*/
public function serialize($data = NULL)
{
if ( ! is_null($data))
{
$this->data = $data;
}
else
{
$data = $this->data;
}
return json_encode($data, JSON_PRETTY_PRINT);
}
/**
* Convert the encoded data to a native format
*
* @param string $data_string
* @return object
*/
public function unserialize($data_string)
{
return json_decode($data_string);
}
}
// --------------------------------------------------------------------------
// ! Helper function
// --------------------------------------------------------------------------
/**
* Function to simplify type instantiation
*
* @param mixed $data
* @return JSON
*/
function JSON($data = NULL)
{
return new JSON($data);
}
// End of types/JSON.php