PHP Classes

How to Use a PHP JSON to String Conversion Serializer and Object Hydrator with the Package PHP JSON Handler: Process value objects to convert to JSON format

Recommend this page to a friend!
  Info   Documentation   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2024-08-28 (Yesterday) RSS 2.0 feedNot yet rated by the usersTotal: 37 This week: 8All time: 10,946 This week: 7Up
Version License PHP version Categories
json-handler-php 1.0.1Custom (specified...8Data types, Language, Traits, PHP 8
Description 

Author

This package can process value objects to convert to JSON format.

It provides classes and traits that can perform several operations with value objects.

There is a hydrate trait using PHP reflection to assign the value object class variables with values from a JSON string.

There is also a handler class that can encode arrays as JSON strings and decode JSON strings to assign arrays.

Picture of Andrey Postal
  Performance   Level  
Name: Andrey Postal <contact>
Classes: 2 packages by
Country: Brazil Brazil

Documentation

Json Handler

Coverage Status Maintainability

Just a light and simple JSON helper that will make it easy for you to deal with json and objects.

Installation

composer require andreypostal/json-handler-php

Usage

Classes

When creating your Value Objects that represent a JSON entity you just need to add the `JsonItemAttribute` to each property that will be present in the JSON.

use \Andrey\JsonHandler\Attributes\JsonItemAttribute;

// { "id": 123, "name": "my name" }
class MyObject {
    #[JsonItemAttribute]
    public int $id;
    #[JsonItemAttribute]
    public name $name;
}

In the case of the entire object being a JsonObject with a direct 1:1 match (or perfect mirror of the keys), you can use the `JsonObjectAttribute`

use \Andrey\JsonHandler\Attributes\JsonObjectAttribute;

// { "id": 123, "name": "my name" }
#[JsonObjectAttribute]
class MyObject {
    public int $id;
    public string $name;
}

You can also combine both when need to add custom key or if you want to make an item required.

use \Andrey\JsonHandler\Attributes\JsonObjectAttribute;
use \Andrey\JsonHandler\Attributes\JsonItemAttribute;

// { "id": 123, "custom_name": "my name" }
#[JsonObjectAttribute]
class MyObject {
    public int $id;
    #[JsonItemAttribute(key: 'custom_name')]
    public string $name;
}

If your Value Object has some property that won't be present in the JSON, you can just omit the attribute for it and the other ones will be processed normally.

use \Andrey\JsonHandler\Attributes\JsonItemAttribute;

// { "id": 123 }
class MyObject {
    #[JsonItemAttribute]
    public int $id;
    public int $myAppGeneratesIt;
}

In case the items are required to exist in the JSON being processed, you must add the required flag in the attribute.

use \Andrey\JsonHandler\Attributes\JsonItemAttribute;

// { "id": 123 } or { "id": 123, "name": "my name" }
class MyObject {
    #[JsonItemAttribute(required: true)]
    public int $id;
    #[JsonItemAttribute]
    public string $name;
}

When some of the keys in your JSON are different from your object, you can include the JSON key in the attribute.

use \Andrey\JsonHandler\Attributes\JsonItemAttribute;

// { "customer_name": "the customer name" }
class MyObject {
    #[JsonItemAttribute(key: 'customer_name')]
    public string $name;
}

Also, if you have a property that is an array of other object, you must inform the class in the attribute using the `type` option. This will work as a hint so the hydrator can instantiate the appropriate object. This works with enums as well.

use \Andrey\JsonHandler\JsonItemAttribute;
use \MyNamespace\MyOtherObj;

// { "list": [ { "key": "value" } ] }
class MyObject {
    / @var MyOtherObj[] */
    #[JsonItemAttribute(type: MyOtherObj::class)]
    public array $list;
}

The type option can be used to validate that all the items in an array have some desired type as well, like "string", "integer"...

Handler

In order to utilize the definitions mentioned above, you must utilize the `JsonHandler`. Two traits are available as well, the `JsonHydratorTraitandJsonSerializerTrait` that provide the methods both for serialization and hydration.

use \Andrey\JsonHandler\JsonHandler;
use \MyNamespace\MyObject;

$handler = new JsonHandler();

$myObject = new MyObject();

// This parses the json string and hydrates the original object, modifying it
$handler->hydrateObject($jsonString, $myObject);

// If you don't want to modify the original object you can use the immutable hydration
$hydratedObject = $handler->hydrateObjectImmutable($jsonString, $myObject);

// You can also use an array to hydrate the object
$handler->hydrateObject($jsonArr, $myObject);

// And to fetch the information as an array you can just serialize it using the handler.
// This allows you to easily implement the JsonSerializable interface in your object.
$arr = $handler->serialize($myObject);

// The json handler also provides the methods to decode and encode
$jsonString = JsonHandler::Encode($arr);
$jsonArr = JsonHandler::Decode($jsonString);

  Files folder image Files (23)  
File Role Description
Files folder image.github (1 directory)
Files folder imagesrc (3 files, 1 directory)
Files folder imagetests (3 files, 1 directory)
Accessible without login Plain text file .editorconfig Data Auxiliary data
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file phpunit.xml.dist Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files (23)  /  .github  
File Role Description
Files folder imageworkflows (1 file)

  Files folder image Files (23)  /  .github  /  workflows  
File Role Description
  Accessible without login Plain text file tests.yml Data Auxiliary data

  Files folder image Files (23)  /  src  
File Role Description
Files folder imageAttributes (2 files)
  Plain text file JsonHandler.php Class Class source
  Plain text file JsonHydratorTrait.php Class Class source
  Plain text file JsonSerializerTrait.php Class Class source

  Files folder image Files (23)  /  src  /  Attributes  
File Role Description
  Plain text file JsonItemAttribute.php Class Class source
  Plain text file JsonObjectAttribute.php Class Class source

  Files folder image Files (23)  /  tests  
File Role Description
Files folder imageutils (9 files)
  Accessible without login Plain text file bootstrap.php Aux. Configuration script
  Plain text file HydratorTest.php Class Class source
  Plain text file SerializerTest.php Class Class source

  Files folder image Files (23)  /  tests  /  utils  
File Role Description
  Accessible without login Plain text file EnumObject.php Aux. Configuration script
  Plain text file MixedAttributesObject.php Class Class source
  Plain text file SimpleTestObject.php Class Class source
  Plain text file SimpleTestWithArrayObject.php Class Class source
  Plain text file SimpleTestWithObjectAttr.php Class Class source
  Plain text file WithArrayOfChildObject.php Class Class source
  Plain text file WithArrayOfEnumObject.php Class Class source
  Plain text file WithChildObject.php Class Class source
  Plain text file WithEnumObject.php Class Class source

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:37
This week:8
All time:10,946
This week:7Up