PHPackages                             glook/jsonmapper - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. glook/jsonmapper

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

glook/jsonmapper
================

Map nested JSON structures onto PHP classes

3.2.1(3mo ago)0183OSL-3.0PHPPHP ^7.0 || ^8.0CI passing

Since Mar 19Pushed 3mo agoCompare

[ Source](https://github.com/glook/jsonmapper)[ Packagist](https://packagist.org/packages/glook/jsonmapper)[ RSS](/packages/glook-jsonmapper/feed)WikiDiscussions master Synced 3w ago

READMEChangelog (2)Dependencies (4)Versions (4)Used By (0)

JsonMapper - map nested JSON structures onto PHP classes
========================================================

[](#jsonmapper---map-nested-json-structures-onto-php-classes)

[![https://img.shields.io/packagist/v/glook/jsonmapper.svg?style=flat](https://camo.githubusercontent.com/b294f6593efdbf8adf9cb0118f0c2973909f91d16bb5f82a305fe20dada16291/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f676c6f6f6b2f6a736f6e6d61707065722e7376673f7374796c653d666c6174)](https://packagist.org/packages/glook/jsonmapper)[![https://img.shields.io/packagist/dm/glook/jsonmapper.svg?style=flat](https://camo.githubusercontent.com/362cc00b95bb823ce299136badb077764e9aa8987ee7f39b62a30ab672b6d8b6/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f646d2f676c6f6f6b2f6a736f6e6d61707065722e7376673f7374796c653d666c6174)](https://packagist.org/packages/glook/jsonmapper)[![](https://github.com/glook/jsonmapper/workflows/Tests/badge.svg)](https://github.com/glook/jsonmapper/actions?query=workflow%3ATests)[![https://img.shields.io/packagist/l/glook/jsonmapper.svg?style=flat](https://camo.githubusercontent.com/a65a8668fea1410a69b9dfab2411329e406587c008484e8f871430e3a9bb6974/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f676c6f6f6b2f6a736f6e6d61707065722e7376673f7374796c653d666c6174)](LICENSE)Takes data retrieved from a [JSON](http://json.org/) web service and converts them into nested object and arrays - using your own model classes.

Starting from a base object, it maps JSON data on class properties, converting them into the correct simple types or objects.

It's a bit like the native SOAP parameter mapping PHP's `SoapClient`gives you, but for JSON. Note that it does not rely on any schema, only your class definitions.

Type detection works by parsing `@var` docblock annotations of class properties, as well as type hints in setter methods. If docblock comments, or comments in general are discarded through some configuration setting like `opcache.save_comments=0`, or any other similar configuration, an exception is thrown, blocking any further operation.

You do not have to modify your model classes by adding JSON specific code; it works automatically by parsing already-existing docblocks.

Keywords: deserialization, hydration

Contents

- [Pro &amp; contra](#pro-contra)
    - [Benefits](#benefits)
    - [Drawbacks](#drawbacks)
- [Usage](#usage)
    - [Basic usage](#basic-usage)
    - [Example](#example)
    - [Letting JsonMapper create the instances for you](#letting-jsonmapper-create-the-instances-for-you)
    - [Property type documentation](#property-type-documentation)
        - [Simple type mapping](#simple-type-mapping)
        - [Custom property initialization](#custom-property-initialization)
    - [Logging](#logging)
    - [Handling invalid or missing data](#handling-invalid-or-missing-data)
        - [Unknown properties](#unknown-properties)
        - [Missing properties](#missing-properties)
        - [Passing arrays to `map()`](#passing-arrays-to-map)
    - [Handling polymorphic responses](#handling-polymorphic-responses)
- [Installation](#installation)
    - [Supported PHP Versions](#supported-php-versions)
    - [Install the Package](#install-the-package)
- [Related software](#related-software)
- [About JsonMapper](#about-jsonmapper)
    - [License](#license)
    - [Coding style](#coding-style)
    - [Authors](#authors)

[Pro &amp; contra](#id14)
-------------------------

[](#pro--contra)

### [Benefits](#id15)

[](#benefits)

- Autocompletion in IDEs
- It's easy to add comfort methods to data model classes
- Your JSON API may change, but your models can stay the same - not breaking applications that use the model classes.

### [Drawbacks](#id16)

[](#drawbacks)

- Model classes need to be written by hand

    Since JsonMapper does not rely on any schema information (e.g. from [json-schema](http://json-schema.org/)), model classes cannot be generated automatically.

[Usage](#id17)
--------------

[](#usage)

### [Basic usage](#id18)

[](#basic-usage)

1. Register an autoloader that can load [PSR-0](http://www.php-fig.org/psr/psr-0/) compatible classes.
2. Create a `JsonMapper` object instance
3. Call the `map` or `mapArray` method, depending on your data

Map a normal object:

```

```

Map an array of objects:

```
