Download the PHP package andreamk/jsonserialize without Composer

On this page you can find all versions of the php package andreamk/jsonserialize. 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 jsonserialize

JsonSerialize

PHPStan PSR12 PHPUnit 5.4,5.6 PHPUnit 7, 8

This library combines the features of the native PHP serialization with the JSON portability, in particular it allows to encode with JSON also protected and private properties of an object. When defined in classes, the magic methods __sleep , __serialize, __wakeup and __unserialize are used in the same way as they are used in serialization.

Values serialized and unserialized with this library retain their type, so arrays, associative arrays, and objects will retain their type and class.

Requirements

PHP 5.4+

Installation

Via Composer

It's possibile include the library either using the composer autoloader or using the library autoloader

Basic usage


The serialize function converts any value in JSON like the json_encode function with the difference that in the JSON will be present also the private and protected properties of the objects in addition to the reference to the corresponding class.

Takes a JSON encoded string and converts it into a PHP variable like json_decode. In case the class to which the object refers is not defined then the object will be instantiated as stdClass and all properties become public.

Note: When an object is unserialized it is instantiated without calling the constructor exactly like the unserialize function of PHP. If the variable being unserialized is an object, after successfully reconstructing the object PHP will automatically attempt to call __wakeup() method (if exists).

Advanced usage

Method __sleep

Amk/JsonSerialize serialize functions checks if the class has a function with the magic name __sleep(). If so, that function is executed prior to any serialization. It can clean up the object and is supposed to return an array with the names of all variables of that object that should be serialized. If the method doesn't return an array an exception is thrown.

The intended use of __sleep() is to commit pending data or perform similar cleanup tasks. Also, the function is useful if a very large objects doesn't need to be saved completely.

Method __wakeup

Amk/JsonSerialize unserialize functions checks for the presence of a function with the magic name __wakeup(). If present, this function can reconstruct any resources that the object may have.

The intended use of __wakeup() is to reestablish any database connections that may have been lost during serialization and perform other reinitialization tasks.

Method __serialize

Amk/JsonSerialize serialize functions checks if the class has a function with the magic name __serialize(). If so, that function is executed prior to any serialization. It must construct and return an associative array of key/value pairs that represent the serialized form of the object. If no array is returned a Exception will be thrown.

Note: If both serialize() and sleep() are defined in the same object, only serialize() will be called. sleep() will be ignored.

Method __unserialize

Amk/JsonSerialize unserialize functions checks for the presence of a function with the magic name unserialize(). If present, this function will be passed the restored array that was returned from serialize(). It may then restore the properties of the object from that array as appropriate.

Note: If both unserialize() and wakeup() are defined in the same object, only unserialize() will be called. wakeup() will be ignored.

AbstractJsonSerializable class

Extending the AbstractJsonSerializable class that implements the JsonSerializable interface allows to use the normal json_encode function of PHP obtaining for the object that extends this class the same result that you would get using JsonSerialize::serialize

Flags

- JSON_SKIP_CLASS_NAME

In some circumstances it can be useful to serialize an object in JSON without exposing the class. For example if we want to send the contents of an object to a browser via an AJAX call. In these cases we can use the JSON_SKIP_CLASS_NAME flag in addition to the normal flags of the json_encode function.

- JSON_SKIP_MAGIC_METHODS

When this flag is on, the sleep and serialize methods of the class are ignored.

- JSON_SKIP_SANITIZE

By default serialization applied string sanitization in case json_encode fails due to invalid characters. Activating this flag turns sanitization off.

Method serializeToData

In some circumstances, it is useful to be able to process the data structure before transforming it into JSON. Using the serializeToData method, you get the value that would be passed to the json_encode function with the serialize method

Method unserializeToObj

In some circumstances it may be useful to unserialize JSON data in an already instantiated object. For example if we are working on a serialized JSON with the JSON_SKIP_CLASS_NAME flag.

In this case we don't have the information about the reference class so using the normal unserialize function the result would be an associative array. Using unserializeToObj method we force the use of the object passed by parameter.

Method unserializeWithMap

Deserialization with mapping is a very powerful method of mapping properties to change the type of in deserialization. The classic use is to force object deserialization of what would normally be an associative array. In addition to nuti PHP native types there are special types in which you can indicate the class of an object and also reference another proprity for offects with recursive references

See the Mapping section for more information

Unserialize Mapping

The mapping is defined through the JsonUnserializeMap class. It can be initialized by passing an array of items to the constructor, and the list of items can be manipulated later with the methods addProp, removeProp, resetMap

A mapping object consists of the key (property identifier) value (property type) pair. Please note that a mapping does not require the definition of all the properties of a structure but only the properties that need to be forced to a specific type

Property identifier

Some examples

Property type

notes: When defining a class type cl: it is initialized by also executing the wakeup and unserialize methods if they exist When defining a reference rf: all child values of this object in the json are ignored and since the class is already initialized no wakeup or __unserialie methods are executed

How works

The serialize and unserialize methods work on standard JSON and can be read by any function that writes/reads json files. In particular if value is a scalar value or an array there is no difference in the result using standard functions json_encode and json_decode.

If you use these functions with objects, in addition to the public and private properties, the class to which the data belongs is also added.

This code

It will generate this JSON

When deserializing a json if the data is of type array and the key AbstractJsonSerializeObjData::CLASS_KEY_FOR_JSONSERIALIZE (CL-=_-=) is present this array is converted into an object.

If the class exists the instantiated object will belong to the defined class, otherwise it will be an stdClass object.

In case the object is of type stdClass then all items of the array will become public properties otherwise every property defined in the class and present in the array will be updated with the value of the array.

It is very important to understand the difference in this functionality if you work with projects that save serialized classes that change over time.

Suppose we have a Wordpress plugin with a Settings class that describes the settings of our plugin. It is very likely that over time properties of this class will be added or removed as the plugin evolves. In this case we can have serialized properties that do not exist in the class and properties that exist in the class but have not been serialized.

Deserialization handles this by discarding all properties that are in the JSON but not defined in the class and leaving the default of properties that are defined in the class but do not exist in the JSON, unless it is the stdClass class where all JSON values are assigned.

So in the case we have this json

and class defined in this way

the result will be


All versions of jsonserialize with dependencies

PHP Build Version
Package Version
Requires php Version >=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 andreamk/jsonserialize contains the following files

Loading the files please wait ....