https://github.com/duncan3dc/serial
A collection of PHP serialization helpers with a consistent interface for each.
https://github.com/duncan3dc/serial
json php serializer yaml
Last synced: about 1 year ago
JSON representation
A collection of PHP serialization helpers with a consistent interface for each.
- Host: GitHub
- URL: https://github.com/duncan3dc/serial
- Owner: duncan3dc
- License: apache-2.0
- Created: 2014-10-13T12:53:01.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2021-07-24T16:50:41.000Z (almost 5 years ago)
- Last Synced: 2025-06-08T22:21:37.300Z (about 1 year ago)
- Topics: json, php, serializer, yaml
- Language: PHP
- Size: 49.8 KB
- Stars: 0
- Watchers: 1
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
serial
======
A collection of PHP serialization helpers with a consistent interface for each.
[](https://packagist.org/packages/duncan3dc/serial)
](https://github.com/duncan3dc/serial/actions?query=branch%3Amaster+workflow%3Abuildcheck)
[](https://codecov.io/gh/duncan3dc/serial)
Available Classes
=================
* Json (using the native json_* functions)
* Yaml (using the Symfony Yaml component)
* Php (using the native serialize/unserialize functions)
Interface
=========
All serialization classes implement the interface [duncan3dc\Serial\SerialInterface](src/SerialInterface.php)
Examples
========
Convert array data to string format
```php
use duncan3dc\Serial\Json;
$data = BusinessLogic::getDataAsArray();
$json = Json::encode($data);
```
Convert string formatted data to an array
```php
use duncan3dc\Serial\Yaml;
$yaml = Api::getYamlResponse($request);
$response = Yaml::decode($yaml);
```
Convient methods to serialize and store data on disk
```php
use duncan3dc\Serial\Json;
$filename = "/tmp/file.json";
$data = BusinessLogic::getDataAsArray();
Json::encodeToFile($filename, $data);
```
Retrieve previously stored data from disk
```php
use duncan3dc\Serial\Json;
$filename = "/tmp/file.json";
$data = Json::decodeFromFile($filename);
```
ArrayObject
===========
The `decode()` and `decodeFromFile()` methods return a custom [ArrayObject](http://php.net/manual/en/class.arrayobject.php).
If you need a plain array you can get one like so:
```php
$array = Json::decode($jsonString)->asArray();
```
There are also helper methods to convert to any of the available serialization formats.
```php
$data = Json::decode($jsonString);
$yaml = $data->asYaml();
$json = $data->asJson();
$php = $data->asPhp();
```