项目作者: Kingson-de

项目描述 :
Marshal JSON is serializing / marshalling data structures to JSON. It is also deserializing / unmarshalling JSON back to the data structures.
高级语言: PHP
项目地址: git://github.com/Kingson-de/marshal-json-serializer.git
创建时间: 2017-12-16T23:13:34Z
项目社区:https://github.com/Kingson-de/marshal-json-serializer

开源协议:Apache License 2.0

下载


Marshal JSON Serializer

Marshal Serializer logo

License
Build Status
Code Coverage
Scrutinizer Code Quality

Introduction

Marshal JSON is serializing / marshalling) data structures to JSON. It is also deserializing / unmarshalling JSON back to the data structures.

Installation

Easiest way to install the library is via composer:

  1. composer require kingson-de/marshal-json-serializer

The following PHP versions are supported:

  • PHP 7.0
  • PHP 7.1
  • PHP 7.2
  • PHP 7.3

Execute tests

Just run:

  1. composer test

Or without code coverage:

  1. composer quicktest

Usage

How to create Data Structures which can be serialized?

Please check the Marshal Serializer README for more information.

How to use the Marshal JSON Serializer library?

The library provides several static methods to create your JSON data once you defined the data structures.

  1. <?php
  2. use KingsonDe\Marshal\Data\Item;
  3. use KingsonDe\Marshal\MarshalJson;
  4. $json = MarshalJson::serialize(new Item($mapper, $model));
  5. // or
  6. $json = MarshalJson::serializeItem($mapper, $model);
  7. // or
  8. $json = MarshalJson::serializeItemCallable(function (User $user) {
  9. return [
  10. 'username' => $user->getUsername(),
  11. 'email' => $user->getEmail(),
  12. 'birthday' => $user->getBirthday()->format('Y-m-d'),
  13. 'followers' => count($user->getFollowers()),
  14. ];
  15. }, $user);
  16. // or
  17. $json = MarshalJson::serializeCollection($mapper, $modelCollection);
  18. // or
  19. $json = MarshalJson::serializeCollectionCallable(function (User $user) {
  20. return [
  21. 'username' => $user->getUsername(),
  22. 'email' => $user->getEmail(),
  23. 'birthday' => $user->getBirthday()->format('Y-m-d'),
  24. 'followers' => count($user->getFollowers()),
  25. ];
  26. }, $userCollection);

Deserializing / Unmarshalling

To transform JSON back to your structure use Marshal’s deserialize functions.
You need a class extending the AbstractObjectMapper which will be passed to the deserializeJson function.
When using FlexibleData’s get function it will throw an OutOfBoundsException if the key does not exist.
If an exception is not needed the find function can be used which will return a custom default value in that case.

  1. <?php
  2. use KingsonDe\Marshal\AbstractObjectMapper;
  3. use KingsonDe\Marshal\Data\FlexibleData;
  4. class UserIdMapper extends AbstractObjectMapper {
  5. public function map(FlexibleData $flexibleData, ...$additionalData) {
  6. return $flexibleData->get('id');
  7. }
  8. }
  1. <?php
  2. use KingsonDe\Marshal\MarshalJson;
  3. $json = '{"id": 123}';
  4. $id = MarshalJson::deserializeJson($json, new UserIdMapper());

Another option would be to use the deserializeCallable function.

  1. <?php
  2. use KingsonDe\Marshal\MarshalJson;
  3. $id = MarshalJson::deserializeJsonCallable($json, function (FlexibleData $flexibleData) {
  4. return $flexibleData['id'];
  5. });

Modify existing JSON

An easy way to modify existing JSON is to use FlexibleData.
Here is an example:

  1. <?php
  2. use KingsonDe\Marshal\Data\FlexibleData;
  3. use KingsonDe\Marshal\MarshalJson;
  4. $json = '{"name": "John Doe"}';
  5. $flexibleData = new FlexibleData(MarshalJson::deserializeJsonToData($json));
  6. $flexibleData['name'] = 'Jane Doe';
  7. $modifiedJson = MarshalJson::serialize($flexibleData);

License

This project is released under the terms of the Apache 2.0 license.