PHP Classes
elePHPant
Icontem

JSON Object Mapper: Create objects of classes mapped from JSON strings

Recommend this page to a friend!
  Info   View files Documentation   View files View files (22)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2017-09-11 (9 days ago) RSS 2.0 feedNot yet rated by the usersTotal: 83 This week: 29All time: 8,844 This week: 16Up
Version License PHP version Categories
json-object-mapper 1.0.0Custom (specified...5PHP 5, Data types
Description Author

This class create objects of classes mapped from JSON strings.

It can take a given class and extracts the annotations to determine the type of its variables.

The class can parse a JSON string and extracts the values to store in the class variables according to the types defined in the annotations.

The class can also retrieve nested objects to be stored in the class variables declared as such.

  Performance   Level  
Name: Julian Finkler <contact>
Classes: 5 packages by
Country: Germany Germany
Age: 24
All time rank: 3785211 in Germany Germany
Week rank: 16 Up3 in Germany Germany Up
Innovation award
Innovation award
Nominee: 1x

Details

Travis Packagist GitHub license Packagist

JOM - JSON Object Mapper

JOM is a powerful object mapper which maps JSON Data into PHP objects.

Installation

You can install this library using composer

$ composer require mintware-de/json-object-mapper

Usage

JOM is totally easy to use. Simply put the annotation @MintWare\JOM\JsonField(name=, type=) to your class properties. name is the name of the json property, type is the field type which can also be an mapped class.

For mapping you have to create an ObjectMapper-Object and call the mapJson Method

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(name="age", type="int") */
    public $age;
    
    / @JsonField(name="address", 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);

Testing

$ phpunit

Contribute

Feel free to fork, contribute and create pull requests

  Files folder image Files  
File Role Description
Files folder imagesrc (2 files, 1 directory)
Files folder imagetests (3 files, 2 directories)
Accessible without login Plain text file .travis.yml Data Auxiliary data
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file phpunit.xml Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files  /  src  
File Role Description
Files folder imageException (4 files)
  Plain text file JsonField.php Class Class source
  Plain text file ObjectMapper.php Class Class source

  Files folder image Files  /  src  /  Exception  
File Role Description
  Plain text file ClassNotFoundException.php Class Class source
  Plain text file InvalidJsonException.php Class Class source
  Plain text file PropertyNotAccessibleException.php Class Class source
  Plain text file TypeMismatchException.php Class Class source

  Files folder image Files  /  tests  
File Role Description
Files folder imageObjects (4 files)
Files folder imageres (4 files)
  Accessible without login Plain text file autoload.php Aux. Auxiliary script
  Plain text file FailPerson.php Class Class source
  Plain text file ObjectMapperTest.php Class Class source

  Files folder image Files  /  tests  /  Objects  
File Role Description
  Plain text file Address.php Class Class source
  Plain text file Person.php Class Class source
  Plain text file PersonWithEscapedFQCN.php Class Class source
  Plain text file PersonWithMultipleAddresses.php Class Class source

  Files folder image Files  /  tests  /  res  
File Role Description
  Accessible without login Plain text file person.json Data Auxiliary data
  Accessible without login Plain text file person_multiple.json Data Auxiliary data
  Accessible without login Plain text file person_multiple_addresses.json Data Auxiliary data
  Accessible without login Plain text file person_protected.json Data Auxiliary data

 Version Control Unique User Downloads Download Rankings  
 100%
Total:83
This week:29
All time:8,844
This week:16Up