PHP code example of brick / json-mapper

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

    

brick / json-mapper example snippets


use Brick\JsonMapper\JsonMapper;

class User
{
    public function __construct(
        public int $id,
        public string $name,
    ) {
    }
}

$json = '{
  "id": 123,
  "name": "John Doe"
}';

$mapper = new JsonMapper();
$user = $mapper->map($json, User::class);

echo $user->name; // John Doe

class Album
{
    public function __construct(
        public int $id,
        public string $title,
        public Artist $artist,
    ) {
    }
}

class Artist
{
    public function __construct(
        public int $id,
        public string $name,
    ) {
    }
}

$json = '{
  "id": 456,
  "title": "The Wall",
  "artist": {
    "id": 789,
    "name": "Pink Floyd"
  }
}';

$mapper = new JsonMapper();
$album = $mapper->map($json, Album::class);

echo $album->artist->name; // Pink Floyd

class Customer
{
    /**
     * @param Address[] $addresses
     */
    public function __construct(
        public int $id,
        public string $name,
        public array $addresses,
    ) {
    }
}

class Address
{
    public function __construct(
        public string $street,
        public string $city,
    ) {
    }
}

$json = '{
  "id": 123,
  "name": "John Doe",
  "addresses": [
    {
      "street": "123 Main Street",
      "city": "New York"
    },
    {
      "street": "456 Side Street",
      "city": "New York"
    }
  ]
}';

$mapper = new JsonMapper();
$customer = $mapper->map($json, Customer::class);

foreach ($customer->addresses as $address) {
    var_export($address instanceof Address); // true
}

class Order
{
    public function __construct(
        public readonly int $id,
        public readonly string $amount,
        public readonly Person|Company $customer, // union type
    ) {
    }
}

class Person
{
    public function __construct(
        public readonly int $id,
        public readonly string $firstname,
        public readonly string $lastname,
    ) {
    }
}

class Company
{
    public function __construct(
        public readonly int $id,
        public readonly string $name,
        public readonly string $companyNumber,
    ) {
    }
}

$json = '{
  "id": 1,
  "amount": "24.99",
  "customer": {
    "id": 2,
    "firstname": "John",
    "lastname": "Doe"
  }
}';

$mapper = new JsonMapper();
$order = $mapper->map($json, Order::class);

// JsonMapper automatically determined that the "id", "firstname",
// and "lastname" properties correspond to a Person and not a Company.
var_export($order->customer instanceof Person); // true

/**
 * @param (Person|Company|(string|int)[])[]|null $customers
 */
public function __construct(
    public readonly ?array $customers,
) {
}

    /**
     * @param (Person|Company)[] $value
     */
    

    /**
     * @param Person[]|Company[] $value
     */
    

class Order
{
    public function __construct(
        public readonly int $id,
        public readonly OrderStatus $status,
    ) {
    }
}

enum OrderStatus: string {
    case PENDING = 'pending';
    case SHIPPED = 'shipped';
    case DELIVERED = 'delivered';
}

$json = '{
  "id": 1,
  "status": "shipped"
}';

$mapper = new JsonMapper();
$order = $mapper->map($json, Order::class);

var_export($order->status === OrderStatus::SHIPPED); // true

  $mapper = new JsonMapper(
      allowUntypedArrays: true,
  );
  

  $mapper = new JsonMapper(
      allowUntypedObjects: true,
  );
  

  $mapper = new JsonMapper(
      allowMixed: true,
  );
  

    use Brick\JsonMapper\JsonMapper;
    use Brick\JsonMapper\OnExtraProperties;
    
    class Order
    {
        public function __construct(
            public readonly int $id,
            public readonly string $amount,
        ) {
        }
    }
    
    $json = '{
      "id": 1,
      "amount": "100.00",
      "extraProperty": "foo",
      "otherExtraProperty": "bar"
    }';
    
    $mapper = new JsonMapper(
        onExtraProperties: OnExtraProperties::IGNORE,
    );
    
    // extra properties "extraProperty" and "otherExtraProperty" are ignored,
    // and do not throw an exception anymore.
    $order = $mapper->map($json, Order::class);
    

    use Brick\JsonMapper\JsonMapper;
    use Brick\JsonMapper\OnMissingProperties;
    
    class Order
    {
        public function __construct(
            public readonly int $id,
            public readonly ?string $customerName,
        ) {
        }
    }
    
    $json = '{
      "id": 1
    }';
    
    $mapper = new JsonMapper(
        onMissingProperties: OnMissingProperties::SET_NULL,
    );
    
    $order = $mapper->map($json, Order::class);
    var_export($order->customerName); // NULL
    

    use Brick\JsonMapper\JsonMapper;
    use Brick\JsonMapper\OnMissingProperties;
    
    class Order
    {
        public function __construct(
            public readonly int $id,
            public readonly string $customerName = 'no name',
        ) {
        }
    }
    
    $json = '{
      "id": 1
    }';
    
    $mapper = new JsonMapper(
        onMissingProperties: OnMissingProperties::SET_DEFAULT,
    );
    
    $order = $mapper->map($json, Order::class);
    var_export($order->customerName); // 'no name'
    

  use Brick\JsonMapper\JsonMapper;
  use Brick\JsonMapper\NameMapper\CamelCaseToSnakeCaseMapper;
  use Brick\JsonMapper\NameMapper\SnakeCaseToCamelCaseMapper;
  
  class Order
  {
      public function __construct(
          public readonly int $id,
          public readonly int $amountInCents,
          public readonly string $customerName,
      ) {
      }
  }
  
  $json = '{
    "id": 1,
    "amount_in_cents": 2499,
    "customer_name": "John Doe"
  }';
  
  $mapper = new JsonMapper(
      jsonToPhpNameMapper: new SnakeCaseToCamelCaseMapper(),
      phpToJsonNameMapper: new CamelCaseToSnakeCaseMapper(),
  );
  
  $order = $mapper->map($json, Order::class);

  echo $order->amountInCents; // 2499
  echo $order->customerName; // 'John Doe'