Download the PHP package luverelle/pson without Composer
On this page you can find all versions of the php package luverelle/pson. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download luverelle/pson
More information about luverelle/pson
Files in luverelle/pson
Package pson
Short Description PHP library to convert data into JSON and vice versa
License Apache-2.0
Informations about the package pson
PSON
A powerful PHP library to convert objects into JSON and vice versa using PHP attributes
Features:
- Easy-to-use
- Extensible
- High performance
- Fully customization
Getting started
Attributes
PSON gets all object properties (including parent if exists) and if some properties has JsonProperty attribute,
they will be converted into JSON.
Convert object into JSON:
- Create an
objectinstance - Call
PSON::toJsonArray(object)method - Call
json_encodewith your flags
Example - convert User object into JSON:
The result will be:
Convert JSON into object:
- Call
json_decodewithassociative: true - If you have already instantiated your object, use
PSON::fromJsonArray. It returns nothing because PHP by default passing all objects by reference - Otherwise, use
PSON::fromJsonArrayAsClassproviding the class name of that object that you want to get (return).
Example - convert JSON into User (previous example but versa):
The result will be:
Nested objects
If you have an object that contains another objects in properties, and you want to convert all together into JSON,
just add JsonProperty attribute. Properties with that attribute will be converted into JSON.
It also works versa: JSON -> object.
Example:
The result will be:
Arrays of objects
If you want to add a JSON convertable array of objects(like User[]),
ensure to add what type of object you want to convert in JsonProperty attribute like this:
TODO list:
- Add more tests
- Add more customizable stuff
Requirements:
- PHP 8.0 or higher
- JSON PHP extension
All versions of pson with dependencies
ext-json Version *