Skip to content
This repository has been archived by the owner on Aug 8, 2021. It is now read-only.

Latest commit

 

History

History
61 lines (48 loc) · 1.27 KB

index.md

File metadata and controls

61 lines (48 loc) · 1.27 KB

JSON Object Mapper Documentation

Examples

  1. Simple Mapping
  2. Mapping with Typehints
  3. Mapping Alternative Property Names
  4. Using Transformers
  5. Convert an Mapped Object to JSON
  6. Multiple Annotations

Basic usage

JOM is totally easy to use. Simply put the annotation @MintWare\JOM\JsonField() to your class properties.

Example

Dataset:

{
    "first_name": "Pete",
    "surname": "Peterson",
    "age": 28,
    "address": {
        "street": "Mainstreet 22a",
        "zip_code": "A-12345",
        "town": "Best Town"
    }
}

Data Class:

<?php

use MintWare\JOM\JsonField;

class Person
{
    /** @JsonField(name="first_name", type="string") */
    public $firstName;
    
    /** @JsonField(name="surname", type="string") */
    public $lastname;
    
    /** @JsonField() */
    public $age;
    
    /** @JsonField(type="Some\Other\DataClass\Address") */
    public $address;
}

Map the JSON:

<?php

use  MintWare\JOM\ObjectMapper;

$mapper = new ObjectMapper();
$person = $mapper->mapJson(file_get_contents('person.json'), Person::class);