定制 seld/jsonlint 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

seld/jsonlint

最新稳定版本:1.12.0

Composer 安装命令:

composer require seld/jsonlint

包简介

JSON Linter

README 文档

README

Build Status

Usage

use Seld\JsonLint\JsonParser;

$parser = new JsonParser();

// returns null if it's valid json, or a ParsingException object.
$parser->lint($json);

// Call getMessage() on the exception object to get
// a well formatted error message error like this

// Parse error on line 2:
// ... "key": "value"    "numbers": [1, 2, 3]
// ----------------------^
// Expected one of: 'EOF', '}', ':', ',', ']'

// Call getDetails() on the exception to get more info.

// returns parsed json, like json_decode() does, but slower, throws
// exceptions on failure.
$parser->parse($json);

You can also pass additional flags to JsonParser::lint/parse that tweak the functionality:

  • JsonParser::DETECT_KEY_CONFLICTS throws an exception on duplicate keys.
  • JsonParser::ALLOW_DUPLICATE_KEYS collects duplicate keys. e.g. if you have two foo keys they will end up as foo and foo.2.
  • JsonParser::PARSE_TO_ASSOC parses to associative arrays instead of stdClass objects.
  • JsonParser::ALLOW_COMMENTS parses while allowing (and ignoring) inline // and multiline /* */ comments in the JSON document.
  • JsonParser::ALLOW_DUPLICATE_KEYS_TO_ARRAY collects duplicate keys. e.g. if you have two foo keys the foo key will become an object (or array in assoc mode) with all foo values accessible as an array in $result->foo->__duplicates__ (or $result['foo']['__duplicates__'] in assoc mode).
  • JsonParser::VALIDATE_UTF8_ENCODING validates that the whole input is well-formed UTF-8 (as defined by RFC 3629) before parsing, throwing an InvalidEncodingException that points at the first invalid byte otherwise.

Example:

$parser = new JsonParser;
try {
    $parser->parse(file_get_contents($jsonFile), JsonParser::DETECT_KEY_CONFLICTS);
} catch (DuplicateKeyException $e) {
    $details = $e->getDetails();
    echo 'Key '.$details['key'].' is a duplicate in '.$jsonFile.' at line '.$details['line'];
}

Validating UTF-8 encoding

The UTF-8 validation behind the VALIDATE_UTF8_ENCODING flag is also available as a standalone, reusable utility through the Seld\JsonLint\Utf8Validator class, should you need to validate UTF-8 encoding outside of JSON parsing:

use Seld\JsonLint\Utf8Validator;
use Seld\JsonLint\InvalidEncodingException;

try {
    // returns void on success, throws on the first invalid byte
    Utf8Validator::validate($string);
} catch (InvalidEncodingException $e) {
    // getMessage() describes the offending byte and its position
    echo $e->getMessage();
    // getDetails() returns that position information as an array
    $details = $e->getDetails();
}

Note: This library is meant to parse JSON while providing good error messages on failure. There is no way it can be as fast as php native json_decode().

It is recommended to parse with json_decode, and when it fails parse again with seld/jsonlint to get a proper error message back to the user. See for example how Composer uses this library:

Installation

For a quick install with Composer use:

composer require seld/jsonlint

JSON Lint can easily be used within another app if you have a PSR-4 autoloader, or it can be installed through Composer for use as a CLI util. Once installed via Composer you can run the following command to lint a json file or URL:

$ bin/jsonlint file.json

Requirements

  • PHP 5.3+
  • [optional] PHPUnit 3.5+ to execute the test suite (phpunit --version)

Submitting bugs and feature requests

Bugs and feature request are tracked on GitHub

Author

Jordi Boggiano - j.boggiano@seld.be - http://twitter.com/seldaek

License

JSON Lint is licensed under the MIT License - see the LICENSE file for details

Acknowledgements

This library is a port of the JavaScript jsonlint library.

seld/jsonlint 适用场景与选型建议

seld/jsonlint 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 226.15M 次下载、GitHub Stars 达 1.32k, 最近一次更新时间为 2011 年 12 月 13 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「json」 「parser」 「linter」 「validator」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 seld/jsonlint 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 seld/jsonlint 我们能提供哪些服务?
定制开发 / 二次开发

基于 seld/jsonlint 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 226.15M
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1348
  • 点击次数: 21
  • 依赖项目数: 217
  • 推荐数: 5

GitHub 信息

  • Stars: 1324
  • Watchers: 22
  • Forks: 59
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2011-12-13