Download the PHP package matthiasnoback/naive-serializer without Composer
On this page you can find all versions of the php package matthiasnoback/naive-serializer. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download matthiasnoback/naive-serializer
More information about matthiasnoback/naive-serializer
Files in matthiasnoback/naive-serializer
Package naive-serializer
Short Description A naive JSON serializer which recursively converts an object graph to and from JSON, without any configuration or custom code.
License MIT
Informations about the package naive-serializer
Naive serializer
The JsonSerializer that comes with this library is a very simple serializer/deserializer which recursively converts an object graph to and from JSON, without any configuration or custom code. Its design goals are:
- Users shouldn't be forced to add custom configuration to their existing classes.
- Users shouldn't need to write any supporting code.
- The solution should take care of as few edge cases as possible.
- The solution should be as small as possible, without becoming useless (<=100 LOC).
- The solution should warn the user about its limitations using descriptive exceptions.
In order to make this work, this library restricts you to using only values of type:
null
- scalar (int, float, bool)
- user-defined objects (so no built-in PHP classes like
\DateTimeImmutable
) - arrays where every value is of the same type (maps or lists)
- and any combination of the above
Furthermore, you need to define the types you used in standard @var
docblock annotations (as you probably already do), e.g.
Of course, every property should have just one @var
annotation.
For simple types, you can use PHP native property types as well:
array
-typed properties still require an additional @var
annotation. You can define lists of the above types by simply adding []
to the @var
annotation, e.g.
To work around the limitation that you can't use PHP's built-in classes, simply convert the data internally to something else. For example, to use a \DateTimeImmutable
timestamp:
To use the serializer:
To deserialize the data:
You can force the (de)serializer to ignore certain properties by adding @ignore
somewhere in its doc block:
If you like, you can create an instance of JsonSerializer
and call its object methods instead of Serializer
's static methods.
Thanks
This library stands on the shoulders of the phpdocumentor/reflection-docblock
library, which does all the work related to property type resolving.
All versions of naive-serializer with dependencies
beberlei/assert Version ^3.2
phpdocumentor/reflection-docblock Version ^5.2
ext-json Version *