{"id":17042145,"url":"https://github.com/bluem/php-javascript-serializer","last_synced_at":"2025-04-30T05:22:05.953Z","repository":{"id":62495410,"uuid":"90306556","full_name":"BlueM/PHP-JavaScript-Serializer","owner":"BlueM","description":"Dependency-free serialization of PHP data to a JavaScript representation","archived":false,"fork":false,"pushed_at":"2019-03-28T07:13:51.000Z","size":17,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-30T13:03:57.825Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/BlueM.png","metadata":{"files":{"readme":"Readme.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-05-04T20:27:37.000Z","updated_at":"2024-06-11T03:39:14.000Z","dependencies_parsed_at":"2022-11-02T09:32:11.438Z","dependency_job_id":null,"html_url":"https://github.com/BlueM/PHP-JavaScript-Serializer","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BlueM%2FPHP-JavaScript-Serializer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BlueM%2FPHP-JavaScript-Serializer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BlueM%2FPHP-JavaScript-Serializer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BlueM%2FPHP-JavaScript-Serializer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/BlueM","download_url":"https://codeload.github.com/BlueM/PHP-JavaScript-Serializer/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251645984,"owners_count":21620846,"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-10-14T09:15:16.418Z","updated_at":"2025-04-30T05:22:05.933Z","avatar_url":"https://github.com/BlueM.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![Build Status](https://api.travis-ci.org/BlueM/PHP-JavaScript-Serializer.svg?branch=master)](https://travis-ci.org/BlueM/PHP-JavaScript-Serializer)\n[![SensioLabsInsight](https://insight.sensiolabs.com/projects/a2a2401a-906e-4f9d-b889-416890a620ca/mini.png)](https://insight.sensiolabs.com/projects/a2a2401a-906e-4f9d-b889-416890a620ca)\n\nOverview\n========\n\nThis library provides a simple, dependency-free serialization of a PHP variable value / datastructure to strings which can be interpreted as a JavaScript value.\n\nThe most typical use case will be the automated generation of JavaScript code in cases where the amount of data is notably and therefore serializing to JSON would suffer from the deserialization penalty and/or the larger size of JSON data.\n\nWhile I would have expected there are already some simililar libraries on packagist.org, I did not find any that are *not* bound to a specific use case, dataset or framework. If I missed something, please do not hesitate to contact me.\n\n\nInstallation\n-------------\nAdd `\"bluem/javascript-serializer\": \"^1.0\"` to the requirements in your `composer.json` file or run `composer require \"bluem/javascript-serializer\"` at the shell.\n\nAs this library uses [semantic versioning](https://semver.org), you will get fixes and feature additions when running `composer update`, but not changes which break the API.\n\n\nUsage\n----\n\nInstantiate the class, call the `serialize()` method with any variable and use the return type. That’s it.\n\n    $jss = new \\BlueM\\JavaScriptSerializer();\n    $result = $jss-\u003eserialize($myVariable);\n\nThe following datatypes can be handled:\n\n* `null`\n* `string`\n* `float`\n* `int`\n* `array`\n* `\\DateTime` (converted to a JavaScript `Date` constructor call)\n* `object` – if the object implements the `\\JsonSerializable` interface, has a public `toArray` method or has a public `__toString` method. (The mentioned order is exactly the order in which the code performs the checks.)\n\n\nExamples\n--------\n\n**Example 1:** Scalar PHP value\n\nInput PHP data:\n\n    $var = 'Hello world';\n\nValue returned from `$serializer-\u003eserialize($var)` call will be this string:\n\n    'Hello world'\n\n**Example 2:** Array with numeric keys\n\nInput PHP data:\n\n    $var = ['A', 'B', 3.14, 4711];\n\nValue returned from `$serializer-\u003eserialize($var)` call will be this string:\n\n    ['A', 'B', 3.14, 4711]\n\n\n**Example 3:** Nested array\n \nInput PHP data:\n\n    [\n        'foo'    =\u003e 'bar',\n        'nested' =\u003e [\n            'pi'     =\u003e 3.14,\n            'key'    =\u003e null,\n            'abc'    =\u003e \"String \\\" with 'quotes'\",\n            'My key' =\u003e 'Hello world',\n            'bar'    =\u003e [\n                'key'  =\u003e 'A',\n                'code' =\u003e 65,\n            ],\n        ]\n    ]\n\nValue returned from `$serializer-\u003eserialize($var)` call will be this string:\n\n    {foo: 'bar', nested: {pi: 3.14, key: null, abc: 'String \" with \\'quotes\\'', 'My key': 'Hello world', bar: {key: 'A', code: 65}}}\n\n\nKnown issues\n------------\n* When creating object properties, strings that can be used as properties without quotes are inserted verbatim, without quotes. However, this decision is based on a very simple RegEx, which not only ignores the existence of ECMAScript 2015 Symbols, but also [other property names that can be used without quotes](https://mothereff.in/js-properties#12e34). This has no influence on using the code in JavaScript, but only on the code size. But chances are you will use some sort of minification, so this should not be a problem.\n\n\nToDo\n----\n* In addition to the current, compact format, a “pretty-print” format should be choosable.\n\n\nVersion History\n=================\n\n1.1, 2019-03-28\n---------------\n* Native PHP `\\DateTime` instances are converted to native JavaScript `Date` constructor calls\n\n1.0.1, 2017-10-14\n-----------------\n* Improve exception message (explicitly mention class name) when object is not serializable.\n* Improve doc comments and Readme.\n\n1.0, 2017-05-04\n----------------\n* First public version. Nothing else to say.\n\n\nAuthor \u0026 License\n=================\nThis code was written by Carsten Blüm (www.bluem.net) and licensed under the BSD 2-Clause license.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbluem%2Fphp-javascript-serializer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbluem%2Fphp-javascript-serializer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbluem%2Fphp-javascript-serializer/lists"}