better-serializer / better-serializer
General de/serializer for vatious serialization formats.
Installs: 3 493
Dependents: 1
Suggesters: 0
Security: 0
Stars: 27
Watchers: 4
Forks: 2
Open Issues: 0
Requires
- php: ^7.2
- doctrine/annotations: ^1.3
- doctrine/cache: ^v1.7
- doctrine/instantiator: ^1.0
- league/flysystem: ^1.0
- marc-mabe/php-enum: ^2.3
- phpdocumentor/reflection-docblock: ^4.3
- pimple/pimple: ^3.0
Requires (Dev)
- doctrine/collections: ^1.5
- friendsofphp/php-cs-fixer: ^2.5
- infection/infection: ^0.8
- jakub-onderka/php-parallel-lint: ^0.9.2
- jms/serializer: ^1.8
- nikic/php-parser: ^3.0
- phpmd/phpmd: ^2.6
- phpro/grumphp: ^v0.14
- phpstan/phpstan: ^0.7
- phpunit/phpunit: ^7.1
- satooshi/php-coveralls: ~1.0
- sebastian/phpcpd: ^4.0
- sensiolabs/security-checker: ^4.0
- squizlabs/php_codesniffer: ^3.2
- symfony/var-dumper: ^v3.2
This package is auto-updated.
Last update: 2023-03-16 14:12:12 UTC
README
This library provides a general serializer for PHP. Currently only JSON serialization format is supported. The project aims to be an alternative to JmsSerializer. It tries to be faster then JmsSerializer and it also tries to sustain a better maintainable and understandable code base. Also, as this is also a learning experiment, one of the goals is to have unit tests with 100% code coverage.
Except the above mentioned goals, the project also aims to provide some cool features - it tries to combine the best features from JmsSerializer and from Jackson in Java.
Current state
Currently, only JSON de/serialization is implemented. It's possible to de/serialize complex nested data structures (objects and arrays). Only arrays are supported as collection types for now.
Performance
For now the code is only a proof of concept, but it is stabilizing more and more each day. It already yields interesting results. Without implementing metadata caching, the serialization process is already 4-6x faster than using JmsSerializer. The deserialization process is also faster, but only cca 3.5x faster.
There is also another benchmark, which compares JMS Serializer, Ivory Serializer and Better Serialier. It also integrates Symfony serializer, but I needed to comment it out, because it was 100-300x slower than Better Serializer. You can find a fork of it here and try it on your own. For best results, please disable XDebug while running the tests.
Here are some of the results:
$ php bin/benchmark --iteration 1000 --horizontal-complexity 2 --vertical-complexity 2 Ivory: Done! JMS: Done! BetterSerializer: Done! +------------------+----------------+--------+ | Serializer | Duration (sec) | Factor | +------------------+----------------+--------+ | BetterSerializer | 0.001188s | 1.00x | | Ivory | 0.002222s | 1.87x | | JMS | 0.002901s | 2.44x | +------------------+----------------+--------+ $ php bin/benchmark --iteration 100 --horizontal-complexity 10 --vertical-complexity 10 Ivory: Done! JMS: Done! BetterSerializer: Done! +------------------+----------------+--------+ | Serializer | Duration (sec) | Factor | +------------------+----------------+--------+ | BetterSerializer | 0.011377s | 1.00x | | Ivory | 0.037377s | 3.29x | | JMS | 0.046837s | 4.12x | +------------------+----------------+--------+ $ php bin/benchmark --iteration 1 --horizontal-complexity 100 --vertical-complexity 200 Ivory: Done! JMS: Done! BetterSerializer: Done! +------------------+----------------+--------+ | Serializer | Duration (sec) | Factor | +------------------+----------------+--------+ | BetterSerializer | 1.040695s | 1.00x | | Ivory | 4.191479s | 4.03x | | JMS | 4.466160s | 4.29x | +------------------+----------------+--------+ $ php bin/benchmark --iteration 1 --horizontal-complexity 200 --vertical-complexity 200 Ivory: Done! JMS: Done! BetterSerializer: Done! +------------------+----------------+--------+ | Serializer | Duration (sec) | Factor | +------------------+----------------+--------+ | BetterSerializer | 4.523851s | 1.00x | | Ivory | 15.580491s | 3.44x | | JMS | 18.571867s | 4.11x | +------------------+----------------+--------+
This means that you can now effectively de/serialize 4x more entities (e.g. in API results) using the same amount of time than before!
Regarding the performance gains - I'd like someone to check the measured values, since the results seem quite great and I'm suspicious myself :).
Features
You can check out the features in the features page. Please also check the supported annotations documentation.
Requirements
This library requires PHP 7.2 and it won't work with older versions. Older versions won't be supported.
Usage
The usage is described here.
The de/serializaton annotations are described here.
If you'd like to write custom extensions for special de/serialising of selected data types, check the extensions section
Future Plans
metadata caching- XML and YAML support
- various collection classes support (
Doctrine collections, internal PHP collections like SplStack) - data injection using class constructors (
internaland static), which should improve performance even more - various features import from JmsSerializer and Jackson
- framework integrations
Contributions are welcome! :)