2017 © Pedro Peláez
 

library cartographer

A super-simple library to map JSON documents to objects, similar to Java's Jackson

image

xenolope/cartographer

A super-simple library to map JSON documents to objects, similar to Java's Jackson

  • Wednesday, January 28, 2015
  • by jonjomckay
  • Repository
  • 3 Watchers
  • 16 Stars
  • 38 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 6 Versions
  • 0 % Grown

The README.md

Cartographer

Build Status, (*1)

Cartographer is a super-simple library for deserializing JSON into POPOs, similar to FasterXML's jackson-databind package for Java., (*2)

Installation

The library can be installed with Composer, by including the following in your composer.json:, (*3)

{
    "require": {
        "xenolope/cartographer": "~0.5"
    }
}

Usage

POPOs

Your POPOs must have a property and corresponding setter, with either the property having a @var ClassName docblock, or the setter having a type hint., (*4)

Setters are called directly, and properties are never touched, even if they're declared public (though this might be added in a later version)., (*5)

Simple values are converted to the types given in their @var docblocks (using settype()), objects are created based on the class specified in the @var docblocks, and arrays of objects are also created, and can be specified with the @var ClassName[] notation., (*6)

class Contact
{

    /**
     * @var string
     */
    private $name;

    /**
     * @var Address
     */
    private $address;

    /**
     * Note, this property doesn't have a @var docblock, but the corresponding setter
     * below *does* have a type hint
     */
    private $secondaryAddress;

    /**
     * @var Note[]
     */
    private $notes;

    /**
     * @param string $name
     */
    public function setName($name)
    {
        $this->name = $name;
    }

    /**
     * @param Address $address
     */
    public function setAddress($address)
    {
        $this->address = $address;
    }

    /**
     * @param Address $secondaryAddress
     */
    public function setAddress(Address $secondaryAddress)
    {
        $this->secondaryAddress = $secondaryAddress;
    }

    /**
     * @param Note[] $notes
     */
    public function setNotes($notes)
    {
        $this->notes = $notes;
    }
}

Mapping

// Create a new instance of the Mapper
$mapper = new \Xenolope\Cartographer\Mapper();

// Map a JSON string to a POPO

// PHP 5.4
$object = $mapper->mapString(
    '{"name":"Liz Lemon","address":{"street":"168 Riverside Dr.","city":"New York"}}',
    'Vendor\Package\Entity\Contact'
);

// PHP >=5.5
$object = $mapper->mapString(
    '{"name":"Liz Lemon","address":{"street":"168 Riverside Dr.","city":"New York"}}',
    Contact::class
);

// Map an already decoded (to array) JSON document to a POPO

// This might happen automatically in your Request class, for example
$jsonDocument = json_decode(
    '{"name":"Liz Lemon","address":{"street":"168 Riverside Dr.","city":"New York"}}',
    true
);

// PHP 5.4
$object = $mapper->map($jsonDocument, 'Vendor\Package\Entity\Contact');

// PHP >= 5.5
$object = $mapper->map(
    '{"name":"Liz Lemon","address":{"street":"168 Riverside Dr.","city":"New York"}}',
    Contact::class
);

Roadmap

  • Add custom property mapping, for when JSON properties don't match with POPO properties
  • Maybe add in support for serializing a POPO to JSON

Thanks

This library was inspired by:, (*7)

License

Cartographer is released under the MIT License; please see LICENSE for more information., (*8)

The Versions

28/01 2015

dev-master

9999999-dev

A super-simple library to map JSON documents to objects, similar to Java's Jackson

  Sources   Download

MIT

The Requires

 

The Development Requires

28/01 2015

dev-develop

dev-develop

A super-simple library to map JSON documents to objects, similar to Java's Jackson

  Sources   Download

MIT

The Requires

 

The Development Requires

28/01 2015

v0.6.1

0.6.1.0

A super-simple library to map JSON documents to objects, similar to Java's Jackson

  Sources   Download

MIT

The Requires

 

The Development Requires

20/12 2014

v0.6

0.6.0.0

A super-simple library to map JSON documents to objects, similar to Java's Jackson

  Sources   Download

MIT

The Requires

 

The Development Requires

20/12 2014

v0.5.1

0.5.1.0

A super-simple library to map JSON documents to objects, similar to Java's Jackson

  Sources   Download

MIT

The Requires

 

The Development Requires

20/12 2014

v0.5

0.5.0.0

A super-simple library to map JSON documents to objects, similar to Java's Jackson

  Sources   Download

MIT

The Requires

 

The Development Requires