PHP code example of hitrain / jsonmapper

1. Go to this page and download the library: Download hitrain/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/ */

    

hitrain / jsonmapper example snippets



    
    new JsonMapper();
    $contactObject = $mapper->map($jsonContact, new Contact());
    


    
    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);
    


    
    class Contact implements JsonMapperInterface
    {
        /**
         * Full name
         * @var string
         */
        public $name;
        
        /**
         * define mapping rule with assoc array
         * @return array
         */
        public function mappingRule()
        {
           return [
               'jsonName' => 'name',
               ];
       }
    }
    


   $jm = new JsonMapper();
   $jm->bUseMappingRule = true;
   $contact->map($json, new Contact());

Getting nested property
-----------------------
Getting nested property to root object property

Your local ``Contact`` class:

.. code:: php

    
    class Contact implements JsonMapperInterface
    {
        /**
         * Full name
         * @var string
         */
        public $name;
        
        /**
         * define mapping rule with assoc array
         * @return array
         */
        public function mappingRule()
        {
           return [
               'jsonObject.nestedJsonObject.jsonName' => 'name',
               ];
       }
    }
    


   $jm = new JsonMapper();
   $jm->bUseMappingRule = true;
   $contact->map($json, new Contact())


    /**
     * @var DateTime
     */
    public $date


    $jm = new JsonMapper();
    $jm->classMap['Foo'] = 'Bar';
    $jm->map(...)


    $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(...)


    /**
     * @var string
     * @


    $jm = new JsonMapper();
    $jm->bExceptionOnMissingData = 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(...)