2017 © Pedro Peláez
 

library pson

Convert an Object into its JSON representation and vice versa

image

mikangali/pson

Convert an Object into its JSON representation and vice versa

  • Friday, November 15, 2013
  • by ElieSauveterre
  • Repository
  • 2 Watchers
  • 4 Stars
  • 1 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

Pson

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., (*1)

Version : 1.0 Require : PHP 5.4 +, (*2)


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 :

Pson + Twitter Api, (*3)

(c) 2013 Mikangali Labs, (*4)

The Versions

15/11 2013

dev-master

9999999-dev https://github.com/mikangali-labs/Pson

Convert an Object into its JSON representation and vice versa

  Sources   Download

GNU Lesser General Public License

The Requires

  • php >=5.4.0

 

json