2017 © Pedro Peláez
 

library json

A JSON encoder and decoder that can also convert PHP objects with private or protected attributes.

image

phputil/json

A JSON encoder and decoder that can also convert PHP objects with private or protected attributes.

  • Saturday, January 27, 2018
  • by thiagodp
  • Repository
  • 2 Watchers
  • 4 Stars
  • 290 Installations
  • PHP
  • 1 Dependents
  • 1 Suggesters
  • 2 Forks
  • 0 Open issues
  • 9 Versions
  • 7 % Grown

The README.md

JSON

A JSON encoder and decoder that can also convert PHP objects with private or protected attributes., (*1)

Build Status, (*2)

We use semantic versioning. See our releases., (*3)

Classes

Installation

composer require phputil/json

Depends only on phputil\RTTI. Requires PHP >= 5.4., (*4)

Example 1

Converting an object with private or protected attributes., (*5)

a; }
    function getB() { return $this->b; }
    function getC() { return $this->c; }
}

echo JSON::encode( new Foo() ); // { "a": 1, "b": 2, "c": 3 }
?>

Example 2

Converting a dynamic object., (*6)

$obj = new stdClass();
$obj->name = 'Suzan';
$obj->age = 21;

echo JSON::encode( $obj ); // { "name": "Suzan", "age": 21 }

Example 3

Converting an array of dynamic objects to JSON and back again., (*7)

$obj1 = new stdClass();
$obj1->name = 'Bob';

$obj2 = new stdClass();
$obj2->name = 'Suzan';
$obj2->age = 21;

$json = JSON::encode( array( $obj1, $obj2 ) );
echo $json; // [ { "name": "Bob" }, { "name": "Suzan", "age": 21 } ]

$array = JSON::decode( $json );
var_dump( $array ); // array with the two PHP dynamic objects 

Example 4

Converting attributes from classes that use the __call magic method., (*8)

class Foo {
    private $a = 1;
    protected $b = 2;
    public $c = 3;

    function __call( $name, $args ) {
        if ( 'getA' === $name ) { return $this->a; }
        if ( 'getB' === $name ) { return $this->b; }
        if ( 'getC' === $name ) { return $this->c; }
    }
}

echo JSON::encode( new Foo() ); // { "a": 1, "b": 2, "c": 3 }

Example 5

Ignoring NULL values in objects' attributes or array values., (*9)

$arr = array( 'name' => 'Bob', 'phone' => null, 'age' => 21 ); // phone is null
// true as the third argument makes encode() to ignore null values
echo JSON::encode( $arr, 'get', true ); // { "name": "Bob", "age": 21 }

Example 6

Using value conversors. A value conversor is a function to convert values of a certain type correctly. For example, suppose that you need to convert values of the type DateTime to the format year-month-day. All you need is to register the type and a function to convert its values, using the static method addConversion:, (*10)

JSON::addConversion( 'DateTime', function( $value ) {
    return $value->format( 'Y-m-d' ); // year-month-day
} );

$obj = new stdClass();
$obj->user = 'bob';
$obj->birthdate = new DateTime( "12/31/1980" ); // month/day/year

echo JSON::encode( $obj ); // { "user": "bob", "birthdate": "1980-12-31" }

License

MIT (c) Thiago Delgado Pinto, (*11)

The Versions

27/01 2018

dev-master

9999999-dev http://github.com/thiagodp/json

A JSON encoder and decoder that can also convert PHP objects with private or protected attributes.

  Sources   Download

MIT LGPL-3

The Requires

 

php json object encode decode private protected

27/01 2018

1.3.1

1.3.1.0 http://github.com/thiagodp/json

A JSON encoder and decoder that can also convert PHP objects with private or protected attributes.

  Sources   Download

MIT

The Requires

 

php json object encode decode private protected

11/11 2016

1.3

1.3.0.0 http://github.com/thiagodp/json

A JSON encoder and decoder that can also convert PHP objects with private or protected attributes.

  Sources   Download

LGPL-3

The Requires

 

php json object encode decode private protected

11/08 2016

1.2.2

1.2.2.0 http://github.com/thiagodp/json

A JSON encoder and decoder that can also convert PHP objects with private or protected attributes.

  Sources   Download

LGPL-3

The Requires

 

The Development Requires

php json object encode decode private protected

11/08 2016

1.2.1

1.2.1.0 http://github.com/thiagodp/json

A JSON encoder and decoder that can also convert PHP objects with private or protected attributes.

  Sources   Download

LGPL-3

The Requires

 

The Development Requires

php json object encode decode private protected

10/03 2016

1.2

1.2.0.0 http://github.com/thiagodp/json

A JSON encoder and decoder that can also convert PHP objects with private or protected attributes.

  Sources   Download

LGPL-3

The Requires

 

The Development Requires

php json object encode decode private protected

29/10 2015

1.1

1.1.0.0 http://github.com/thiagodp/json

A JSON encoder and decoder that can also convert PHP objects with private or protected attributes.

  Sources   Download

LGPL-3

The Requires

 

The Development Requires

php json object encode decode private protected

23/10 2015

1.0.1

1.0.1.0 http://github.com/thiagodp/json

A JSON encoder and decoder that can also convert PHP objects with private or protected attributes.

  Sources   Download

LGPL-3

The Requires

 

php json object encode decode private protected

23/10 2015

1.0

1.0.0.0 http://github.com/thiagodp/json

A JSON encoder and decoder that can also convert PHP objects with private or protected attributes.

  Sources   Download

LGPL-3

The Requires

 

php json object encode decode private protected