https://github.com/b2pweb/bdf-serializer
A basic but powerful PHP serializer
https://github.com/b2pweb/bdf-serializer
Last synced: 9 months ago
JSON representation
A basic but powerful PHP serializer
- Host: GitHub
- URL: https://github.com/b2pweb/bdf-serializer
- Owner: b2pweb
- License: mit
- Created: 2019-11-15T15:21:29.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2025-07-01T08:04:45.000Z (12 months ago)
- Last Synced: 2025-08-23T19:39:23.018Z (10 months ago)
- Language: PHP
- Homepage:
- Size: 137 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
## Serializer
The Bdf Serializer can normalize, hydrate / extract and encode data or object.
It use doctrine/instantiator for instancing class and phpdocumentor for reading annotations.
[](https://github.com/b2pweb/bdf-serializer/actions/workflows/php.yml)
[](https://codecov.io/github/b2pweb/bdf-serializer)
[](https://packagist.org/packages/b2pweb/bdf-serializer)
[](https://packagist.org/packages/b2pweb/bdf-serializer)
[](https://shepherd.dev/github/b2pweb/bdf-serializer)
### Installation with Composer
```bash
composer require b2p/bdf-serializer
```
### Basic usage
```PHP
build();
$json = $serializer->toJson($object);
//...
```
### Declare metadata
2 drivers are available. The static method called and the annotations driver.
#### Static method driver
Declare your static method to build metadata
```PHP
integer('id');
$builder->string('name');
// You can also add group, alias, ...
$builder->string('name')
->addGroup('all')
->alias('user_name')
->since('1.0.0');
// DateTime options are available
$builder->dateTime('date')
->dateFormat('Y/m/d H:i')
->timezone('+01:00') // Use this timezone in internal
->toTimezone('+00:00'); // Export date with this timezone
}
}
```
#### Annotations driver
The annotations driver use phpdocumentor/reflection-docblock. The tag `@var` will be read.
If no tag is found, the default type is `string`.
Supported tags
* `var`: This annotation specify the type of the property. This tag is mandatory for deserialization.
* `since`: Enable object versionning. The value specify starting from which version this property is available.
* `until`: Enable object versionning. The value specify until which version this property was available.
* `SerializeIgnore`: Don't serialize this property.
NOTE: If type has not been detected in the phpdoc we try to add the typed property value added in PHP 7.4
#### JMS/serializer driver
The driver `Bdf\Serializer\Metadata\Driver\JMSAnnotationDriver` allows you to use JMS drivers.
The JMS metadata will be used to create Bdf metadata. Only few options of the serializer is used:
* `serializedName`
* `readOnly`
* `inline`
* `sinceVersion`
* `untilVersion`
* `getter`
* `setter`
* `groups`
* `type`
NOTE: The driver works with jms/serializer > v3.0 and php > v7.2
```PHP
"John",
"age" => null,
];
$builder = new \Bdf\Serializer\SerializerBuilder();
$builder->setNormalizers([new \Bdf\Serializer\Normalizer\ObjectNormalizer()]);
$serializer = $builder->build();
echo $serializer->toJson($object);
// {"name":"John"}
echo $serializer->toJson($object, [NormalizationContext::NULL => true]);
// {"name":"John","age":null}
```