{"id":21894767,"url":"https://github.com/zumba/json-serializer","last_synced_at":"2025-05-15T15:04:39.587Z","repository":{"id":12977621,"uuid":"15656289","full_name":"zumba/json-serializer","owner":"zumba","description":"Serialize PHP variables, including objects, in JSON format. Support to unserialize it too.","archived":false,"fork":false,"pushed_at":"2024-12-11T02:38:15.000Z","size":185,"stargazers_count":126,"open_issues_count":6,"forks_count":23,"subscribers_count":15,"default_branch":"master","last_synced_at":"2025-05-04T17:41:59.528Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"http://tech.zumba.com/2014/01/06/json-serializer/","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":"aspnetboilerplate/eventcloud","license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/zumba.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2014-01-05T18:27:03.000Z","updated_at":"2025-02-10T11:22:37.000Z","dependencies_parsed_at":"2023-01-13T17:14:15.238Z","dependency_job_id":"e2ccbd35-e577-4383-9053-4a0772d61421","html_url":"https://github.com/zumba/json-serializer","commit_stats":{"total_commits":109,"total_committers":13,"mean_commits":8.384615384615385,"dds":"0.33944954128440363","last_synced_commit":"1b1b2302d46692f317021ee4b9cc06b1311b7333"},"previous_names":[],"tags_count":14,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zumba%2Fjson-serializer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zumba%2Fjson-serializer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zumba%2Fjson-serializer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zumba%2Fjson-serializer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/zumba","download_url":"https://codeload.github.com/zumba/json-serializer/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253273341,"owners_count":21881967,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-28T13:28:03.215Z","updated_at":"2025-05-15T15:04:39.566Z","avatar_url":"https://github.com/zumba.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Json Serializer for PHP\n\n\u003cp align=\"center\"\u003e\n    \u003ca href=\"LICENSE.txt\" target=\"_blank\"\u003e\n        \u003cimg alt=\"Software License\" src=\"https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square\"\u003e\n    \u003c/a\u003e\n    \u003cimg alt=\"Build Status\" src=\"https://github.com/zumba/json-serializer/actions/workflows/php.yml/badge.svg?branch=master\"\u003e\n    \u003ca href=\"https://packagist.org/packages/zumba/json-serializer\" target=\"_blank\"\u003e\n        \u003cimg alt=\"Total Downloads\" src=\"https://img.shields.io/packagist/dt/zumba/json-serializer.svg?style=flat-square\"\u003e\n    \u003c/a\u003e\n    \u003ca href=\"https://packagist.org/packages/zumba/json-serializer\" target=\"_blank\"\u003e\n        \u003cimg alt=\"Latest Stable Version\" src=\"https://img.shields.io/packagist/v/zumba/json-serializer.svg?style=flat-square\u0026label=stable\"\u003e\n    \u003c/a\u003e\n\u003c/p\u003e\n\nThis is a library to serialize PHP variables in JSON format. It is similar of the `serialize()` function in PHP,\nbut the output is a string JSON encoded. You can also unserialize the JSON generated by this tool and have you\nPHP content back.\n\nSupported features:\n\n- Encode/Decode of scalar, null, array\n- Encode/Decode of objects\n- Encode/Decode of binary data\n- Support nested serialization\n- Support not declared properties on the original class definition (ie, properties in `stdClass`)\n- Support object recursion\n- Closures (via 3rd party library. See details below)\n\nUnsupported serialization content:\n\n- Resource (ie, `fopen()` response)\n- NAN, INF constants\n\nLimitations:\n\n- Binary data containing null bytes (\\u0000) as array keys cannot be properly decoded because of a json extension bug:\n    - https://github.com/remicollet/pecl-json-c/issues/7\n    - https://github.com/json-c/json-c/issues/108\n\nThis project should not be confused with `JsonSerializable` interface added on PHP 5.4. This interface is used on\n`json_encode` to encode the objects. There is no unserialization with this interface, differently from this project.\n\n*Json Serializer requires PHP \u003e= 7.2 and tested until PHP 8.4*\n\n## Example\n\n```php\n\nclass MyCustomClass {\n\tpublic $isItAwesome = true;\n\tprotected $nice = 'very!';\n}\n\n$instance = new MyCustomClass();\n\n$serializer = new Zumba\\JsonSerializer\\JsonSerializer();\n$json = $serializer-\u003eserialize($instance);\n// $json will contain the content {\"@type\":\"MyCustomClass\",\"isItAwesome\":true,\"nice\":\"very!\"}\n\n$restoredInstance = $serializer-\u003eunserialize($json);\n// $restoredInstance will be an instance of MyCustomClass\n```\n\n## How to Install\n\nIf you are using composer, install the package [`zumba/json-serializer`](https://packagist.org/packages/zumba/json-serializer).\n\n```\n$ composer require zumba/json-serializer\n```\n\nOr add the `zumba/json-serializer` directly in your `composer.json` file.\n\nIf you are not using composer, you can just copy the files from `src` folder in your project.\n\n## Serializing Binary Strings\n\nBinary strings introduce two special identifiers in the final json: `@utf8encoded` and `@scalar`.\n`@utf8encoded` is an array of keys from the original data which have their value (or the keys themselves)\nencoded from 8bit to UTF-8. This is how the serializer knows what to encode back from UTF-8 to 8bit when deserializing.\nExample:\n\n```php\n$data = ['key' =\u003e '\u003cbinaryvalue\u003e', 'anotherkey' =\u003e 'nonbinaryvalue'];\n$serializer = new Zumba\\JsonSerializer\\JsonSerializer();\n$json = $serializer-\u003eserialize($data);\n// $json will contain the content {\"key\":\"\u003cutf8encodedbinaryvalue\u003e\",\"anotherkey\":\"nonbinaryvalue\",\"@utf8encoded\":{\"key\":1}}\n```\n\n`@scalar` is used only when the value to be encoded is not an array or an object but a binary string. Example:\n\n```php\n$data = '\u003cbinaryvalue\u003e';\n$serializer = new Zumba\\JsonSerializer\\JsonSerializer();\n$json = $serializer-\u003eserialize($data);\n// $json will contain the content {\"@scalar\":\"\u003cutf8encodedbinaryvalue\u003e\",\"@utf8encoded\":1}\n```\n\n\n## Serializing Closure\n\nFor serializing PHP closures you can either use [OpisClosure](https://github.com/opis/closure) (preferred) or\n[SuperClosure](https://github.com/jeremeamia/super_closure) (the project is abandoned, so kept here for backward\ncompatibility).\n\nClosure serialization has some limitations. Please check the OpisClosure or SuperClosure project to check if it fits\nyour needs.\n\nTo use the OpisClosure with JsonSerializer, just add it to the closure serializer list. Example:\n\n```php\n$toBeSerialized = [\n\t'data' =\u003e [1, 2, 3],\n\t'worker' =\u003e function ($data) {\n\t\t$double = [];\n\t\tforeach ($data as $i =\u003e $number) {\n\t\t\t$double[$i] = $number * 2;\n\t\t}\n\t\treturn $double;\n\t}\n];\n\n$jsonSerializer = new \\Zumba\\JsonSerializer\\JsonSerializer();\n$jsonSerializer-\u003eaddClosureSerializer(new \\Zumba\\JsonSerializer\\ClosureSerializer\\OpisClosureSerializer());\n$serialized = $jsonSerializer-\u003eserialize($toBeSerialized);\n```\n\nYou can load multiple closure serializers in case you are migrating from SuperClosure to OpisClosure for example.\n\nPS: JsonSerializer does not have a hard dependency of OpisClosure or SuperClosure. If you want to use both projects\nmake sure you add both on your composer requirements and load them with `addClosureSerializer()` method.\n\n## Custom Serializers\n\nSome classes may not be suited to be serialized and unserialized using the default reflection methods.\n\nCustom serializers provide the ability to define ```serialize``` and ```unserialize``` methods for specific classes.\n\n```php\nclass MyType {\n    public $field1;\n    public $field2;\n}\n\nclass MyTypeSerializer {\n    public function serialize(MyType $obj) {\n        return array('fields' =\u003e $obj-\u003efield1 . ' ' . $obj-\u003efield2);\n    }\n\n    public function unserialize($values) {\n        list($field1, $field2) = explode(' ', $values['fields']);\n        $obj = new MyType();\n        $obj-\u003efield1 = $field1;\n        $obj-\u003efield2 = $field2;\n        return $obj;\n    }\n}\n\n// map of \"class name\" =\u003e Custom serializer\n$customObjectSerializers['MyType'] = new MyTypeSerializer();\n$jsonSerializer = new Zumba\\JsonSerializer\\JsonSerializer(null, $customObjectSerializers);\n\n$toBeSerialized = new MyType();\n$toBeSerialized-\u003efield1 = 'x';\n$toBeSerialized-\u003efield2 = 'y';\n$json = $jsonSerializer-\u003eserialize($toBeSerialized);\n// $json == {\"@type\":\"Zumba\\\\\\\\JsonSerializer\\\\\\\\Test\\\\\\\\SupportClasses\\\\\\\\MyType\",\"fields\":\"x y\"}\n\n$myType = $jsonSerializer-\u003eunserialize($json);\n// $myType == $toBeSerialized\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzumba%2Fjson-serializer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzumba%2Fjson-serializer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzumba%2Fjson-serializer/lists"}