PHP code example of anujrnair / php-json-marshaller
1. Go to this page and download the library: Download anujrnair/php-json-marshaller library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
anujrnair / php-json-marshaller example snippets
use PhpJsonMarshaller\Annotations\MarshallProperty;
class User
{
/**
* @MarshallProperty(name="id", type="int")
* This property is public - it will be accessed directly
*/
public $id;
/**
* This property is NOT public - it must be accessed through a getter (marshalling) or a setter (unmarshalling)
*/
protected $name;
/**
* @MarshallProperty(name="name", type="string")
* This function will be used for marshalling json
*/
public function getName()
{
return $this->name;
}
/**
* @MarshallProperty(name="name", type="string")
* This function will be used for unmarshalling json
*/
public function setName($name)
{
$this->name = $name;
}
}
use PhpJsonMarshaller\Decoder\ClassDecoder;
use PhpJsonMarshaller\Marshaller\JsonMarshaller;
use PhpJsonMarshaller\Reader\DoctrineAnnotationReader;
$json = '{
"id": 12345,
"name": "Anuj"
}';
// Assuming autoloading
$marshaller = new JsonMarshaller(new ClassDecoder(new DoctrineAnnotationReader()));
// Notice the fully qualified namespace!
$user = $marshaller->unmarshall($json, '\My\Example\User');
// Use the new class
echo $user->getName(); // (string) 'Anuj'
// Marshall the class
$marshalled = $marshaller->marshall($user);
// $json and $marshalled are both json_encoded string holding the same data
$json == $marshalled;
use PhpJsonMarshaller\Annotations\MarshallProperty;
class User
{
/**
* @MarshallProperty(name="address", type="\My\Example\Address")
* Notice the fully qualified namespace!
*/
public $address;
}
use PhpJsonMarshaller\Decoder\ClassDecoder;
use PhpJsonMarshaller\Marshaller\JsonMarshaller;
use PhpJsonMarshaller\Reader\DoctrineAnnotationReader;
$json = '{
"id": 12345,
"name": "Anuj",
"address": {
"id": 1,
"street": "123 Main Street"
}
}';
// Assuming autoloading
$marshaller = new JsonMarshaller(new ClassDecoder(new DoctrineAnnotationReader()));
// Notice the fully qualified namespace!
$user = $marshaller->unmarshall($json, '\My\Example\User');
// Use the nested object
$user->getAddress()->getStreet(); // (string) "123 Main Street"
use PhpJsonMarshaller\Annotations\MarshallProperty;
class User
{
/**
* @MarshallProperty(name="flags", type="\My\Example\Flag[]")
* Notice the fully qualified namespace and the array indicator in the type
*/
public $flags;
}
use PhpJsonMarshaller\Decoder\ClassDecoder;
use PhpJsonMarshaller\Marshaller\JsonMarshaller;
use PhpJsonMarshaller\Reader\DoctrineAnnotationReader;
$json = '{
"id": 12345,
"name": "Anuj",
"flags": [
{
"id": 11087,
"value": 0
},
{
"id": 11088,
"value": 1
}
],
}';
// Assuming autoloading
$marshaller = new JsonMarshaller(new ClassDecoder(new DoctrineAnnotationReader()));
// Notice the fully qualified namespace!
$user = $marshaller->unmarshall($json, '\My\Example\User');
// Use the data
$user->getFlags()[0]->getId(); // (int) 11087
use PhpJsonMarshaller\Annotations\MarshallCreator;
use PhpJsonMarshaller\Annotations\MarshallProperty;
class User
{
/**
* @var int $id
*/
protected $id;
/**
* @var float $rank
*/
protected $rank;
/**
* @MarshallCreator({@MarshallProperty(name="id", type="int"), @MarshallProperty(name="rank", type="float")})
*/
public function __construct($id, $rank)
{
$this->id = $id;
$this->rank = $rank;
}
}
use PhpJsonMarshaller\Cache\Cache;
use PhpJsonMarshaller\Cache\Storage\InMemoryStorage;
use PhpJsonMarshaller\Decoder\ClassDecoder;
use PhpJsonMarshaller\Marshaller\JsonMarshaller;
use PhpJsonMarshaller\Reader\DoctrineAnnotationReader;
$marshaller = new JsonMarshaller(
new ClassDecoder(
new DoctrineAnnotationReader(),
new Cache(
new InMemoryStorage()
)
)
);
use PhpJsonMarshaller\Annotations\MarshallConfig;
/**
* @MarshallConfig(ignoreUnknown=false)
*/
class User
{
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.