{"id":15740087,"url":"https://github.com/marcospassos/java-php-serializer","last_synced_at":"2025-11-10T12:04:51.996Z","repository":{"id":57726467,"uuid":"95620502","full_name":"marcospassos/java-php-serializer","owner":"marcospassos","description":"A Java library for serializing objects as PHP serialization format.","archived":false,"fork":false,"pushed_at":"2018-02-28T22:37:47.000Z","size":323,"stargazers_count":15,"open_issues_count":0,"forks_count":3,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-02-26T19:27:35.189Z","etag":null,"topics":["bridge","java","java-library","php","serialize-objects","serializer"],"latest_commit_sha":null,"homepage":"","language":"Java","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/marcospassos.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-06-28T02:28:28.000Z","updated_at":"2024-03-31T14:19:38.000Z","dependencies_parsed_at":"2022-09-26T21:50:48.929Z","dependency_job_id":null,"html_url":"https://github.com/marcospassos/java-php-serializer","commit_stats":null,"previous_names":[],"tags_count":11,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/marcospassos%2Fjava-php-serializer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/marcospassos%2Fjava-php-serializer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/marcospassos%2Fjava-php-serializer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/marcospassos%2Fjava-php-serializer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/marcospassos","download_url":"https://codeload.github.com/marcospassos/java-php-serializer/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243369893,"owners_count":20280097,"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":["bridge","java","java-library","php","serialize-objects","serializer"],"created_at":"2024-10-04T02:11:21.537Z","updated_at":"2025-11-10T12:04:51.960Z","avatar_url":"https://github.com/marcospassos.png","language":"Java","readme":"Java PHP Serializer\n===================\n[![Build Status][travis-badge]][travis-status]\n[![Coverage Status][coverall-badge]][coverall-status]\n[![Java 7+][java-badge]][java]\n[![License][mit-license-badge]](LICENSE)\n\nLatest release: [![Maven Central][maven-central-badge]][maven-central-latest]\n\nA Java library for serializing objects as PHP serialization format.\n\nThe library fully implements the PHP serialization format specification, which \nincludes:\n\n- Scalar values\n- Objects\n- Serializable (custom serializable objects)\n- Object and variable references\n\nThe [API documentation][api-docs] is available on GitHub Pages more convenient viewing in\nbrowser.\n\n\u003e **A word of notice**\n\u003e\n\u003e This library does not provide any mechanism for creating a communication\n\u003e channel between Java and PHP. For such purpose, consider using \n[Soluble Java][soluble-java].\n\n## Use case\n\nOne of the easier way to exchange data between Java and PHP consists in\nserializing the value to a data-interchanging format, such as JSON or XML,\nsend it through a communication channel and finally deserialize it back to the\noriginal form on the PHP side. The problem with this approach is that the cost\nof deserializing complex objects in PHP is very high.\n\nMost of the serialization libraries in PHP use reflection for re-hydrating\nobjects, and it becomes an issue when you have to deserialize large structures\nwith hundreds of objects. Fortunately, PHP's native serialization is fast and \nthe `unserialize()` function can handle such cases in a few milliseconds.\n\nThis library implements the full format specification through a friendly API \nthat encapsulates the complexity of the serialization process.\n\n## Installation\n\n### Maven\n\nThe PHP Serializer is available in the \n[Maven Central repository][maven-central-latest].\nAny Maven based project can use it directly by adding the appropriate entries\nto the `dependencies` section of its `pom.xml` file:\n\n```xml\n\u003cdependencies\u003e\n  \u003cdependency\u003e\n    \u003cgroupId\u003ecom.marcospassos\u003c/groupId\u003e\n    \u003cartifactId\u003ephpserializer\u003c/artifactId\u003e\n    \u003cversion\u003e0.8.0\u003c/version\u003e\n  \u003c/dependency\u003e\n\u003c/dependencies\u003e\n```\n\n### Binaries\n\nPackaged JARs can be downloaded directly from the [releases page][releases-page] \nand extracted using tar or unzip.\n\n## Usage\n\n### How to serialize data\n\nThe first step to serialize a Java object into a PHP serialization format\nstring is to create a Serializer instance according to your application domain \nmodel. The library ships a builder that help us with this task:\n\n```java\nSerializer serializer = new SerializerBuilder()\n\n  // Adds a custom exclusion strategy to determine which field\n  // should be serialized or not (default: no exclusion)\n  .addExclusionStrategy(new MyCustomExclusionStrategy())\n  \n  // Sets the naming strategy to convert the name of classes\n  // and fields from Java to PHP (default: PsrNamingStrategy)\n  .setNamingStrategy(new MyCustomNamingStrategy())\n  \n  // Registers all builtin adapters, using UTF-8 for encoding strings \n  // (default: all built-in adapters, UTF-8 charset)\n  .registerBuiltinAdapters()\n  \n  // Sets ISO-8859-1 as the default charset\n  //\n  // Notice that setCharset() register a new adapter configured with the \n  // specified charset. Calling setCharset() prior to registerBuiltinAdapters()\n  // will have no effect as the previous configuration will get overriden\n  // by the default adapter which encodes strings in UTF-8. \n  .setCharset(Charset.forName(\"ISO-8859-1\"))\n  \n  // Register a custom type adapter\n  .registerAdapter(CustomObject.class, new CustomObjectAdapter())\n  \n  // Creates the serialized based on the given configuration\n  .build();\n```\n\nNow you have a `Serializer` instance configured according to your application\ndomain model. To serialize a value, just invoke `serialize(Object object)` on\nthe serializer instance:\n\n```java\nList list = new ArrayList();\nlist.add(\"A string\");\nlist.add(12345);\nlist.add(true);\n\n// Outputs: a:3:{i:0;s:8:\"A string\";i:1;i:12345;i:2;b:1;}\nSystem.out.println(serializer.serialize(list));\n```\n\n### Naming strategies\n\nA naming strategy allows you to translate the name of classes and fields\naccording to the target domain model. The library ships with a built-in adapter\nthat converts Java packages to PHP namespaces in accordance with PSR-1 rules.\nIf the PSR strategy does not fit your needs you can easily implement a custom \nnaming strategy. Takes as reference the following strategy that appends an \nunderscore to all private fields:\n\n```java\npublic class UnderscoreNamingStrategy implements NamingStrategy\n{\n    public String getClassName(Class type)\n    {\n        return type.getName();\n    }\n\n    public String getFieldName(Field field)\n    {\n        if (Modifier.isPrivate(field.getModifiers())) {\n            return \"_\" + field.getName();\n        }\n\n        return field.getName();\n    }\n}\n```\n\n### Type Adapters\n\nA type adapter provides the serializer the logic for how to encode a specific \ntype. The following example shows how to create a custom type adapter\nto serialize Enums as string using the name of the enum constant:\n\n```java\npublic class EnumTypeAdapter implements TypeAdapter\u003cEnum\u003e\n{\n    public void write(Enum value, Writer writer, Context context)\n    {\n        writer.writeString(value.name());\n    }\n}\n```\n\nNotice that circular references are handled as per case basis, once it is not\nalways the desirable. For instance, you may not want to serialize lists as\nobjects references, as array is probably the most appropriate corresponding\ntype in PHP. The following example shows how to handle such cases:\n\n```java\npublic class MyCustomAdapter implements TypeAdapter\u003cCustomObject\u003e\n{\n    @Override\n    public void write(CustomObject object, Writer writer, Context context)\n    {\n        int reference = context.getReference(object);\n\n        if (reference \u003e 0) {\n            writer.writeObjectReference(reference);\n\n            return;\n        }\n\n        context.setReference(reference, object);\n\n        // Custom serialization logic\n    }\n}\n```\n\n## Change log\n\nPlease see [CHANGELOG](CHANGELOG.md) for more information what has changed \nrecently.\n\n## Contributing\n\nContributions to the package are always welcome!\n\n* Report any bugs or issues you find on the [issue tracker][issue-tracker].\n* You can grab the source code at the package's\n[Git repository][repository].\n\nPlease see [CONTRIBUTING](CONTRIBUTING.md) and [CONDUCT](CONDUCT.md) for\ndetails.\n\n## Security\n\nIf you discover any security related issues, please email\nmarcos@marcospassos.com instead of using the issue tracker.\n\n## Credits\n\n* [Marcos Passos][author-page]\n- [All Contributors][contributors-page]\n\n\n## License\n\nAll contents of this package are licensed under the [MIT license](LICENSE).\n\n```\nCopyright (c) 2018 Marcos Passos \u003cmarcos@marcospassos.com\u003e\n\nPermission is hereby granted, free of charge, to any person obtaining a\ncopy of this software and associated documentation files (the \"Software\"),\nto deal in the Software without restriction, including without limitation\nthe rights to use, copy, modify, merge, publish, distribute, sublicense,\nand/or sell copies of the Software, and to permit persons to whom the\nSoftware is furnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in\nall copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING\nFROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER\nDEALINGS IN THE SOFTWARE.\n```\n\n[maven-central-badge]: https://img.shields.io/badge/maven%20central-v0.8.0-blue.svg\n[maven-central-latest]: http://search.maven.org/#artifactdetails%7Ccom.marcospassos%7Cphpserializer%7C0.8.0%7Cjar\n[coverall-status]: https://coveralls.io/github/marcospassos/java-php-serializer\n[coverall-badge]: https://coveralls.io/repos/github/marcospassos/java-php-serializer/badge.svg\n[travis-badge]: https://travis-ci.org/marcospassos/java-php-serializer.svg?branch=master\n[travis-status]: https://travis-ci.org/marcospassos/java-php-serializer\n[java-badge]: https://img.shields.io/badge/java-7+-4c7e9f.svg\n[java]: http://java.oracle.com\n[mit-license-badge]: https://img.shields.io/badge/license-MIT-blue.svg\n[api-docs]: https://marcospassos.github.io/java-php-serializer/docs/api/latest/\n[soluble-java]: https://github.com/belgattitude/soluble-japha\n[author-page]: http://github.com/marcospassos\n[contributors-page]: https://github.com/marcospassos/java-php-serializer/graphs/contributors\n[issue-tracker]: https://github.com/marcospassos/java-php-serializer/issues\n[repository]: https://github.com/marcospassos/java-php-serializer\n[releases-page]: https://github.com/marcospassos/java-php-serializer/releases\n[latest-release]: https://github.com/marcospassos/java-php-serializer/releases/tag/0.8.0\n","funding_links":[],"categories":["Projects","项目"],"sub_categories":["Serialization","序列化"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmarcospassos%2Fjava-php-serializer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmarcospassos%2Fjava-php-serializer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmarcospassos%2Fjava-php-serializer/lists"}