php-gson
, (*1)
Zero dependency simple PHP entity mapper.
JSON String to Objcet and Object to JSON String., (*2)
Installation, (*3)
composer require archangeldesign/php-gson
or download and include autoload.php, (*4)
include 'php-gson/src/PHPGson/autoload.php';
Usage, (*5)
Without instance.
Object will be created using given class name., (*6)
$complexObject = null;
$success = \PHPGson\Gson::fromJson(
$complexObject,
'{"age":35, "hydratorTestObject":{"username":"raff"}}',
\PHPGson\Extractor::EXTRACTION_MODE_METHOD,
ComplexHydrationObject::class
);
With manual instantiation.
Sub-objects will be created automatically in both cases., (*7)
$complexObject = new ComplexHydrationObject();
$success = \PHPGson\Gson::fromJson(
$complexObject,
'{"age":35, "hydratorTestObject":{"username":"raff"}}'
);
$object = new MainObject();
$jsonString = Gson::toJson($object);