PHPackages                             pingyi/json-serializer - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Parsing &amp; Serialization](/categories/parsing)
4. /
5. pingyi/json-serializer

ActiveLibrary[Parsing &amp; Serialization](/categories/parsing)

pingyi/json-serializer
======================

Mixed of two repositories for serializing and unserializing PHP variables, object and closure.

v0.1.2(3y ago)1170↓100%1MITPHPPHP ^7.4||^8.0

Since Jan 30Pushed 3y ago1 watchersCompare

[ Source](https://github.com/ping-yee/json-serializer)[ Packagist](https://packagist.org/packages/pingyi/json-serializer)[ RSS](/packages/pingyi-json-serializer/feed)WikiDiscussions main Synced today

READMEChangelog (3)Dependencies (7)Versions (6)Used By (1)

Json Serializer with Closure for PHP
====================================

[](#json-serializer-with-closure-for-php)

Introduction
------------

[](#introduction)

This repository provides the variables, object and closure serializing and unserializing by integrate two repositories([Zumba\\JsonSerializer](https://github.com/zumba/json-serializer) and [laravel/serializable-closure](https://github.com/laravel/serializable-closure)) for PHP.

It still leave the original repo [Zumba\\JsonSerializer](https://github.com/zumba/json-serializer) operation and solve it's problem that cannot serialize the *closure* because the dependency repo [SuperClosure\\Serializer](https://github.com/jeremeamia/super_closure) is no longer maintained.

Simply put, This repository complements the shortcomings of two repositories and provides a complete soluation for PHP Object/Closure serialization.

Installation
------------

[](#installation)

### Prerequisites

[](#prerequisites)

1. PHP 7.4 or PHP 8^
2. Composer

### Composer Install

[](#composer-install)

```
composer require pingyi/json-serializer
```

How to Use
----------

[](#how-to-use)

### Serializing Data

[](#serializing-data)

You still use the Zumba\\JsonSerializer to serialize\\unserialize your variables or object, it will be like:

```
class MyCustomClass {
	public $isItAwesome = true;
	protected $nice = 'very!';
}

$instance = new MyCustomClass();

$serializer = new Zumba\JsonSerializer\JsonSerializer();
$json = $serializer->serialize($instance);
// $json will contain the content {"@type":"MyCustomClass","isItAwesome":true,"nice":"very!"}

$restoredInstance = $serializer->unserialize($json);
// $restoredInstance will be an instance of MyCustomClass
```

(From [Zumba\\JsonSerializer](https://github.com/zumba/json-serializer))

And the other more operation, refer to [Zumba\\JsonSerializer](https://github.com/zumba/json-serializer).

### Serializing Closure

[](#serializing-closure)

You may serialize your data including the clousre like this:

```
use Pingyi\JsonSerializer\ClosureSerializer\ClosureSerializer;
use Zumba\JsonSerializer\JsonSerializer;

$outsideData = "Jack";
$toBeSerialized = [
    "name"      => 'N$ck',
    "arrayData" => [1, 2, 3],
    "closure_1"   => fn (string $s = "Hello") => print_r($s . PHP_EOL),
    "closure_2"   => function (string $s = "Hello") use ($outsideData) {
        print_r("{$s} {$outsideData}." . PHP_EOL);
    }
];

$jsonSerializer = new JsonSerializer(new ClosureSerializer());

$serializedData   = $jsonSerializer->serialize($toBeSerialized);
$unserializedData = $jsonSerializer->unserialize($serializedData);

call_user_func($unserializedData["closure_1"]);
call_user_func($unserializedData["closure_2"]);
```

or your class including closure, like this:

```
use Pingyi\JsonSerializer\ClosureSerializer\ClosureSerializer;
use Zumba\JsonSerializer\JsonSerializer;
use stdClass;

$toBeSerializedClass = new stdClass();
$toBeSerializedClass->name      = "Mary";
$toBeSerializedClass->arrayData = [1, 2, 3];
$toBeSerializedClass->closure   = function (string $s = "Hello") {
    print_r("{$s}." . PHP_EOL);
};

$jsonSerializer = new JsonSerializer(new ClosureSerializer());

$serializedData   = $jsonSerializer->serialize($toBeSerializedClass);
$unserializedData = $jsonSerializer->unserialize($serializedData);

call_user_func($unserializedData->closure);
```

Also, you can serialize the simple closure by using the `ClosureSerializer`, like:

```
$closure   = function (string $s = "Hello") {
    print_r("{$s}." . PHP_EOL);
};

$closureSerializer = new ClosureSerializer();

$serializedData   = $closureSerializer->serialize($closure);
$unserializedData = $closureSerializer->unserialize($serializedData);

call_user_func($unserializedData);
```

In addition, you can set your secret key when you construct the `ClosureSerializer()`, like:

```
new ClosureSerializer("your secret key");
```

###  Health Score

24

—

LowBetter than 35% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity15

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity43

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~0 days

Total

4

Last Release

1145d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/6fbbf1829c221eef6af932074cd1fe65a6f80873f629cc93a7863ad7d633fae8?d=identicon)[ping-yee](/maintainers/ping-yee)

---

Top Contributors

[![ping-yee](https://avatars.githubusercontent.com/u/65348108?v=4)](https://github.com/ping-yee "ping-yee (20 commits)")

---

Tags

phpserializationphpjsonserializeserializerclosure

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan, Rector

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/pingyi-json-serializer/health.svg)

```
[![Health](https://phpackages.com/badges/pingyi-json-serializer/health.svg)](https://phpackages.com/packages/pingyi-json-serializer)
```

###  Alternatives

[zumba/json-serializer

Serialize PHP variables, including objects, in JSON format. Support to unserialize it too.

124731.8k12](/packages/zumba-json-serializer)[opensoft/simple-serializer

Simple Serializer

1914.2k1](/packages/opensoft-simple-serializer)[wayofdev/laravel-symfony-serializer

📦 Laravel wrapper around Symfony Serializer.

2112.1k](/packages/wayofdev-laravel-symfony-serializer)[goetas/xsd2php-runtime

Convert XSD (XML Schema) definitions into PHP classes

493.3k](/packages/goetas-xsd2php-runtime)[blancks/fast-jsonpatch-php

Class designed to efficiently handle JSON Patch operations in accordance with the RFC 6902 specification

395.2k](/packages/blancks-fast-jsonpatch-php)[josantonius/json

PHP simple library for managing Json files.

1721.3k10](/packages/josantonius-json)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
