cse/helpers-json

该助手允许您进行JSON处理。编码,解码,检查错误,抛出异常 - 所有这些都在这个库中可用。

1.1.2 2019-05-05 16:10 UTC

This package is auto-updated.

Last update: 2024-09-06 04:09:07 UTC


README

英语 | 俄语

JSON CSE 助手

Travis (.org) Codecov Scrutinizer code quality

Packagist Minimum PHP Version Packagist GitHub repo size

该助手允许您进行JSON处理。编码,解码,检查错误,抛出异常 - 所有这些都在这个库中可用。

项目仓库: https://github.com/cs-eliseev/helpers-json

DEMO

$json = [
    '{"example": 12345}',   // success
    "{'example': 12345}",   // Syntax error
    '{"example": 12345}',   // success
];

foreach ($json as $item) {
    try {
        Json::decode($item);
        Json::errorToException();
        var_dump('success');
    } catch (CSEHelpersJsonException $e) {
        var_dump($e->getMessage());
    }
}

简介

CSE 助手 是一系列简单的函数库集合,由 PHP 编写,供人们使用。

尽管 PHP 是互联网的主要编程语言,但其功能仍然不足。JSON CSE 助手使用的方法:编码,解码,检查错误,抛出异常。

CSE 助手 是为快速开发网络应用而创建的。

CSE 助手项目

下面将介绍如何初始化库和执行常见命令的一些信息。

安装

您可以在此处找到此项目的最新版本。

Composer

执行以下命令以获取该软件包的最新版本

composer require cse/helpers-json

或 composer.json 文件应包含以下内容

{
    "require": {
        "cse/helpers-json": "*"
    }
}

Git

将此仓库克隆到本地

git clone https://github.com/cs-eliseev/helpers-json.git

下载

在此处下载最新版本.

使用

该类由静态方法组成,方便在任何项目中使用。请参阅示例

JSON 编码

示例

Json::encode(['example' => 12345]);
// {"example": 12345}

设置检查异常

Json::setCheckException();
Json::encode([urldecode('bad utf string %C4_')]);
// Exception: Malformed UTF-8 characters, possibly incorrectly encoded

JSON 美化打印

示例

Json::prettyPrint(['example' => 12345, 'example2' => 56789]);
// {
//    "example": 12345,
//    "example": 56789
// }

设置检查异常

Json::setCheckException();
Json::prettyPrint([urldecode('bad utf string %C4_')]);
// Exception: Malformed UTF-8 characters, possibly incorrectly encoded

JSON 解码

示例

Json::decode('{"example": 12345}');
// ['example' => 12345]

设置检查异常

Json::setCheckException();
Json::decode("{'example': 12345}");
// Syntax error

获取 JSON 数据到键

示例

Json::get('{"example": 12345}', 'example');
// 12345

设置默认数据

Json::get('{"example": 12345}', 'example2', 56789);
// 56789

设置检查异常

Json::setCheckException();
Json::get("{'example': 12345}", 'example');
// Syntax error

设置 JSON 数据

示例

Json::set('{"example": 12345}', 'example2', 56789);
// {"example": 12345, "example2": 56789}

更改值

Json::set('{"example": 12345}', 'example', 56789);
// {"example": 56789}

设置检查异常

Json::setCheckException();
Json::set("{'example': 12345}", 'example2', 56789);
// Syntax error

设置数组 JSON

示例

Json::setArray('{"example": 12345}', ['example2' => 56789]);
// {"example": 12345, "example2": 56789}

更改值

Json::setArray('{"example": 12345}', ['example' => 56789]);
// {"example": 56789}

设置检查异常

Json::setCheckException();
Json::setArray("{'example': 12345}", ['example2' => 56789]);
// Syntax error

检查最后一个 JSON 转换的错误

示例

Json::decode('{"example": 12345}');
Json::isNoteError();
// true
Json::decode("{'example': 12345}");
Json::isNoteError();
// false

获取错误

示例

Json::decode('{"example": 12345}');
Json::getErrorMsg();
// NULL
Json::decode("{'example': 12345}");
Json::getErrorMsg();
// Syntax error

添加消息

Json::decode("{'example': 12345}");
Json::getErrorMsg('- Example');
// Syntax error - Example

错误转换为异常

示例

try {
    Json::decode("{'example': 12345}");
    Json::errorToException();
} catch (CSEHelpersJsonException $e) {
    var_dump($e->getMessage());
}
// Syntax error

添加消息

try {
    Json::decode("{'example': 12345}");
    Json::errorToException('(JSON)');
} catch (CSEHelpersJsonException $e) {
    var_dump($e->getMessage());
}
// Syntax error (JSON)

异常实例

try {
    Json::decode("{'example': 12345}");
    Json::errorToException('(JSON)');
} catch (CseExceptions $e) {
    var_dump($e->getMessage());
}
// Syntax error (JSON)

设置检查异常

示例

class Default
{
    public function example(): void
    {
        Json::encode('{"example": 12345}');
    }
}

class ExceptionTrue
{
    public function example(): void
    {
        Json::setCheckException();
        Json::encode("{'example': 12345}");
    }
}

class ExceptionFalse
{
    public function example(): void
    {
        Json::setCheckException(false);
        Json::encode("{'example': 12345}");
    }
}

$default = new Default();
$e_true = new ExceptionTrue();
$e_false = new ExceptionFalse();

try {
    $default->example();
} catch (CSEHelpersJsonException $e) {
    var_dump($e->getMessage());
}

try {
    $e_true->example();
} catch (CSEHelpersJsonException $e) {
    var_dump($e->getMessage());
}
// Syntax error

try {
    $default->example();
} catch (CSEHelpersJsonException $e) {
    var_dump($e->getMessage());
}
// Syntax error

try {
    $e_false->example();
} catch (CSEHelpersJsonException $e) {
    var_dump($e->getMessage());
}

try {
    $default->example();
} catch (CSEHelpersJsonException $e) {
    var_dump($e->getMessage());
}

测试与代码覆盖率

PHPUnit 用于单元测试。单元测试确保类和方法确实做了它应该做的事情。

PHPUnit 的一般文档可以在https://phpunit.de/documentation.html找到。

要运行 PHPUnit 单元测试,请执行以下操作

phpunit PATH/TO/PROJECT/tests/

如果您想要代码覆盖率报告,请使用以下命令

phpunit --coverage-html ./report PATH/TO/PROJECT/tests/

使用 PHPUnit 默认配置

phpunit --configuration PATH/TO/PROJECT/phpunit.xml

捐赠

您可以通过此处支持此项目。您也可以通过为项目做出贡献或报告错误来帮助。即使是提出关于功能的建议也是很好的。任何帮助都是非常受欢迎的。

许可

JSON CSE 助手是 MIT 许可的开源 PHP 库。请参阅许可文件以获取更多信息。

GitHub @cs-eliseev