{"id":13462327,"url":"https://github.com/karriereat/json-decoder","last_synced_at":"2025-09-26T02:08:57.009Z","repository":{"id":44937691,"uuid":"89163851","full_name":"karriereat/json-decoder","owner":"karriereat","description":"JsonDecoder implementation that allows you to convert your JSON data into PHP class objects.","archived":false,"fork":false,"pushed_at":"2025-01-08T09:14:57.000Z","size":130,"stargazers_count":139,"open_issues_count":3,"forks_count":23,"subscribers_count":14,"default_branch":"master","last_synced_at":"2025-01-08T09:35:31.531Z","etag":null,"topics":["json","json-data","json-decoding","jsondecoder","php","php-json"],"latest_commit_sha":null,"homepage":"https://packagist.org/packages/karriere/json-decoder","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/karriereat.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","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}},"created_at":"2017-04-23T18:39:52.000Z","updated_at":"2024-11-12T10:11:36.000Z","dependencies_parsed_at":"2023-01-22T13:16:07.972Z","dependency_job_id":"2e5b5234-6f8c-4dac-8141-c63395132134","html_url":"https://github.com/karriereat/json-decoder","commit_stats":{"total_commits":82,"total_committers":9,"mean_commits":9.11111111111111,"dds":"0.30487804878048785","last_synced_commit":"20583a8ff3e8459f462041ca95f99ba4402541d7"},"previous_names":[],"tags_count":20,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/karriereat%2Fjson-decoder","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/karriereat%2Fjson-decoder/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/karriereat%2Fjson-decoder/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/karriereat%2Fjson-decoder/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/karriereat","download_url":"https://codeload.github.com/karriereat/json-decoder/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245382133,"owners_count":20606159,"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":["json","json-data","json-decoding","jsondecoder","php","php-json"],"created_at":"2024-07-31T12:00:44.777Z","updated_at":"2025-09-26T02:08:56.918Z","avatar_url":"https://github.com/karriereat.png","language":"PHP","readme":"\u003ca href=\"https://www.karriere.at/\" target=\"_blank\"\u003e\u003cimg width=\"200\" src=\"https://raw.githubusercontent.com/karriereat/.github/main/profile/logo.svg\"\u003e\u003c/a\u003e\n\u003cspan\u003e\u0026nbsp;\u0026nbsp;\u0026nbsp;\u003c/span\u003e\n![](https://github.com/karriereat/json-decoder/workflows/CI/badge.svg)\n[![Packagist Downloads](https://img.shields.io/packagist/dt/karriere/json-decoder.svg?style=flat-square)](https://packagist.org/packages/karriere/json-decoder)\n\n# JsonDecoder for PHP\n\nThis package contains a JsonDecoder implementation that allows you to convert your JSON data into php class objects other than `stdclass`.\n\n## Installation\n\nYou can install the package via composer\n\n```\ncomposer require karriere/json-decoder\n```\n\n## Usage\n\nBy default the Decoder will iterate over all JSON fields defined and will try to set this values on the given class type instance. This change in behavior allows the use of `json-decoder` on classes that use the **magic** `__get` and `__set` functions like Laravel's Eloquent models.\n\nIf a property equally named like the JSON field is found or a explicit `Binding` is defined for the JSON field it will be decoded into the defined place. Otherwise the property will just be created and assigned (you need the `#[AllowDynamicProperties]` attribute if you are on PHP 8.2.).\n\nThe `JsonDecoder` class can receive one parameter called `shouldAutoCase`. If set to true it will try to find the camel-case version from either snake-case or kebap-case automatically if no other binding was registered for the field and it will use an `AliasBinding` if one of the variants can be found.\n\n### A simple example\n\nAssume you have a class `Person` that looks like this:\n\n```php\n#[AllowDynamicProperties]\nclass Person\n{\n    public int $id;\n    public string $name;\n    public ?string $lastname = '';\n}\n```\n\nThe following code will transform the given JSON data into an instance of `Person`.\n\n```php\n$jsonDecoder = new JsonDecoder();\n$jsonData = '{\"id\": 1, \"name\": \"John Doe\", \"lastname\": null, \"dynamicProperty\": \"foo\"}';\n\n$person = $jsonDecoder-\u003edecode($jsonData, Person::class);\n```\n\nPlease be aware that since PHP 8.2. dynamic properties are deprecated. So if you still wish to have the ability to make\nuse of those dynamic properties you have to add the PHP attribute `AllowDynamicProperties` to your class.\nIf you are using PHP 8.2. (and greater) and don't use the `AllowDynamicProperties` attribute all dynamic properties will\nbe ignored.\n\n### More complex use case\n\nLet's extend the previous example with a property called address. This address field should contain an instance of `Address`.\nAs of version 4 you can use the introduced method `scanAndRegister` to automatically generate the transformer based on class annotations.\nSince version 5 you can also make use of the property type instead of a class annotation.\n\n```php\nclass Person\n{\n    public int $id;\n    public string $name;\n\n    /**\n     * @var Address\n     */\n    public $address;\n    \n    public ?Address $typedAddress = null;\n}\n```\n\nFor this class definition we can decode JSON data as follows:\n\n```php\n$jsonDecoder = new JsonDecoder();\n$jsonDecoder-\u003escanAndRegister(Person::class);\n\n$jsonData = '{\"id\": 1, \"name\": \"John Doe\", \"address\": {\"street\": \"Samplestreet\", \"city\": \"Samplecity\"}, , \"typedAddress\": {\"street\": \"Samplestreet\", \"city\": \"Samplecity\"}}';\n\n$person = $jsonDecoder-\u003edecode($jsonData, Person::class);\n```\n\n### Defining a Transformer\n\nIf you don't use annotations or need a more flexible `Transformer` you can also create a custom transformer. Let's look at the previous example without annotation.\n\n```php\nclass Person\n{\n    public int $id;\n    public string $name;\n    public mixed $address;\n}\n```\n\nTo be able to transform the address data into an `Address` class object you need to define a transformer for `Person`:\n\nThe transformer interface defines two methods:\n\n-   register: here you register your field, array, alias and callback bindings\n-   transforms: gives you the full qualified class name e.g.: Your\\Namespace\\Class\n\n```php\nclass PersonTransformer implements Transformer\n{\n    public function register(ClassBindings $classBindings)\n    {\n        $classBindings-\u003eregister(new FieldBinding('address', 'address', Address::class));\n    }\n\n    public function transforms()\n    {\n        return Person::class;\n    }\n}\n```\n\nAfter registering the transformer the `JsonDecoder` will use the defined transformer:\n\n```php\n$jsonDecoder = new JsonDecoder();\n$jsonDecoder-\u003eregister(new PersonTransformer());\n\n$jsonData = '{\"id\": 1, \"name\": \"John Doe\"}';\n\n$person = $jsonDecoder-\u003edecode($jsonData, Person::class);\n```\n\n### Handling private and protected properties\n\nAs of version 4 the `JsonDecoder` class will handle `private` and `protected` properties out of the box.\n\n### Transforming an array of elements\n\nIf your JSON contains an array of elements at the root level you can use the `decodeMultiple` method to transform the JSON data into an array of class type objects.\n\n```php\n$jsonDecoder = new JsonDecoder();\n\n$jsonData = '[{\"id\": 1, \"name\": \"John Doe\"}, {\"id\": 2, \"name\": \"Jane Doe\"}]';\n\n$personArray = $jsonDecoder-\u003edecodeMultiple($jsonData, Person::class);\n```\n\n## Documentation\n\n### Transformer Bindings\n\nThe following `Binding` implementations are available\n\n-   [FieldBinding](#fieldbinding)\n-   [ArrayBinding](#arraybinding)\n-   [AliasBinding](#aliasbinding)\n-   [DateTimeBinding](#datetimebinding)\n-   [CallbackBinding](#callbackbinding)\n\n#### FieldBinding\n\nDefines a JSON field to property binding for the given type.\n\n**Signature:**\n\n```php\nnew FieldBinding(string $property, ?string $jsonField = null, ?string $type = null, bool $isRequired = false);\n```\n\nThis defines a field mapping for the property `$property` to a class instance of type `$type` with data in `$jsonField`.\n\n#### ArrayBinding\n\nDefines an array field binding for the given type.\n\n**Signature:**\n\n```php\nnew ArrayBinding(string $property, ?string $jsonField = null, ?string $type = null, bool $isRequired = false);\n```\n\nThis defines a field mapping for the property `$property` to an array of class instance of type `$type` with data in `$jsonField`.\n\n#### AliasBinding\n\nDefines a JSON field to property binding.\n\n**Signature:**\n\n```php\nnew AliasBinding(string $property, ?string $jsonField = null, bool $isRequired = false);\n```\n\n#### DateTimeBinding\n\nDefines a JSON field to property binding and converts the given string to a `DateTime` instance.\n\n**Signature:**\n\n```php\nnew DateTimeBinding(string $property, ?string $jsonField = null, bool $isRequired = false, $dateTimeFormat = DateTime::ATOM);\n```\n\n#### CallbackBinding\n\nDefines a property binding that gets the callback result set as its value.\n\n**Signature:**\n\n```php\nnew CallbackBinding(string $property, private Closure $callback);\n```\n\n## License\n\nApache License 2.0 Please see [LICENSE](LICENSE) for more information.\n","funding_links":[],"categories":["Uncategorized"],"sub_categories":["Uncategorized"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkarriereat%2Fjson-decoder","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkarriereat%2Fjson-decoder","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkarriereat%2Fjson-decoder/lists"}