{"id":18550274,"url":"https://github.com/xp-forge/marshalling","last_synced_at":"2025-07-02T19:32:53.512Z","repository":{"id":55102837,"uuid":"146600182","full_name":"xp-forge/marshalling","owner":"xp-forge","description":"Marshalling","archived":false,"fork":false,"pushed_at":"2024-12-29T16:01:06.000Z","size":72,"stargazers_count":0,"open_issues_count":1,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-05-28T05:50:51.779Z","etag":null,"topics":["data","marshalling","object-mapping","xp-framework"],"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/xp-forge.png","metadata":{"files":{"readme":"README.md","changelog":"ChangeLog.md","contributing":null,"funding":null,"license":null,"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":"2018-08-29T12:58:41.000Z","updated_at":"2024-12-29T16:00:33.000Z","dependencies_parsed_at":"2024-02-04T12:01:13.591Z","dependency_job_id":"f247553e-0390-4c39-8df9-35c72dcba7c1","html_url":"https://github.com/xp-forge/marshalling","commit_stats":null,"previous_names":[],"tags_count":13,"template":false,"template_full_name":null,"purl":"pkg:github/xp-forge/marshalling","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xp-forge%2Fmarshalling","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xp-forge%2Fmarshalling/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xp-forge%2Fmarshalling/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xp-forge%2Fmarshalling/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/xp-forge","download_url":"https://codeload.github.com/xp-forge/marshalling/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xp-forge%2Fmarshalling/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":262490431,"owners_count":23319289,"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":["data","marshalling","object-mapping","xp-framework"],"created_at":"2024-11-06T21:04:04.105Z","updated_at":"2025-07-02T19:32:53.473Z","avatar_url":"https://github.com/xp-forge.png","language":"PHP","readme":"Marshalling\n========================================================================\n\n[![Build status on GitHub](https://github.com/xp-forge/marshalling/workflows/Tests/badge.svg)](https://github.com/xp-forge/marshalling/actions)\n[![XP Framework Module](https://raw.githubusercontent.com/xp-framework/web/master/static/xp-framework-badge.png)](https://github.com/xp-framework/core)\n[![BSD Licence](https://raw.githubusercontent.com/xp-framework/web/master/static/licence-bsd.png)](https://github.com/xp-framework/core/blob/master/LICENCE.md)\n[![Requires PHP 7.0+](https://raw.githubusercontent.com/xp-framework/web/master/static/php-7_0plus.svg)](http://php.net/)\n[![Supports PHP 8.0+](https://raw.githubusercontent.com/xp-framework/web/master/static/php-8_0plus.svg)](http://php.net/)\n[![Latest Stable Version](https://poser.pugx.org/xp-forge/marshalling/version.svg)](https://packagist.org/packages/xp-forge/marshalling)\n\nMarshalling converts objects to maps and vice versa.\n\nExample\n-------\nAll primitives, arrays and maps thereof are yielded as-is:\n\n```php\nuse util\\data\\Marshalling;\n\n$m= new Marshalling();\n$m-\u003emarshal('Test');            // \"Test\"\n$m-\u003emarshal([1, 2, 3]);         // [1, 2, 3]\n$m-\u003emarshal(['admin' =\u003e true]); // [\"admin\" =\u003e true]\n```\n\nValue objects are marshalled using field =\u003e getter lookups; supporting both *method named for field* and *get[Field]()* conventions.\n\n```php\nuse util\\data\\Marshalling;\n\nclass Person {\n  private $name, $age;\n\n  public function __construct(string $name, int $age) {\n    $this-\u003ename= $name;\n    $this-\u003eage= $age;\n  }\n\n  public function name(): string { return $this-\u003ename; }\n  public function age(): int { return $this-\u003eage; }\n}\n\n$m= new Marshalling();\n$m-\u003emarshal(new Person('...', 42)); // [\"name\" =\u003e \"...\", \"age\" =\u003e 42]\n```\n\nWhen unmarshalling from maps, pass the type as second parameter. Objects are created without invoking the constructor, and by either setting the fields directly or by using the *set[Field]()* convention.\n\n```php\nuse util\\data\\Marshalling;\n\n$m= new Marshalling();\n$person= $m-\u003eunmarshal(['name' =\u003e '...', 'age' =\u003e 42], Person::class);\n```\n\nTypes from the `util` package are handled by default:\n\n```php\nuse util\\data\\Marshalling;\nuse util\\{Date, Bytes, Money, UUID};\n\n$m= new Marshalling();\n$m-\u003emarshal(Date::now());                   // \"2018-08-29T10:40:49+0200\" (ISO 8601)\n$m-\u003emarshal(new Bytes(\"\\x50\\x4b\\x03\\x04\")); // \"UEsDBA==\" (base64)\n$m-\u003emarshal(new Money(12.99, $currency));   // [\"amount\" =\u003e 12.99, \"currency\" =\u003e \"EUR\"]\n$m-\u003emarshal(UUID::randomUUID());            // [\"value\" =\u003e \"a2b15f7b-f6e0-45fa-9d7f-703fda05d4ac\"]\n```\n\nCustom logic\n------------\nTo supply a function for marshalling and unmarshalling, use *mapping()* and *resolving()*:\n\n```php\nuse util\\data\\Marshalling;\n\nclass User {\n  public function __construct(public int $id, public string $name) { }\n}\n\n$marshalling= (new Marshalling())\n  -\u003emapping(User::class, fn($user) =\u003e $user-\u003eid)\n  -\u003eresolving(User::class, fn($value) =\u003e new User($value, posix_getpwuid($value)['name'])\n;\n$value= $marshalling-\u003emarshal(new User(0, 'root'));\n// 0\n\n$user= $marshalling-\u003eunmarshal($value, User::class);\n// User(id: 0, name: 'root')\n```\n\nSee also\n--------\n* https://stackoverflow.com/questions/1443158/binary-data-in-json-string-something-better-than-base64\n* https://stackoverflow.com/questions/30249406/what-is-the-standard-for-formatting-currency-values-in-json","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxp-forge%2Fmarshalling","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fxp-forge%2Fmarshalling","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxp-forge%2Fmarshalling/lists"}