PHP code example of pocketmine / netresearch-jsonmapper
1. Go to this page and download the library: Download pocketmine/netresearch-jsonmapper 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/ */
pocketmine / netresearch-jsonmapper example snippets
new JsonMapper();
$contactObject = $mapper->map($jsonContact, new Contact());
// or as classname
$contactObject = $mapper->map($jsonContact, Contact::class);
new JsonMapper();
$contactsArray = $mapper->mapArray(
$jsonContacts, array(), 'Contact'
);
class Contact
{
/**
* Full name
* @var string
*/
public $name;
/**
* @var Address
*/
public $address;
}
class Address
{
public $street;
public $city;
public function getGeoCoords()
{
//do something with $street and $city
}
}
$json = json_decode(file_get_contents('http://example.org/sheldon.json'));
$mapper = new JsonMapper();
$contact = $mapper->map($json, new Contact());
echo "Geo coordinates for " . $contact->name . ": "
. var_export($contact->address->getGeoCoords(), true);
/**
* @var DateTime
*/
public $date
$jm = new JsonMapper();
$jm->classMap['Foo'] = 'Bar';
$jm->map(...)
$mapper = function ($class, $jvalue) {
// examine $class and $jvalue to figure out what class to use...
return 'DateTime';
}
$jm->bStrictNullTypes = false
$jm = new JsonMapper();
$jm->bExceptionOnUndefinedProperty = true;
$jm->map(...)
/**
* Handle undefined properties during JsonMapper::map()
*
* @param object $object Object that is being filled
* @param string $propName Name of the unknown JSON property
* @param mixed $jsonValue JSON value of the property
*
* @return void
*/
function setUndefinedProperty($object, $propName, $jsonValue)
{
$object->{'UNDEF' . $propName} = $jsonValue;
}
$jm = new JsonMapper();
$jm->undefinedPropertyHandler = 'setUndefinedProperty';
$jm->map(...)
/**
* Handle undefined properties during JsonMapper::map()
*
* @param object $object Object that is being filled
* @param string $propName Name of the unknown JSON property
* @param mixed $jsonValue JSON value of the property
*
* @return void
*/
function fixPropName($object, $propName, $jsonValue)
{
return ucfirst($propName);
}
$jm = new JsonMapper();
$jm->undefinedPropertyHandler = 'fixPropName';
$jm->map(...)
/**
* @var string
* @
$jm = new JsonMapper();
$jm->bExceptionOnMissingData = true;
$jm->map(...)
$jm = new JsonMapper();
$jm->bRemoveUndefinedAttributes = true;
$jm->map(...)
$jm = new JsonMapper();
$jm->bIgnoreVisibility = true;
$jm->map(...)
$jm = new JsonMapper();
$jm->bStrictObjectTypeChecking = true;
$jm->map(...)
json_decode($jsonString, true)
By default, JsonMapper will throw an exception because ``map()`` .. code:: php
$jm = new JsonMapper();
$jm->bEnforceMapType = false;
$jm->map(...)
$jm = new JsonMapper();
$jm->postMappingMethod = 'afterMapping';
$jm->map(...)