PHPackages                             hitrain/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. hitrain/jsonmapper

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

hitrain/jsonmapper
==================

Map nested JSON structures onto PHP classes

v1.3.0(8y ago)012.0k↓33.3%OSL-3.0PHPPHP &gt;=5.6

Since Jan 28Pushed 8y ago1 watchersCompare

[ Source](https://github.com/hitrain/jsonmapper)[ Packagist](https://packagist.org/packages/hitrain/jsonmapper)[ RSS](/packages/hitrain-jsonmapper/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (2)Versions (26)Used By (0)

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

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

[![https://api.travis-ci.org/cweiske/jsonmapper.png](https://camo.githubusercontent.com/dab5ca1b8a55d0a544cc42bf96b2b7f961a5a77c6c4da510fd79e5e2e48eb956/68747470733a2f2f6170692e7472617669732d63692e6f72672f63776569736b652f6a736f6e6d61707065722e706e67)](https://travis-ci.org/cweiske/jsonmapper)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. It does not rely on any schema, only your PHP class definitions.

Type detection works by parsing `@var` docblock annotations of class properties, as well as type hints in setter methods.

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)
    - [Property type mapping](#property-type-mapping)
        - [Supported type names](#supported-type-names)
        - [Different property name](#different-property-name)
        - [Getting nested property](#getting-nested-property)
        - [Simple type mapping](#simple-type-mapping)
        - [Class map](#class-map)
        - [Nullables](#nullables)
    - [Logging](#logging)
    - [Handling invalid or missing data](#handling-invalid-or-missing-data)
        - [Unknown properties](#unknown-properties)
        - [Missing properties](#missing-properties)
        - [Private properties and functions](#private-properties-and-functions)
        - [Simple types instead of objects](#simple-types-instead-of-objects)
        - [Passing arrays to `map()`](#passing-arrays-to-map)
- [Installation](#installation)
    - [via Composer](#via-composer)
    - [via PEAR](#via-pear)
- [Related software](#related-software)
- [About JsonMapper](#about-jsonmapper)
    - [License](#license)
    - [Coding style](#coding-style)
    - [Author](#author)

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

[](#pro--contra)

### [Benefits](#id17)

[](#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](#id18)

[](#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](#id19)
--------------

[](#usage)

### [Basic usage](#id20)

[](#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:

```
