PHP Classes

File: doc/index.md

Recommend this page to a friend!
  Classes of Julian Finkler   JSON Object Mapper   doc/index.md   Download  
File: doc/index.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,299 bytes
 

Contents

Class file image Download

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);