cmpayments/jsonlint

PHP JSON Lint 检查字符串是否存在无效或格式错误的 JSON,控制字符错误,编码错误或简单的语法错误。它返回一个关于首次出现的错误的一行错误信息(一行信息特别针对 REST API)。

v0.0.5 2016-03-25 12:28 UTC

This package is auto-updated.

Last update: 2024-08-27 00:56:28 UTC


README

License Latest Stable Version Scrutinizer Code Quality Total Downloads Reference Status

PHP JSON Lint 检查字符串是否存在无效或格式错误的 JSON,控制字符错误,编码错误或简单的语法错误。它返回一个关于首次出现的错误的一行错误信息(一行信息特别针对 REST API)。它支持 RFC 4627 及其(替代)RFC 7159。

用法

use CMPayments\JsonLint\JsonLinter;

$linter = new JsonLinter();

// example JSON
$json = '{
    "id": 2,
    "name": "JSON Parser test",
    \'dimensions\': {
        "length": 7.0,
        "width": 12.0,
        "height": 9.5
    }
}';

// returns null if it's valid json, or a ParseException object.
$result = $linter->lint($json);

// Call $result->getMessage() on the ParseException object to get a well formatted error message error like this:
// You used single quotes instead of double quotes on string 'dimensions' on line 4 at column 9. It appears you have an extra trailing comma

// Call $result->getDetails() on the exception to get more info.
$result->getDetails();

// $result->getDetails() returns;
array (size=6)
  'errorCode' => int 3
  'match' => string 'dimensions' (length=10)
  'token' => string 'INVALID' (length=7)
  'line' => int 3
  'column' => int 9
  'expected' =>
    array (size=1)
      0 => string 'STRING' (length=6)

安装

使用 Composer 快速安装:

$ composer require cmpayments/jsonlint

如果有一个 PSR-4 自动加载器,PHP JSON Lint 可以轻松地在另一个应用中使用,或者可以通过 Composer 安装。

要求

  • PHP 5.4+
  • [可选] PHPUnit 3.5+ 执行测试套件(phpunit --version)

提交错误和功能请求

错误和功能请求在 GitHub 上跟踪

待办事项

  • 添加对不带双引号键的支持*

*RFC 4627 表示对象是一个零个或多个键/值对的未排序集合,其中键是一个字符串,值是一个字符串、数字、布尔值、null、对象或数组。这个 JSON 检查器通常对不带引号的键更加宽容,以便接受来自 JavaScript 前端的请求。

作者

Boy Wijnmaalen - boy.wijnmaalen@cmtelecom.com - https://twitter.com/boywijnmaalen

许可

JSON Lint 在 MIT 许可证下发布 - 有关详细信息,请参阅 LICENSE 文件

致谢

此库基于 Seldaek/jsonlintzaach/jsonlint 构建。