Download the PHP package robtimus/json-object-mapper without Composer

On this page you can find all versions of the php package robtimus/json-object-mapper. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package json-object-mapper

JSON Object Mapper

Converts objects to and from JSON (serialization and deserialization respectively). Although built upon json_encode and json_decode, it supports any type of object, not just stdClass.

Basic usage

An ObjectMapper instance has two methods:

Example:

$mapper = new ObjectMapper();
$json = $mapper->toJSON($object);
$object = $mapper->fromJSON($json, '\MyClass');

JSON Property mapping

By default, all public properties, getters (both get and is prefixes are supported) and setters are included. These need to specify their types using phpDocumenter annotations. For instance:

class Person {
    /**
     * @var string
     */
    public $firstName;

    private $lastName;

    /**
     * @return string
     */
    public function getLastName() {
        return $this->lastName;
    }

    /**
     * @param string $value
     */
    public function setLastName($value) {
        $this->lastName = $value;
    }
}

The following are supported:

Including and excluding JSON properties

Note: all of the below annotations are located in namespace Robtimus\JSON\Mapper\Annotations, and either need to be included with a use statement, or be used with their fully qualified class names.

@JSONAccessorType

A class can be annotated with @JSONAccessorType("<ACCESSOR_TYPE>") to change what is automatically included. The following are valid accessor types:

@JSONProperty

A single property, getter or setter can be explicitly included by annotating it with @JSONProperty, even if it would otherwise be excluded. This annotation can take an optional name property to override the default property name: @JSONProperty(name = "name").

If no explicit JSON property name is given it is determined as follows:

@JSONIgnore

A single property, getter or setter can be ignored by annotating it with @JSONIgnore, even if it would otherwise be included (this overrides @JSONProperty).

@JSONReadOnly

A single property can be made read-only (i.e. it will be ignored during deserialization) by annotating it with @JSONReadOnly. The same can also be achieved by making the property private and only providing a getter, or by annotating the setter with @JSONIgnore.

@JSONWriteOnly

A single property can be made write-only (i.e. it will be ignored during serialization) by annotating it with @JSONWriteOnly. The same can also be achieved by making the property private and only providing a setter, or by annotating the getter with @JSONIgnore.

@JSONAnyGetter

A single method that takes no arguments and returns an associative array, stdClass instance or an iterator can be annotated with @JSONAnyGetter. The keys and values of the return value will be added to the JSON string during serialization.

Ignoring null values and/or empty arrays

Set the omitNullValues property of an ObjectMapper instance to true to omit JSON properties with null values.

Set the omitEmptyArrays property of an ObjectMapper instance to true to omit JSON properties with empty array values.

JSON Property order

By default, JSON properties will have an undetermined order during serialization. A class can be annotated with @JSONPropertyOrder to create a specific order. This annotation can be used in two ways:

Note that this property order does not include properties returned by methods annotated with @JSONAnyGetter.

Handling unknown properties

By default, when deserializing a JSON string to an object, any JSON property that is not defined in the object will cause a JSONParseException to be thrown. This can be be prevented in one of two ways:

Custom serialization and deserialization

By default, scalar values are used as-is, and object are processed as described above. This behaviour can be changed by providing a custom serializer or deserializer.

Custom serialization

A custom serializer can be created by implementing JSONSerializer, and can be defined for a JSON property in two ways:

Custom serialization

A custom deserializer can be created by implementing JSONDeserializer, and can be defined for a JSON property in two ways:

Programmatic mapping

Besides using annotations, mappings for a class can also be created programmatically by creating a ClassDescriptor and adding properties. It can then be added to an ObjectMapper instance using its registerClass method.

The following is a mapping of annotations to the methods that replace them:

Note that all getters and setters are callables. This removes the requirement to use instance methods.

An example:

$classDescriptor = new ClassDescriptor(new ReflectionClass('\Person'));
$classDescriptor->addProperty('firstName', 'string')
    ->withGetter(array(new ReflectionProperty('\Person', 'firstName'), 'getValue'))
    ->withSetter(array(new ReflectionProperty('\Person', 'firstName'), 'setValue'));
$classDescriptor->addProperty('lastName', 'string')
    ->withGetter(array(new ReflectionMethod('\Person', 'getLastName'), 'invoke'))
    ->withSetter(array(new ReflectionMethod('\Person', 'setLastName'), 'invoke'));
$mapper->registerClass($classDescriptor);

Formatting

Similar to json_encode, the toJSON method of an ObjectMapper instance can take a bitmask of options. The same options are supported.


All versions of json-object-mapper with dependencies

PHP Build Version
Package Version
Requires php Version >=5.3.2
doctrine/annotations Version >=1.2.7
doctrine/cache Version >=1.5.4
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package robtimus/json-object-mapper contains the following files

Loading the files please wait ....