Map JSON data to PHP objects and back. No custom annotations needed.
With Composer:
$ composer require tsufeki/kayo-json-mapper
use Tsufeki\KayoJsonMapper\MapperBuilder;
$mapper = MapperBuilder::create()
->getMapper();
$serialized = '{"foo": [1, 2], "bar": "baz"}';
// Pass data and the expected type:
$object = $mapper->load(json_decode($serialized), AClass::class);
$serialized2 = json_encode($mapper->dump($object));
Kayo is designed to load/dump data without the need for special per class configuration such as annotations etc. All necessary information is gathered from reflection and doc comments.
However, its general behaviour can be customized in many ways through
MapperBuilder
methods.
All types recognized by phpDocumentor can be loaded, even union type (A|B
) --
but please note that objects are differentiated on their shape (i.e.
properties) so throwOnMissingProperty(true)
and
throwOnUnknownProperty(true)
are usually necessary.
MIT - see LICENCE.