darling / php-json-utilities
PHPJsonUtilities 库提供用于在 PHP 中处理 json 的类。
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-09-27 04:02:18 UTC
README
___ __ _____ __ __ ____ _ ___ __ _
/ _ \/ // / _ \__ / /__ ___ ___ / / / / /_(_) (_) /_(_)__ ___
/ ___/ _ / ___/ // (_-</ _ \/ _ \/ /_/ / __/ / / / __/ / -_|_-<
/_/ /_//_/_/ \___/___/\___/_//_/\____/\__/_/_/_/\__/_/\__/___/
A library for working with json in php
https://github.com/sevidmusic/PHPJsonUtilities
PHPJsonUtilities 库提供用于在 PHP 中处理 json
的类。
本库提供以下类:
\Darling\PHPJsonUtilities\classes\encoded\data\Json
这是一个 \Stringable
类型,可以用于将各种类型的值编码为有效的 json
。
\Darling\PHPJsonUtilities\classes\decoders\JsonDecoder
它提供了一个 decode()
方法,可以用于解码通过 \Darling\PHPJsonUtilities\classes\encoded\data\Json
实例编码为 json
的值。
概览
安装
composer require darling/php-json-utilities
编码和解码值的示例
<?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;
预期输出
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
更多示例
有关如何使用此库提供的类的更多示例,请参阅 集成测试,位于 tests/integration/
目录中。