{"id":15028306,"url":"https://github.com/tnapf/jsonmapper","last_synced_at":"2025-08-03T11:03:52.207Z","repository":{"id":168260908,"uuid":"643928681","full_name":"tnapf/JsonMapper","owner":"tnapf","description":"A JSON Mapper","archived":false,"fork":false,"pushed_at":"2023-09-14T19:56:26.000Z","size":101,"stargazers_count":0,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-04-24T03:23:53.171Z","etag":null,"topics":["composer","jsonmapping","php","php82"],"latest_commit_sha":null,"homepage":"","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/tnapf.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-05-22T12:56:07.000Z","updated_at":"2023-09-13T21:02:51.000Z","dependencies_parsed_at":null,"dependency_job_id":"0d8e5edd-a23b-456d-8d85-b501dde6052d","html_url":"https://github.com/tnapf/JsonMapper","commit_stats":null,"previous_names":["tnapf/jsonmapper"],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tnapf%2FJsonMapper","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tnapf%2FJsonMapper/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tnapf%2FJsonMapper/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tnapf%2FJsonMapper/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tnapf","download_url":"https://codeload.github.com/tnapf/JsonMapper/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243338306,"owners_count":20275468,"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":["composer","jsonmapping","php","php82"],"created_at":"2024-09-24T20:07:59.911Z","updated_at":"2025-03-13T04:26:28.410Z","avatar_url":"https://github.com/tnapf.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Tnapf/JsonMapper\n\nA JSON Mapper\n\n# Installation\n\n```bash\ncomposer require tnapf/json-mapper\n```\n\n# Supported Types\n\n* Enums\n* Primitives\n* Objects\n* Arrays\n* Custom Types (via CallbackType)\n\n# Usage\n\n## Instantiate Mapper\n\n```php\nuse Tnapf\\JsonMapper\\Mapper;\n\n$mapper = new Mapper;\n```\n\n## Create an abstraction class\n\n```php\nclass User\n{\n    public int $id;\n    public string $username;\n    public string $password;\n}\n```\n\n## Convert JSON to array and map the class\n```php\n$user = [\n    \"id\" =\u003e 1,\n    \"username\" =\u003e \":username:\",\n    \"password\" =\u003e \":password:\"\n];\n\n$mappedUser = $mapper-\u003emap($user, User::class);\n```\n\n## Typing with Attributes\n\n### Primitive Types\n\nFor primitive types you can use the `PrimitiveArray` attribute on the property\n\n```php\nuse Tnapf\\JsonMapper\\Attributes\\PrimitiveType;\nuse Tnapf\\JsonMapper\\Attributes\\PrimitiveArrayType;\n\nclass User\n{\n    public int $id;\n    public string $username;\n    public string $password;\n    \n    #[PrimitiveArrayType(name: 'roles', type: PrimitiveType::STRING)]\n    public array $roles;\n}\n```\n\n```php\n// what the new item will look like\n$user = [\n    // ...\n    'roles' =\u003e [':name:', ':name:', ':name:']\n];\n```\n\n### Object Types\n\nIf you want the array to have a class, you can use the ObjectArrayType attribute\n\n```php\nuse Tnapf\\JsonMapper\\Attributes\\ObjectArrayType;\n\nclass User\n{\n    public int $id;\n    public string $username;\n    public string $password;\n    \n    #[ObjectArrayType(name: 'roles', type: Role::class)]\n    public array $roles;\n}\n\nclass Role {\n    public int $id;\n    public string $name;\n}\n```\n```php\n// what the updated item will look like\n$user = [\n    // ...\n    'roles' =\u003e [\n        [\n            'id' =\u003e 1,\n            'name' =\u003e ':name:'\n        ],\n        [\n            'id' =\u003e 2,\n            'name' =\u003e ':name:'\n        ],\n        [\n            'id' =\u003e 3,\n            'name' =\u003e ':name:'\n        ]\n    ]\n];\n```\n\n### Array Enumeration Types\n\n```php\nuse Tnapf\\JsonMapper\\Attributes\\EnumerationArrayType;\n\nclass User {\n    public int $id;\n    public string $username;\n    public string $password;\n    \n    #[EnumerationArrayType(name: 'roles', type: Role::class))]\n    public array $roles;\n}\n\nenum Role: int\n{\n    case USER = 1;\n    case ADMIN = 2;\n    case OWNER = 3;\n}\n```\n```php\n// what the updated item will look like\n$user = [\n    // ...\n    'roles' =\u003e [1, 3]\n];\n```\n\n### CallbackType\n   \nIf you need to do something specific to map a class, you can extend the CallbackType class to create a custom type\n\n```php\nuse Attribute;\nuse ReflectionException;\nuse Tnapf\\JsonMapper\\Attributes\\MappableType;\nuse Tnapf\\JsonMapper\\MapperException;\nuse Tnapf\\JsonMapper\\MapperInterface;\n\n#[Attribute(Attribute::TARGET_PROPERTY)]\nclass FriendsType extends MappableType\n{\n    public function isType(mixed $data): bool\n    {\n        if (!is_array($data)) {\n            return false;\n        }\n\n        foreach ($data as $item) {\n            if (!$item instanceof User) {\n                return false;\n            }\n        }\n\n        return true;\n    }\n\n    /**\n     * @throws ReflectionException\n     * @throws MapperException\n     */\n    public function map(mixed $data, MapperInterface $mapper): mixed\n    {\n        $friends = [];\n\n        foreach ($data as $item) {\n            $friend = $mapper-\u003emap(User::class, $item);\n            $friends[$friend-\u003eusername] = $friend;\n        }\n\n        return $friends;\n    }\n}\n\nclass User {\n    public string $username;\n    public string $password;\n    \n    #[FriendsType(name: 'friends', nullable: true)]\n    public array $friends;\n}\n```\n```php\n// what the updated item will look like\n$user = [\n    // ...\n    'friends' =\u003e [\n        [\n            'username' =\u003e ':username:',\n            'password' =\u003e ':password:'\n        ],\n        [\n            'username' =\u003e ':username:',\n            'password' =\u003e ':password:'\n        ],\n        [\n            'username' =\u003e ':username:',\n            'password' =\u003e ':password:'\n        ]\n    ]\n];\n```\n\n## Property Case Conversion\n\nSince the common json naming convention is `snake_case` and PHP's is `camelCase` you can use apply an attribute to the class to have the `snake_case` json properties routed to your `camelCase` properties.\n\n```php\nuse Tnapf\\JsonMapper\\Attributes\\SnakeToCamelCase;\nuse Tnapf\\JsonMapper\\Attributes\\PrimitiveType;\nuse Tnapf\\JsonMapper\\Attributes\\PrimitiveArrayType;\n\n#[SnakeToCamelCase]\nclass User\n{\n    public int $id;\n    public string $username;\n    public string $password;\n    \n    #[PrimitiveArrayType(name: 'all_roles', type: PrimitiveType::STRING)]\n    public array $allRoles;\n}\n```\n\nWhile allRoles is `camelCase` in the class you can see the JSON below uses `snake_case`\n\n```json\n{\n    \"id\": 1,\n    \"username\": \":username:\",\n    \"password\": \"1234\",\n    \"all_roles\": [\n        \"admin\",\n        \"user\"\n    ]\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftnapf%2Fjsonmapper","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftnapf%2Fjsonmapper","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftnapf%2Fjsonmapper/lists"}