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

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

pocketmine/netresearch-jsonmapper
=================================

Fork of netresearch/jsonmapper with patches needed by pocketmine/pocketmine-mp

v5.0.999(4mo ago)0194.1k[3 PRs](https://github.com/pmmp/netresearch-jsonmapper/pulls)2OSL-3.0PHPPHP &gt;=7.1CI passing

Since Jan 28Pushed 2mo agoCompare

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

READMEChangelogDependencies (2)Versions (50)Used By (2)

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

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

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 type declarations and `@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.

This library has no dependencies.

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)
        - [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)
    - [Post-mapping callback](#post-mapping-callback)
- [Installation](#id10)
- [Related software](#related-software)
- [About JsonMapper](#about-jsonmapper)
    - [License](#license)
    - [Coding style](#coding-style)
    - [Author](#author)

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

[](#pro--contra)

### [Benefits](#id24)

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

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

[](#usage)

### [Basic usage](#id27)

[](#basic-usage)

1. [Install](#installation) `netresearch/jsonmapper` with composer
2. Create a `JsonMapper` object instance
3. Call the `map` or `mapArray` method, depending on your data

Map a normal object:

```
