json-mapper / json-mapper
Map JSON structures to PHP classes
Fund package maintenance!
Requires
- php: ^7.4 || ^8.0
- ext-json: *
- myclabs/php-enum: ^1.7
- nikic/php-parser: ^4.13 || ^5.0
- phpdocumentor/reflection-docblock: ^5.6
- psr/log: ^1.1 || ^2.0 || ^3.0
- psr/simple-cache: ^1.0 || ^2.0 || ^3.0
- symfony/cache: ^4.4 || ^5.0 || ^6.0 || ^7.0
- symfony/polyfill-php73: ^1.18
Requires (Dev)
- guzzlehttp/guzzle: ^6.5 || ^7.0
- php-coveralls/php-coveralls: ^2.4
- phpstan/phpstan: ^0.12.14
- phpstan/phpstan-phpunit: ^0.12.17
- phpunit/phpunit: ^7.5 || ^8.5 || ^9.0
- squizlabs/php_codesniffer: ^3.5
- symfony/console: ^2.1 || ^3.0 || ^4.0 || ^5.0
- vimeo/psalm: ^4.10 || ^5.0
Suggests
- json-mapper/laravel-package: Use JsonMapper directly with Laravel
- json-mapper/symfony-bundle: Use JsonMapper directly with Symfony
Provides
None
Conflicts
None
Replaces
None
- dev-develop
- 2.25.1
- 2.25.0
- 2.24.0
- 2.23.0
- 2.22.3
- 2.22.2
- 2.22.1
- 2.22.0
- 2.21.0
- 2.20.0
- 2.19.0
- 2.18.0
- 2.17.0
- 2.16.0
- 2.15.0
- 2.14.4
- 2.14.3
- 2.14.2
- 2.14.1
- 2.14.0
- 2.13.0
- 2.12.0
- 2.11.1
- 2.11.0
- 2.10.0
- 2.9.1
- 2.9.0
- 2.8.0
- 2.7.0
- 2.6.0
- 2.5.1
- 2.5.0
- 2.4.1
- 2.4.0
- 2.3.1
- 2.3.0
- 2.2.0
- 2.1.0
- 2.0.0
- 1.4.2
- 1.4.1
- 1.4.0
- 1.3.0
- 1.2.0
- 1.1.0
- 1.0.1
- 1.0.0
- 0.3.0
- 0.2.1
- 0.2.0
- 0.1.0
- 0.0.2
- 0.0.1
- dev-docs/refresh-readme-feature-list
- dev-ci/fix-php80-cache-resolution
- dev-copilot/analyze-performance-improvements
- dev-chore/add-php85-to-build-workflow
- dev-chore/update-jetbrains-logo
- dev-main
- dev-fix/nullable-union-property-with-null-cannot-be-mapped
- dev-feat/use-reflection-docblock-for-type-parsing
- dev-chore/consolidate-docblock-functions
- dev-PHP-8.4-support
- dev-add-scorecard
- dev-Issue-176
- dev-Introduce-psalm
- dev-MixedNamespace
- dev-AddPsalmAnnotations
- dev-shared-cache
- dev-Add-benchmarks
- dev-Strengthen-codebase-using-mutation-testing
- dev-psalm-middleware
This package is auto-updated.
Last update: 2026-08-31 18:58:25 UTC
README
JsonMapper is a PHP library that allows you to map a JSON response to your PHP objects that are either annotated using doc blocks or use typed properties. For more information see the project website: https://jsonmapper.net/
Why use JsonMapper
Continuously mapping your JSON responses to your own objects becomes tedious and is error-prone. Not mentioning the tests that need to be written for said mapping.
JsonMapper has been built with the most common usages in mind. To allow for those edge cases which are not supported by default, it can easily be extended as its core has been designed using middleware.
JsonMapper supports the following features
- DocBlock annotations and typed properties
- Namespace resolving, so your models can stay organised the way you want them
- Custom constructors, including readonly properties and classes on PHP 8.1+
- Enums on PHP 8.1+
- Mapping from names that do not match your model, through PHP attributes, an explicit rename mapping, or case conversion
- Value transformation through a callback
- Interfaces and abstract classes, through factories you register
- Strict scalar casting, which throws rather than coercing a mismatched type
- A final callback once an object is filled, and debug logging of the mapping as it happens
- First-party Laravel and Symfony integrations, plus mapping straight onto Eloquent models
Installing JsonMapper
The installation of JsonMapper can easily be done with Composer
$ composer require json-mapper/json-mapper
The example shown above assumes that composer is on your $PATH.
How to use JsonMapper
Given the following class definition
class User { /** @var string */ private $name; public function getName(): string { return $this->name; } public function setName(string $name): void { $this->name = $name; } }
Combined with the following JsonMapper code as part of your application
$mapper = (new \JsonMapper\JsonMapperFactory())->default(); $user = $mapper->mapToClassFromString('{ "name": "John Doe" }', User::class); echo $user->getName(); // "John Doe"
The property is private, so JsonMapper fills it through setName(). A non-public property with no
matching setter raises a RuntimeException, so your model keeps its encapsulation either way.
Customizing JsonMapper
Writing your own middleware has been made as easy as possible with an AbstractMiddleware that can be extended with the functionality
you need for your project.
$mapper = (new \JsonMapper\JsonMapperFactory())->bestFit(); $mapper->push(new class extends \JsonMapper\Middleware\AbstractMiddleware { public function handle( \stdClass $json, \JsonMapper\Wrapper\ObjectWrapper $object, \JsonMapper\ValueObjects\PropertyMap $map, \JsonMapper\JsonMapperInterface $mapper ): void { /* Custom logic here */ } });
Contributing
Please refer to CONTRIBUTING.md for information on how to contribute to JsonMapper.
List of Contributors
Thanks to everyone who has contributed to JsonMapper! You can find a detailed list of people that contributed to JsonMapper on GitHub.
Sponsoring
This project is sponsored by JetBrains providing a license for PhpStorm to continue building on JsonMapper.
License
The MIT License (MIT). Please see License File for more information.