mikangali / pson
Convert an Object into its JSON representation and vice versa
dev-master
2013-11-15 11:05 UTC
Requires
- php: >=5.4.0
This package is not auto-updated.
Last update: 2025-09-13 18:39:24 UTC
README
A PHP library to convert an Object into its JSON representation and vice versa. Use Addendum PHP Lib. The librairy try to do the same job that Java Gson Library.
Version : 1.0 Require : PHP 5.4 +
Features
- Convert object to json and Json to object
- Convert PHP object containning private fiels
- Use annotation to serialize/deserialize complex fileds
- PsonBuilder class to set Pson options (serializeNulls, excludeModifiers ...)
- @Expose annotation for managing fields exclusion
Package content
- ./Pson.class.php : Pson lib class
- ./addendum/ : Addendum lib used by Pson
- ./index.php : Pson usage sample
Usage
<?php
//-- Include Pson Lib
require ' Pson/src/Mikangali/Pson/Pson.php';
//-- Simple PHP modele class
class User{
private $_nom; //-- private field
public $_prenom;
/**
* @FieldClass('Voiture')
*/
private $_voiture; //-- complex type field
}
class Voiture {
public $_modele;
public $_prix;
}
//-- json string
$json = '{"_nom":"mike","_prenom":"brandon","_voiture":{"_modele":"Audi A4","_prix":20000}}';
$pson = new Pson();
$user = $pson->fromJson($json,'User');
//-- #1
echo "<pre>"; print_r($user);
//-- #2
echo "<pre>"; print_r($pson->toJson($user));
Ouput
//-- #1
User Object
(
[_nom:User:private] => mike
[_prenom] => phoenix
[_user] =>
[_voiture:User:private] => Voiture Object
(
[_modele] => Audi A4
[_prix] => 20000
)
)
//-- #2
{"_nom":"mike","_prenom":"phoenix","_voiture":{"_modele":"Audi A4","_prix":20000}}
Others samples :
(c) 2013 Mikangali Labs