{"id":16948246,"url":"https://github.com/dermanomann/json-object-mapper","last_synced_at":"2026-04-24T22:35:25.408Z","repository":{"id":32122194,"uuid":"128856600","full_name":"DerManoMann/json-object-mapper","owner":"DerManoMann","description":"Json deserializer ","archived":false,"fork":false,"pushed_at":"2024-09-09T17:16:57.000Z","size":152,"stargazers_count":1,"open_issues_count":2,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-02-04T04:43:28.353Z","etag":null,"topics":["deserialization","hacktoberfest","hydration","json","php"],"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/DerManoMann.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":"2018-04-10T01:43:02.000Z","updated_at":"2023-03-12T22:31:13.000Z","dependencies_parsed_at":"2023-01-14T20:33:56.060Z","dependency_job_id":null,"html_url":"https://github.com/DerManoMann/json-object-mapper","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/DerManoMann%2Fjson-object-mapper","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DerManoMann%2Fjson-object-mapper/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DerManoMann%2Fjson-object-mapper/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DerManoMann%2Fjson-object-mapper/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/DerManoMann","download_url":"https://codeload.github.com/DerManoMann/json-object-mapper/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":239766748,"owners_count":19693376,"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":["deserialization","hacktoberfest","hydration","json","php"],"created_at":"2024-10-13T21:50:06.961Z","updated_at":"2026-04-24T22:35:25.359Z","avatar_url":"https://github.com/DerManoMann.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# (JSON) object mapper #\nA simple library to deserialize JSON into (nested) PHP  arrays / objects.\n\n[![Build Status](https://github.com/DerManoMann/json-object-mapper/workflows/build/badge.svg)](https://github.com/DerManoMann/json-object-mapper/actions?query=workflow:build)\n[![Coverage Status](https://coveralls.io/repos/github/DerManoMann/json-object-mapper/badge.svg)](https://coveralls.io/github/DerManoMann/json-object-mapper)\n[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)\n\n## Requirements ##\n* [PHP 7.2 or higher](http://www.php.net/)\n\n## Installation ##\n\nYou can use **Composer** or simply **Download the Release**\n\n### Composer ###\n\nThe preferred method is via [composer](https://getcomposer.org). Follow the\n[installation instructions](https://getcomposer.org/doc/00-intro.md) if you do not already have\ncomposer installed.\n\nOnce composer is installed, execute the following command in your project root to install this library:\n\n```sh\ncomposer require radebatz/object-mapper\n```\n\n## Usage ##\n### Simple model using getXXX()/setXXX() ###\n```php\nuse Radebatz\\ObjectMapper\\ObjectMapper;\n\nclass MyClass \n{\n    protected $foo;\n    \n    public function getFoo()\n    {\n        return $this-\u003efoo;\n    }\n    \n    public function setFoo($foo) \n    {\n        $this-\u003efoo = $foo;\n    }\n}\n\n    \n$objectMapper = new ObjectMapper();\n\n$json = '{\"foo\":\"bar\"}';\n\n/** @var \\MyClass $obj */\n$obj = $objectMapper-\u003emap($json, \\MyClass::class);\n\necho $obj-\u003egetFoo(); // 'bar'\n```\n\n### Value union using interface ###\n```php\nuse Radebatz\\ObjectMapper\\ObjectMapper;\nuse Radebatz\\ObjectMapper\\ValueTypeResolverInterface;\n\ninterface ValueInterface\n{\n    public function getType(): string;\n}\n\nclass Tree implements ValueInterface\n{\n    public function getType(): string\n    {\n        return \"tree\";\n    }\n}\n\nclass Flower implements ValueInterface\n{\n    public function getType(): string\n    {\n        return \"flower\";\n    }\n}\n\n$objectMapper = new ObjectMapper();\n$objectMapper-\u003eaddValueTypeResolver(\n    new class() implements ValueTypeResolverInterface {\n        public function resolve($className, $json): ?string\n        {\n            if (is_object($json) \u0026\u0026 \\ValueInterface::class == $className) {\n                if (property_exists($json, 'type')) {\n                    switch ($json-\u003etype) {\n                        case 'tree':\n                            return \\Tree::class;\n                        case 'flower':\n                            return \\Flower::class;\n                    }\n                }\n            }\n\n            return null;\n        }\n    }\n);\n\n$json = '{\"type\": \"flower\"}';\n\n/** @var \\ValueInterface $obj */\n$obj = $objectMapper-\u003emap($json, \\ValueInterface::class);\n\necho get_class($obj); // '\\Flower'\n\n```\n        $objectMapper-\u003eaddValueTypeResolver(\n            new class() implements ValueTypeResolverInterface {\n                public function resolve($className, $json): ?string\n                {\n                    if (is_object($json) \u0026\u0026 PopoInterface::class == $className) {\n                        if (property_exists($json, 'foo')) {\n                            return AnotherPopo::class;\n                        }\n\n                        return SimplePopo::class;\n                    }\n\n                    return null;\n                }\n            }\n        );\n\n\n## Configuration ##\nThe `ObjectMapper` class takes an array (map) as first constructor argument which allows to customise the behaviour when mapping data.\nAll option names (keys) are defined as class constants.\n\n**`OPTION_STRICT_TYPES`**\n---\nEnforces strict type checking on build in data types (excluding `array`).\n\nDefault: `true`\n\n**`OPTION_STRICT_COLLECTIONS`**\n---\nEnforces strict type checking on arrays.\n\nDefault: `true`\n\n**`OPTION_STRICT_NULL`**\n---\nEnforces strict check on `null` value assignments.\n\nDefault: `true`\n\n**`OPTION_IGNORE_UNKNOWN`**\n---\nEnable/disable reporting unmapped properties (will throw `ObjectMapperException`).\n\nDefault: `true`\n\n**`OPTION_VERIFY_REQUIRED`**\n---\nIf enabled check if all properties with a `@required` annotation have been mapped (will throw `ObjectMapperException`).\n\nDefault: `false`\n\n**`OPTION_INSTANTIATE_REQUIRE_CTOR`**\n---\nIf disabled, object instantiation will fall back to `ReflectionClass::newInstanceWithoutConstructor()` if a regular `new $class()` fails.\nFuthermore, if set it will enforce a constructor argument check in case a `scalar` is deserialized into an object.\n\nDefault: `true`\n\n**`OPTION_UNKNOWN_PROPERTY_HANDLER`**\n---\nOptional callable to handle unknown/unmappable properties.\nSignature:\n```php\nfunction ($obj, $key, $value) {}\n```\nThe return value is epected to be either a property name or `null`.\n\nDefault: `null`\n\n**`OPTION_VARIADIC_SETTER`**\n---\nIf enabled, setting list values will allow variadic parameters on models (requires `5.2 \u003e= PropertyAccess`).\n```php\nclass Model {\n  private $list = [];\n  public function setList(ListModel ... $listModels) {\n    $this-\u003elist = $listModels;  \n  }\n}\n```\nThe return value is expected to be either a property name or `null`.\n\nDefault: `false`\n\n\n## Testing ##\nThis package is inspired by the excellent [jsonmapper](https://github.com/cweiske/jsonmapper) package.\nIn order to evaluate its features, the tests folder contains an adapter that lets you run the ```jsonmapper``` test suite agaist the ```json-object-manager``` codebase.\n\nFor this you run:\n\n```sh\nrm -rf vendor/netresearch/jsonmapper \u0026\u0026 composer install --prefer-source\n./vendor/bin/phpunit -c phpunit.xml.jsonmapper\n``` \n\nNot all tests pass as this library support also, for example, mapping scalar values. As it stands the result of running the tests is:\n\n```sh\nTests: 104, Assertions: 249, Errors: 2, Failures: 14.  (jsonmapper 4.x)\n```\n\nThe tests use a custom [`JsonMapper`](tests/JsonMapper/JsonMapper.php) class that internally uses the `ObjectMapper`.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdermanomann%2Fjson-object-mapper","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdermanomann%2Fjson-object-mapper","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdermanomann%2Fjson-object-mapper/lists"}