Coder Social home page Coder Social logo

jsonmapper's Introduction

JsonMapper - map nested JSON structures onto PHP classes

https://api.travis-ci.org/netresearch/jsonmapper.png

Takes data retrieved from a JSON 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. Note that it does not rely on any schema, only your class definitions.

Type detection works by parsing @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.

  • 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.
  • Model classes need to be written by hand

    Since JsonMapper does not rely on any schema information (e.g. from json-schema), model classes cannot be generated automatically.

  1. Register an autoloader that can load PSR-0 compatible classes.
  2. Create a JsonMapper object instance
  3. Call the map or mapArray method, depending on your data

Map a normal object:

<?php
require 'autoload.php';
$mapper = new JsonMapper();
$contactObject = $mapper->map($jsonContact, new Contact());
?>

Map an array of objects:

<?php
require 'autoload.php';
$mapper = new JsonMapper();
$contactsArray = $mapper->mapArray(
    $jsonContacts, new ArrayObject(), 'Contact'
);
?>

JSON from a address book web service:

{
    'name':'Sheldon Cooper',
    'address': {
        'street': '2311 N. Los Robles Avenue',
        'city': 'Pasadena'
    }
}

Your local Contact class:

<?php
class Contact
{
    /**
     * Full name
     * @var string
     */
    public $name;

    /**
     * @var Address
     */
    public $address;
}
?>

Your local Address class:

<?php
class Address
{
    public $street;
    public $city;

    public function getGeoCoords()
    {
        //do something with the $street and $city
    }
}
?>

Your application code:

<?php
$json = json_decode(file_get_contents('http://example.org/bigbang.json'));
$mapper = new JsonMapper();
$contact = $mapper->map($json, new Contact());

echo "Geo coordinates for " . $contact->name . ": "
    . var_export($contact->address->getGeoCoords(), true);
?>

JsonMapper uses several sources to detect the correct type of a property:

  1. @var $type docblock annotation of class properties:

    /**
     * @var \my\application\model\Contact
     */
    public $person;
    
  2. If the property does not exist, the setter method (set + ucfirst($propertyname)) is inspected

    1. If it has a type hint in the method signature, this type used:

      public function setPerson(Contact $person) {...}
      
    2. The method's docblock is inspected for @param $type annotations:

      /**
       * @param Contact $person Main contact for this application
       */
      public function setPerson($person) {...}
      
  3. If all fails, the plain JSON data is set to the property

Supported type names:

  • Simple types:
    • string
    • bool, boolean
    • int, integer
    • float
    • array
    • object
  • Class names, with and without namespaces
  • Arrays of simple types and class names:
    • int[]
    • Contact[]
  • ArrayObjects of simple types and class names:
    • ContactList[Contact]
    • NumberList[int]
  • Nullable types:
    • int|null - will be null if the value in JSON is null, otherwise it will be an integer

ArrayObjects and extending classes are treated as arrays.

See phpdoc's type documentation for more information.

When an object shall be created but the JSON contains a simple type only (e.g. string, float, boolean), this value is passed to the classes' constructor. Example:

PHP code:

/**
 * @var DateTime
 */
public $date;

JSON:

{"date":"2014-05-15"}

This will result in new DateTime('2014-05-15') being called.

JsonMapper's setLogger() method supports all PSR-3 compatible logger instances.

Events that get logged:

  • JSON data contain a key, but the class does not have a property or setter method for it.
  • Neither setter nor property can be set from outside because they are protected or private

During development, APIs often change. To get notified about such changes, JsonMapper may throw exceptions in case of either missing or yet unknown data.

When JsonMapper sees properties in the JSON data that are not defined in the PHP class, you can let it throw an exception by setting $bExceptionOnUndefinedProperty:

$jm = new JsonMapper();
$jm->bExceptionOnUndefinedProperty = true;
$jm->map(...);

Properties in your PHP classes can be marked as "required" by putting @required in their docblock:

/**
 * @var string
 * @required
 */
public $someDatum;

When the JSON data do not contain this property, JsonMapper will throw an exception when $bExceptionOnMissingData is activated:

$jm = new JsonMapper();
$jm->bExceptionOnMissingData = true;
$jm->map(...);
$ pear channel-discover pear.nrdev.de
$ pear install nr/jsonmapper-alpha
$ composer require netresearch/jsonmapper

JsonMapper is licensed under the OSL 3.0.

JsonMapper follows the PEAR Coding Standards.

Christian Weiske, Netresearch GmbH & Co KG

jsonmapper's People

Contributors

ammmze avatar barryvdh avatar cweiske avatar cybottm avatar darkgaro avatar itscaro avatar jalle19 avatar loco8878 avatar philsturgeon avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.