PHPackages                             andrewsauder/json-deserialize - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. andrewsauder/json-deserialize

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

andrewsauder/json-deserialize
=============================

PHP Utility to add automated json deserialize into typed objects

v3.0.2(6mo ago)05057MITPHPPHP &gt;=8.1

Since Sep 27Pushed 6mo ago1 watchersCompare

[ Source](https://github.com/andrewsauder/jsonDeserialize)[ Packagist](https://packagist.org/packages/andrewsauder/json-deserialize)[ RSS](/packages/andrewsauder-json-deserialize/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)Dependencies (1)Versions (28)Used By (7)

Json Deserialize Utility
========================

[](#json-deserialize-utility)

JSON Deserialize is an abstract class that enables JSON deserialization into a specific class. Simply extend the jsonDeserialize class and then call the static jsonDeserialize method. Requires all properties to be typed. Array type will be determined by a PHPDoc definition.

Requires &gt;=PHP 8

Installation
------------

[](#installation)

`composer require andrewsauder/json-deserialize`

Usage
-----

[](#usage)

Extend your class from `\andrewsauder\jsonDeserialize\jsonDeserialize` class then call ` {yourClass}::jsonDeserialize( {stringified json} );` to deserialize the JSON automatically into your class.

Attributes
----------

[](#attributes)

### excludeJsonDeserialize

[](#excludejsondeserialize)

Tag properties on your class with the `#[excludeJsonDeserialize]` attribute to prevent the value of the field from being **deserialized** into the class generated by `jsonDeserialize()`

### excludeJsonSerialize

[](#excludejsonserialize)

Tag properties on your class with the `#[excludeJsonSerialize]` attribute to prevent that field from being **serialized** into the output of `json_encode()`;

### jsonSerializeDateTimeFormat(string $format)

[](#jsonserializedatetimeformatstring-format)

Tag properties on your class that implement DateTimeInterface with the `#[jsonSerializeDateTimeFormat('Y-m-d')]` attribute to force a specific format output. When this attribute is not present, the `DATE_ATOM` format is used by default.

Hooks
-----

[](#hooks)

The hooks provided allow you ti extend the default functionality of jsonDeserialize. Add hook methods to your class that extends `\andrewsauder\jsonDeserialize\jsonDeserialize`. The hook will be called automatically during the deserialization and serialization lifecycle.

### Before Json Deserialize

[](#before-json-deserialize)

Static method called on a class immediately before deserialization into the class occurs.

```
protected static function _beforeJsonDeserialize( string|\stdClass $json ): void {}
```

### After Json Deserialize

[](#after-json-deserialize)

Called on a newly created instance after deserialization is complete.

```
protected function _afterJsonDeserialize() : void {}
```

### Before Json Serialize

[](#before-json-serialize)

Called on an instance immediately before serialization to JSON.

```
protected function _beforeJsonSerialize() : void {}
```

### After Json Serialize

[](#after-json-serialize)

Called on an instance after serialization to a plain array. The standard export data is provided to the hook and the hook must return an array which will be immediately encoded to JSON.

```
protected function _afterJsonSerialize( array $export ): array {
    return $export;
}
```

Code Example
------------

[](#code-example)

myModel.php

```
class myModel extends \andrewsauder\jsonDeserialize\jsonDeserialize {

	public int    $varA = 1;

	public string $varB = 'B1';

	#[excludeJsonDeserialize]
	public string $varC = 'C1';

	/** @var string[]  */
	public array $varD = [ 'D1', 'D2', 'D3' ];

	public int    $varASquared = 1;

	protected function _afterJsonDeserialize() {
		//automagically called after self::jsonDeserialize() has finished its deserialization
		$this->varASquared = $this->varA * $this->varA;
	}

	protected function _beforeJsonSerialize() {
		//automagically called before json_encode( {$this} ) serializes object into JSON
		$this->varASquared = $this->varA * $this->varA;
	}
}
```

myController.php

```
class myController {

    public function post() {

        $jsonString = '{ "varA":2, "varASquared":3, "varB":"B2", "varC":"C2", "varD":[ "D4", "D5", "D6" ] }';
        $myModel = myModel::jsonDeserialize( $jsonString );

        //$myModel is now an instance of myModel
        echo $myModel->varA;
        echo $myModel->varASquared; //varB;
        echo $myModel->varC; //
