PHP Classes

File: doc/2_mapping_with_typehints.md

Recommend this page to a friend!
  Classes of Julian Finkler   JSON Object Mapper   doc/2_mapping_with_typehints.md   Download  
File: doc/2_mapping_with_typehints.md
Role: Documentation
Content type: text/markdown
Description: Documentation
Class: JSON Object Mapper
Create objects of classes mapped from JSON strings
Author: By
Last change:
Date: 5 years ago
Size: 1,243 bytes
 

Contents

Class file image Download

Mapping Data with Typehints

? Go to index

The object mapper can also map nested data. For example you have in your JSON a property which contains a address for the user:

{
    "firstname": "Pete",
    "surname": "Peterson",
    "address": {
        "street": "Mainstreet 22a",
        "zip_code": "A-12345",
        "town": "Best Town",
        "country": "Germany"
    }
}

Your user and address object looks like this

<?php

class User {
    / @JsonField() */
    public $firstname;

    / @JsonField() */
    public $surname;

    / @JsonField() */
    public $address;
}

class Address {

    / @JsonField() */
    public $street;
    
    / @JsonField() */
    public $zip_code;
    
    / @JsonField() */
    public $town;
    
    / @JsonField() */
    public $country;
}

If you map the json to this user object, the address property would become an array - but we want an instance of Address.

In this case you need to modify the annotation of the $address property and add the argument type :

class User {
    // ...

    / @JsonField(type="Address") */
    public $address;
}

With this, the $address property will be mapped as an instance of Address