tsufeki / kayo-json-mapper
Map JSON data to PHP objects
Installs: 2 293
Dependents: 2
Suggesters: 0
Security: 0
Stars: 0
Watchers: 2
Forks: 1
Open Issues: 2
Requires
- php: >=7.1
- phpdocumentor/reflection-docblock: ^4.3
- phpdocumentor/type-resolver: ^0.5 || ^0.4
Requires (Dev)
- phpstan/phpstan: ^0.11
- phpstan/phpstan-phpunit: ^0.11
- phpunit/phpunit: ^7
README
Map JSON data to PHP objects and back. No custom annotations needed.
Installation
With Composer:
$ composer require tsufeki/kayo-json-mapper
Usage
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));
Configuration
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.
Types & loading
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.
License
MIT - see LICENCE.