{"id":43655672,"url":"https://github.com/realshadow/serializers","last_synced_at":"2026-02-04T20:34:59.488Z","repository":{"id":62533944,"uuid":"39917877","full_name":"realshadow/serializers","owner":"realshadow","description":"PHP collection of serializers for JSON, JSONP, XML, YAML, INI","archived":false,"fork":false,"pushed_at":"2015-12-18T16:31:59.000Z","size":44,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2023-07-15T19:27:10.271Z","etag":null,"topics":["deserialization","json-serialization","jsonp","php","serialization","xml","xml-serialization","yaml"],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/realshadow.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-07-29T21:44:33.000Z","updated_at":"2023-07-15T19:27:10.272Z","dependencies_parsed_at":"2022-11-02T16:00:56.828Z","dependency_job_id":null,"html_url":"https://github.com/realshadow/serializers","commit_stats":null,"previous_names":[],"tags_count":1,"template":null,"template_full_name":null,"purl":"pkg:github/realshadow/serializers","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/realshadow%2Fserializers","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/realshadow%2Fserializers/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/realshadow%2Fserializers/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/realshadow%2Fserializers/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/realshadow","download_url":"https://codeload.github.com/realshadow/serializers/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/realshadow%2Fserializers/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29095444,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-04T20:17:23.003Z","status":"ssl_error","status_checked_at":"2026-02-04T20:16:36.396Z","response_time":62,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":["deserialization","json-serialization","jsonp","php","serialization","xml","xml-serialization","yaml"],"created_at":"2026-02-04T20:34:57.867Z","updated_at":"2026-02-04T20:34:59.482Z","avatar_url":"https://github.com/realshadow.png","language":"PHP","readme":"# PHP collection of serializers for JSON, JSONP, XML, YAML, INI\nWritten for PHP 5.3 to solve a specific issue - **singularization/pluralization** of elements when transforming them from and to arrays. The difference between serializers was obvious when we passed XML files between PHP, Python and .NET. Later, more serializers were added to get a complete package.\n\nThe goal was to automatically perform singularization of arrays when serializing to XML. E.g.\n\n```php\narray(\n\t'products' =\u003e array(\n\t    array(\n\t        'brand' =\u003e 'Samsung',\n\t        'model' =\u003e 'Galaxy',\n\t        'price' =\u003e 999\n\t    ),\n\t    array(\n\t        'brand' =\u003e 'HTC',\n\t        'model' =\u003e 'One',\n\t        'price' =\u003e null\n\t    )\n\t)\n);\n```\n\nWhich, when serialized, would become\n\n```xml\n \u003cproducts\u003e\n  \u003cproduct\u003e\n   \u003cbrand\u003eSamsung\u003c/brand\u003e\n   \u003cmodel\u003eGalaxy\u003c/model\u003e\n   \u003cprice\u003e999\u003c/price\u003e\n  \u003c/product\u003e\n  \u003cproduct\u003e\n   \u003cbrand\u003eHTC\u003c/brand\u003e\n   \u003cmodel\u003eOne\u003c/model\u003e\n   \u003cprice xsi:nil=\"true\"\u003e\u003c/price\u003e\n  \u003c/product\u003e\n \u003c/products\u003e\n```\n\ninstead of (this is the default behaviour)\n\n```xml\n \u003cproducts\u003e\n   \u003cbrand\u003eSamsung\u003c/brand\u003e\n   \u003cmodel\u003eGalaxy\u003c/model\u003e\n   \u003cprice\u003e999\u003c/price\u003e\n \u003c/products\u003e\n \u003cproducts\u003e\n   \u003cbrand\u003eHTC\u003c/brand\u003e\n   \u003cmodel\u003eOne\u003c/model\u003e\n   \u003cprice xsi:nil=\"true\"\u003e\u003c/price\u003e\n \u003c/products\u003e\n```\n\nSame rule applies to deserialization which, by using different serializers, would turn into into different arrays. By applying singularization the other way around, it is possible to **get back the same array** as was used for serialization.\n\n### XML Serialization\nSupport for\n\n* attributes, namespaces, cdata and comments\n* singularization of words - products =\u003e product\n* option to automatically add xsi:nil=true to null elements\n* event for manipulation of nodes\n\n```php\n$array = array(\n\tSerializers\\Encoders\\Xml::ATTRIBUTES =\u003e array(\n\t\t'xmlns' =\u003e 'http://cfh.sk/izmluvnik/xsell',\n\t\tSerializers\\Encoders\\Xml::NS =\u003e array(\n\t\t\tarray(\n\t\t\t\t'name' =\u003e 'xmlns:xsi',\n\t\t\t\t'content' =\u003e 'http://www.w3.org/2001/XMLSchema-instance'\n\t\t\t),\n\t\t\tarray(\n\t\t\t\t'name' =\u003e 'xmlns:xsd',\n\t\t\t\t'content' =\u003e 'http://www.w3.org/2001/XMLSchema'\n\t\t\t)\n\t\t)\n\t),\n\t'products' =\u003e array(\n\t    array(\n\t        'brand' =\u003e 'Samsung',\n\t        'model' =\u003e 'Galaxy',\n\t        'price' =\u003e 999\n\t    ),\n\t    array(\n\t        'brand' =\u003e 'HTC',\n\t        'model' =\u003e 'One',\n\t        'price' =\u003e null\n\t    )\n\t)\n);\n\n$xml = Serializers\\Encode::toXml('root', $array, array(\n    'singularize_words' =\u003e true,\n    'nil_on_null' =\u003e true\n));\n\nprint $xml-\u003ewithHeaders();\n```\n\nWhich outputs\n\n```xml\n\u003c?xml version=\"1.0\" encoding=\"UTF-8\"?\u003e\n\u003croot xmlns=\"http://cfh.sk/izmluvnik/xsell\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"\u003e\n \u003cproducts\u003e\n  \u003cproduct\u003e\n   \u003cbrand\u003eSamsung\u003c/brand\u003e\n   \u003cmodel\u003eGalaxy\u003c/model\u003e\n   \u003cprice\u003e999\u003c/price\u003e\n  \u003c/product\u003e\n  \u003cproduct\u003e\n   \u003cbrand\u003eHTC\u003c/brand\u003e\n   \u003cmodel\u003eOne\u003c/model\u003e\n   \u003cprice xsi:nil=\"true\"\u003e\u003c/price\u003e\n  \u003c/product\u003e\n \u003c/products\u003e\n\u003c/root\u003e\n```\n\n### XML Deserialization\nBy default every comment, attribute, namespace will be stripped from the result as well as\nthe root element. Every option can be turned off/on in config\n\nDeserialization is done by SimpleXML coupled with json_encode (in this case provided JSON decoder) with one\nsimple addition - SimpleXML object will be transformed before being encoded with json_encode (backport of\nJSONSerialize interface)\n\nComments are parsed separately via DOMXpath (since SimpleXML can not handle them) and are\nadded to a separate array with indexes poiting to their original location, with that, it should\nbe easy to merge comments with the main result and receive the original array.\n\nBy default, transforming elements from their singular counterpart back to plural and thus\nflattening the whole array is turned off and must be turned on. Its possible to both - include\nnew mappings for words and to exclude specific words. This works exactly as in provided XML\nencoder.\n\nThe whole goal of flattening is to get back **exactly** the same array as the one that was used\nto create provided XML.\n\n```php\n// using the same XML that we got in serialization\n$output = Serializers\\Decode::xml($xml-\u003eload(), array('singularize_words' =\u003e true));\n```\n\nWhich outputs **exactly the same array** as was used in the example before\n\n```php\nprint_r($output-\u003etoArray());\n```\n\n### JSON Deserialization\nSupport for:\n *  automatic parsing of Microsoft's JSON date format (e.g. `/Date(1425556377427+0100)/`)\n *  backport of `JSON_BIGINT_AS_STRING` available from PHP 5.4.0\n *  isValid method for checking validity of provided JSON string\n *  possible conversion from JSON to:\n    - PHP types (string, array, object)\n    - XML, YAML, INI\n\nWith overriding configuration one can change the default timeformat and timezone settings form MS date conversion, or turn it off completely.\n\nIt's possible to register an event callback to be called during escaping of BIGINT, in case said escaping is not good enough, or to turning it off completely.\n\nCallback method must accept one parameter and thats registered JSON string. Callback can be a closure or anything else that will pass as callable.\n\n```php\n$json = \u003c\u003c\u003cEOT\n    {\n        \"foo\" : \"bar\",\n        \"small\" : \"123456\",\n        \"large\" : 200000000000009093302,\n        \"text\" : \"Example ratio 1000000000000000:1\",\n        \"date\" : \"/Date(1425556377427+0100)/\"\n    }\nEOT;\n\n$s = Serializers\\Decode::json($json);\n\nprint_r($s-\u003etoObject());\n\n// transform said json to xml and output it\n\nprint Serializers\\Decode::json($json)-\u003etoXml('root')-\u003ewithHeaders();\n\n// events\n\n$json = Serializers\\Decode::json($json)-\u003eon(Serializers\\Events::JSON_MSDATE_MATCH, function($date) {\n    // matches returned from preg_replace_callback\n    list(, $timestamp,,) = $date;\n\n    return date('Y-m-d H:i:s', $timestamp);\n});\n```\n\n### JSON Serialization\nIt is possible to register JSON_SERIALIZE event that works exactly like PHP 5.4 `JsonSerializable` interface and thus allows modifying the object before it is converted to JSON.\n\nJSON Serializer also includes a method for creating dates in Microsoft JSON date format, e.g `/Date(1425556377427+0100)/`\n\n```php\n$json = Serializers\\Encode::toJson(array(\n    'foo' =\u003e 'bar',\n    'foodate' =\u003e date('d.m.Y H:i:s')\n))-\u003eonSerialize(function($data) {\n    $data['foodate'] = Serializers\\Encoders\\Json::toMSDate($data['foodate']);\n\n    return $data;\n});\n\nprint $json-\u003ewithHeaders();\n```\n\n### JSONP Serialization\nClass for easy JSONP serialization, behaves like JSON serializer with additional checks for callback function name validation, which can be changed with custom event\n\n```php\n$jsonp = Serializers\\Encode::toJsonp('_foo.bar', array(\n    'foo' =\u003e 'bar',\n    'bar' =\u003e 'foo'\n));\n\n$jsonp-\u003eallowCors('*', array('GET', 'POST'));\n\nprint $jsonp-\u003ewithHeaders();\n```\n\n### JSONP Deserialization\nClass for easy JSONP deserialization, behaves like JSON deserializer\n\n```php\n$json = '_foo.bar({\"foo\":\"bar\",\"bar\":\"foo\"})';\n\n$data = Serializers\\Decode::jsonp($json);\n\nprint_r($data-\u003etoObject());\n\n// transform said json to xml with callback name as root element and output it\n\nprint Serializers\\Decode::jsonp($json)-\u003etoXml()-\u003ewithHeaders();\n```\n\n### YAML Serializer\nUses Symfony's YAML component under the hood\n\n```php\n$yaml = \\Serializers\\Encode::toYaml($array);\n\nprint_r($yaml-\u003eload());\n\n// or\n\n$yaml-\u003etoFile('config.yml');\n```\n\n### YAML Deserializer\nUses Symfony's YAML component under the hood.\n\nTransformation to `XML`, `JSON`, etc. is possible, but is subjected to the possibilities of the YAML converter.\n\n```php\n$yaml = Serializers\\Decode::yaml(file_get_contents('config.yml'));\n\nprint_r($yaml-\u003etoObject());\n\n// transform said json to xml and output it\n\nprint Serializers\\Decode::yaml(file_get_contents('config.yml'))-\u003etoXml('yaml')-\u003ewithHeaders();\n```\n\n### INI Deserializer\nUses INI parser by @austinhyde\n\n```php\n$array = array(\n\t'a' =\u003e 'd',\n\t'b' =\u003e array('test' =\u003e 'c'),\n\t'database' =\u003e array(\n\t\t'default' =\u003e array(\n\t\t\t'name' =\u003e 'db',\n\t\t\t'host' =\u003e 'master.db',\n\t\t\t'ip' =\u003e 'dd',\n\t\t)\n\t),\n\t'array' =\u003e array('a', '1', 3),\n);\n\n$encode = Serializers\\Encode::toIni($array);\n$encode-\u003etoFile('config.ini');\n```\n\n### INI Serializer\nThe functionality is limited to basic INI formats, e.g. no support for inheritance. As I can't see a good use case at the moment, this class is here only for keeping a complete stack of encoders/decoders together\n\n```php\n$ini = Serializers\\Encode::toIni($array);\n\nprint_r($ini-\u003eload());\n```","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frealshadow%2Fserializers","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frealshadow%2Fserializers","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frealshadow%2Fserializers/lists"}