Download the PHP package andrewsauder/json-deserialize without Composer
On this page you can find all versions of the php package andrewsauder/json-deserialize. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download andrewsauder/json-deserialize
More information about andrewsauder/json-deserialize
Files in andrewsauder/json-deserialize
Package json-deserialize
Short Description PHP Utility to add automated json deserialize into typed objects
License MIT
Informations about the package json-deserialize
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 >=PHP 8
Installation
composer require andrewsauder/json-deserialize
Usage
Extend your class from \andrewsauder\jsonDeserialize\jsonDeserialize
class then call {yourClass}::jsonDeserialize( {stringified json} );
to deserialize the JSON automatically into your class.
Attributes
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
Tag properties on your class with the #[excludeJsonSerialize]
attribute to prevent that field from being serialized into the output of json_encode()
;
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
Static method called on a class immediately before deserialization into the class occurs.
After Json Deserialize
Called on a newly created instance after deserialization is complete.
Before Json Serialize
Called on an instance immediately before serialization to JSON.
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.
Code Example
myModel.php
myController.php
Output:
Debug Logging
To enable debug logging, add these lines prior to deserializing. All objects deserialized after will include debug logging to a new log file in the provided path. The logging is expensive but it is useful for determining missing properties on classes or json data.
Fine Tune Logging
By default, when logging is enabled, all debugging messages are enabled.
-
To turn off messages about properties that exist in the class but do not exist in the JSON source:
\andrewsauder\jsonDeserialize\config::setLogJsonMissingProperty( true );
-
To turn off messages about properties that exist in the JSON source but do not exist in the class:
\andrewsauder\jsonDeserialize\config::setLogClassMissingProperty( true );
- To turn off messages about properties in the class without a strict type:
\andrewsauder\jsonDeserialize\config::setLogClassPropertyMissingType( true );