darling / php-json-utilities
The PHPJsonUtilities library provides classes for working with json in php.
v1.2.2
2023-10-25 23:40 UTC
Requires
- php: ^8.1
- darling/php-darling-dev-tools: ^1.0
- darling/php-mocking-utilities: ^1.0
- darling/php-reflection-utilities: ^1.0
- darling/php-text-types: ^1.0
- darling/php-unit-test-utilities: ^1.0
Requires (Dev)
- dev-main
- v1.2.2
- v1.2.1
- v1.2.0
- v1.1.9
- v1.1.8
- v1.1.7
- v1.1.6
- v1.1.5
- v1.1.4
- v1.1.3
- v1.1.2
- v1.1.1
- v1.1.0
- v1.0.9
- v1.0.8
- v1.0.7
- v1.0.6
- v1.0.5
- v1.0.4
- v1.0.3
- v1.0.2
- v1.0.1
- v1.0.0
- v0.0.9-alpha
- v0.0.8-alpha
- v0.0.7-alpha
- v0.0.6-alpha
- v0.0.5-alpha
- v0.0.4-alpha
- v0.0.3-alpha
- v0.0.2-alpha
- v0.0.1-alpha
- v0.0.0-alpha
- dev-PHPJsonUtilities1706281326
This package is auto-updated.
Last update: 2024-10-27 04:13:08 UTC
README
___ __ _____ __ __ ____ _ ___ __ _
/ _ \/ // / _ \__ / /__ ___ ___ / / / / /_(_) (_) /_(_)__ ___
/ ___/ _ / ___/ // (_-</ _ \/ _ \/ /_/ / __/ / / / __/ / -_|_-<
/_/ /_//_/_/ \___/___/\___/_//_/\____/\__/_/_/_/\__/_/\__/___/
A library for working with json in php
https://github.com/sevidmusic/PHPJsonUtilities
The PHPJsonUtilities library provides classes for working with
json
in php.
The following classes are provided by this library:
\Darling\PHPJsonUtilities\classes\encoded\data\Json
Which is a \Stringable
type that can be used to encode values of
various types as valid json
.
\Darling\PHPJsonUtilities\classes\decoders\JsonDecoder
Which provides a decode()
method that can be used
to decode values that were encoded as json
via a
\Darling\PHPJsonUtilities\classes\encoded\data\Json
instance.
Overview
Installation
composer require darling/php-json-utilities
Example of encoding and decoding a value
<?php
include(__DIR__ . DIRECTORY_SEPARATOR . 'vendor/autoload.php');
use \Darling\PHPJsonUtilities\classes\decoders\JsonDecoder;
use \Darling\PHPJsonUtilities\classes\encoded\data\Json;
class Foo {
/**
* Example class.
*
* @param int $int,
* @param bool $bool,
* @param string $string,
* @param float $float,
* @param array<mixed> $array,
* @param Json $json
*
*/
public function __construct(
private int $int,
private bool $bool,
protected string $string,
public float $float,
public array $array,
public Json $json
) {}
public function int():int {
return $this->int;
}
public function bool(): bool {
return $this->bool;
}
public function string(): string {
return $this->string;
}
}
$value = new Foo(
rand(0, 100),
boolval(rand(0, 1)),
strval(json_encode(str_shuffle('abcdefg'))),
floatval(strval(rand(1, 100)) . strval(rand(1, 100))),
[
rand(1, 100),
[
'string' => str_shuffle('abcdefg'),
],
],
new Json(new Json(json_encode(str_shuffle('absdefg'))))
);
$json = new Json($value);
$jsonDecoder = new JsonDecoder();
/** @var Foo $decodedValue */
$decodedValue = $jsonDecoder->decode($json);
echo 'types match' . PHP_EOL;
echo ($value::class == $decodedValue::class ? 'true' : 'false') . PHP_EOL;
/**
* == is used to compare $value with $decodedValue because we just
* need verify object equality, i.e. equal type and property values,
* not instance equality.
*/
echo 'object are equal in terms of type and property values.' . PHP_EOL;
echo ($value == $decodedValue ? 'true' : 'false') . PHP_EOL;
echo 'float property values match' . PHP_EOL;
echo ($value->float === $decodedValue->float ? 'true' : 'false') . PHP_EOL;
echo 'array property values match' . PHP_EOL;
echo ($value->array === $decodedValue->array ? 'true' : 'false') . PHP_EOL;
/**
* When checking properties that accept an object instance,
* == is used to compare original property value with the
* decoded property value because we just need verify object
* equality, i.e. equal type and property values, not instance
* equality.
*/
echo 'json property values match in terms of object equality.' . PHP_EOL;
echo ($value->json == $decodedValue->json ? 'true' : 'false') . PHP_EOL;
echo 'int property values match' . PHP_EOL;
echo ($value->int() === $decodedValue->int() ? 'true' : 'false') . PHP_EOL;
echo 'bool property values match' . PHP_EOL;
echo ($value->bool() === $decodedValue->bool() ? 'true' : 'false') . PHP_EOL;
echo 'string property values match' . PHP_EOL;
echo ($value->string() === $decodedValue->string() ? 'true' : 'false') . PHP_EOL;
Expected Output
types match
true
object are equal in terms of type and property values.
true
float property values match
true
array property values match
true
json property values match in terms of object equality.
true
int property values match
true
bool property values match
true
string property values match
true
More Examples
See the
integration tests
defined in the tests/integration/
directory for more examples of how
to use the classes provided by this library.