PHPackages                             radebatz/object-mapper - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. radebatz/object-mapper

ActiveLibrary

radebatz/object-mapper
======================

(Json) Object mapper.

v1.2.0(4y ago)127[2 PRs](https://github.com/DerManoMann/json-object-mapper/pulls)MITPHPPHP &gt;=7.2

Since Jul 24Pushed 1y ago1 watchersCompare

[ Source](https://github.com/DerManoMann/json-object-mapper)[ Packagist](https://packagist.org/packages/radebatz/object-mapper)[ Docs](http://radebatz.net/mano/)[ RSS](/packages/radebatz-object-mapper/feed)WikiDiscussions master Synced 3d ago

READMEChangelog (3)Dependencies (9)Versions (7)Used By (0)

(JSON) object mapper
====================

[](#json-object-mapper)

A simple library to deserialize JSON into (nested) PHP arrays / objects.

[![Build Status](https://github.com/DerManoMann/json-object-mapper/workflows/build/badge.svg)](https://github.com/DerManoMann/json-object-mapper/actions?query=workflow:build)[![Coverage Status](https://camo.githubusercontent.com/9b1846663e9619cb4c2138b7b67d2d68f36d786cc7bf638c3ad0fc7613b18857/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f4465724d616e6f4d616e6e2f6a736f6e2d6f626a6563742d6d61707065722f62616467652e737667)](https://coveralls.io/github/DerManoMann/json-object-mapper)[![License: MIT](https://camo.githubusercontent.com/fdf2982b9f5d7489dcf44570e714e3a15fce6253e0cc6b5aa61a075aac2ff71b/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c6963656e73652d4d49542d79656c6c6f772e737667)](https://opensource.org/licenses/MIT)

Requirements
------------

[](#requirements)

- [PHP 7.2 or higher](http://www.php.net/)

Installation
------------

[](#installation)

You can use **Composer** or simply **Download the Release**

### Composer

[](#composer)

The preferred method is via [composer](https://getcomposer.org). Follow the [installation instructions](https://getcomposer.org/doc/00-intro.md) if you do not already have composer installed.

Once composer is installed, execute the following command in your project root to install this library:

```
composer require radebatz/object-mapper
```

Usage
-----

[](#usage)

### Simple model using getXXX()/setXXX()

[](#simple-model-using-getxxxsetxxx)

```
use Radebatz\ObjectMapper\ObjectMapper;

class MyClass
{
    protected $foo;

    public function getFoo()
    {
        return $this->foo;
    }

    public function setFoo($foo)
    {
        $this->foo = $foo;
    }
}

$objectMapper = new ObjectMapper();

$json = '{"foo":"bar"}';

/** @var \MyClass $obj */
$obj = $objectMapper->map($json, \MyClass::class);

echo $obj->getFoo(); // 'bar'
```

### Value union using interface

[](#value-union-using-interface)

```
use Radebatz\ObjectMapper\ObjectMapper;
use Radebatz\ObjectMapper\ValueTypeResolverInterface;

interface ValueInterface
{
    public function getType(): string;
}

class Tree implements ValueInterface
{
    public function getType(): string
    {
        return "tree";
    }
}

class Flower implements ValueInterface
{
    public function getType(): string
    {
        return "flower";
    }
}

$objectMapper = new ObjectMapper();
$objectMapper->addValueTypeResolver(
    new class() implements ValueTypeResolverInterface {
        public function resolve($className, $json): ?string
        {
            if (is_object($json) && \ValueInterface::class == $className) {
                if (property_exists($json, 'type')) {
                    switch ($json->type) {
                        case 'tree':
                            return \Tree::class;
                        case 'flower':
                            return \Flower::class;
                    }
                }
            }

            return null;
        }
    }
);

$json = '{"type": "flower"}';

/** @var \ValueInterface $obj */
$obj = $objectMapper->map($json, \ValueInterface::class);

echo get_class($obj); // '\Flower'
```

```
    $objectMapper->addValueTypeResolver(
        new class() implements ValueTypeResolverInterface {
            public function resolve($className, $json): ?string
            {
                if (is_object($json) && PopoInterface::class == $className) {
                    if (property_exists($json, 'foo')) {
                        return AnotherPopo::class;
                    }

                    return SimplePopo::class;
                }

                return null;
            }
        }
    );

```

Configuration
-------------

[](#configuration)

The `ObjectMapper` class takes an array (map) as first constructor argument which allows to customise the behaviour when mapping data. All option names (keys) are defined as class constants.

**`OPTION_STRICT_TYPES`**
-------------------------

[](#option_strict_types)

Enforces strict type checking on build in data types (excluding `array`).

Default: `true`

**`OPTION_STRICT_COLLECTIONS`**
-------------------------------

[](#option_strict_collections)

Enforces strict type checking on arrays.

Default: `true`

**`OPTION_STRICT_NULL`**
------------------------

[](#option_strict_null)

Enforces strict check on `null` value assignments.

Default: `true`

**`OPTION_IGNORE_UNKNOWN`**
---------------------------

[](#option_ignore_unknown)

Enable/disable reporting unmapped properties (will throw `ObjectMapperException`).

Default: `true`

**`OPTION_VERIFY_REQUIRED`**
----------------------------

[](#option_verify_required)

If enabled check if all properties with a `@required` annotation have been mapped (will throw `ObjectMapperException`).

Default: `false`

**`OPTION_INSTANTIATE_REQUIRE_CTOR`**
-------------------------------------

[](#option_instantiate_require_ctor)

If disabled, object instantiation will fall back to `ReflectionClass::newInstanceWithoutConstructor()` if a regular `new $class()` fails. Futhermore, if set it will enforce a constructor argument check in case a `scalar` is deserialized into an object.

Default: `true`

**`OPTION_UNKNOWN_PROPERTY_HANDLER`**
-------------------------------------

[](#option_unknown_property_handler)

Optional callable to handle unknown/unmappable properties. Signature:

```
function ($obj, $key, $value) {}
```

The return value is epected to be either a property name or `null`.

Default: `null`

**`OPTION_VARIADIC_SETTER`**
----------------------------

[](#option_variadic_setter)

If enabled, setting list values will allow variadic parameters on models (requires `5.2 >= PropertyAccess`).

```
class Model {
  private $list = [];
  public function setList(ListModel ... $listModels) {
    $this->list = $listModels;
  }
}
```

The return value is expected to be either a property name or `null`.

Default: `false`

Testing
-------

[](#testing)

This package is inspired by the excellent [jsonmapper](https://github.com/cweiske/jsonmapper) package. In 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.

For this you run:

```
rm -rf vendor/netresearch/jsonmapper && composer install --prefer-source
./vendor/bin/phpunit -c phpunit.xml.jsonmapper
```

Not all tests pass as this library support also, for example, mapping scalar values. As it stands the result of running the tests is:

```
Tests: 104, Assertions: 249, Errors: 2, Failures: 14.  (jsonmapper 4.x)
```

The tests use a custom [`JsonMapper`](tests/JsonMapper/JsonMapper.php) class that internally uses the `ObjectMapper`.

###  Health Score

29

—

LowBetter than 59% of packages

Maintenance29

Infrequent updates — may be unmaintained

Popularity9

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity58

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 96.2% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~506 days

Total

3

Last Release

1474d ago

PHP version history (2 changes)v1.0.0PHP &gt;=7.1

v1.1.0PHP &gt;=7.2

### Community

Maintainers

![](https://www.gravatar.com/avatar/58fc8faf924fe63e767e726dd23888bda8c2b42b4b28c33243bcfa31078318c7?d=identicon)[DerManoMann](/maintainers/DerManoMann)

---

Top Contributors

[![DerManoMann](https://avatars.githubusercontent.com/u/47783?v=4)](https://github.com/DerManoMann "DerManoMann (102 commits)")[![dependabot-preview[bot]](https://avatars.githubusercontent.com/in/2141?v=4)](https://github.com/dependabot-preview[bot] "dependabot-preview[bot] (3 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (1 commits)")

---

Tags

deserializationhacktoberfesthydrationjsonphp

###  Code Quality

TestsPHPUnit

Code StylePHP CS Fixer

### Embed Badge

![Health badge](/badges/radebatz-object-mapper/health.svg)

```
[![Health](https://phpackages.com/badges/radebatz-object-mapper/health.svg)](https://phpackages.com/packages/radebatz-object-mapper)
```

###  Alternatives

[sylius/sylius

E-Commerce platform for PHP, based on Symfony framework.

8.4k5.6M651](/packages/sylius-sylius)[shopware/platform

The Shopware e-commerce core

3.3k1.5M3](/packages/shopware-platform)[web-auth/webauthn-lib

FIDO2/Webauthn Support For PHP

1195.3M72](/packages/web-auth-webauthn-lib)[shopware/core

Shopware platform is the core for all Shopware ecommerce products.

595.2M386](/packages/shopware-core)[web-auth/webauthn-framework

FIDO2/Webauthn library for PHP and Symfony Bundle.

50570.7k1](/packages/web-auth-webauthn-framework)[cognesy/instructor-php

The complete AI toolkit for PHP: unified LLM API, structured outputs, agents, and coding agent control

310107.9k1](/packages/cognesy-instructor-php)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
