adhocore / json-comment
轻量级PHP JSON注释剥离库
1.2.1
2022-10-02 11:22 UTC
Requires
- php: >=7.0
- ext-ctype: *
Requires (Dev)
- phpunit/phpunit: ^6.5 || ^7.5 || ^8.5
README
- 轻量级PHP JSON注释剥离库。
- 允许在JSON数据的任何形式中包含注释。
- 支持的注释:单行
//注释或多行/*注释*/。 - 还可以剥离数组或对象末尾的尾随逗号,例如
[1,2,,]=>[1,2]{"x":1,,}⇒{"x":1}
- 处理字符串表示法中的文本换行符(LF),以便我们可以有多行字符串
- 支持JSON字符串中的JSON字符串(参见问题#15和PR#16)
- 无依赖(无供应商膨胀)。
安装
composer require adhocore/json-comment
# for php5.6
composer require adhocore/json-comment:^0.2
用法
use Ahc\Json\Comment; // The JSON string! $someJsonText = '{"a":1, "b":2,// comment "c":3 /* inline comment */, // comment "d":/* also a comment */"d", /* creepy comment*/"e":2.3, /* multi line comment */ "f":"f1",}'; // OR $someJsonText = file_get_contents('...'); // Strip only! (new Comment)->strip($someJsonText); // Strip and decode! (new Comment)->decode($someJsonText); // You can pass args like in `json_decode` (new Comment)->decode($someJsonText, $assoc = true, $depth = 512, $options = JSON_BIGINT_AS_STRING); // Or you can use static alias of decode: Comment::parse($json, true); # Or use file directly Comment::parseFromFile('/path/to/file.json', true);
示例
此库可以解析的JSON示例
{
"name": "adhocore/json-comment",
"description": "JSON comment stripper library for PHP.
There is literal line break just above this line but that's okay",
"type":/* This is creepy comment */ "library",
"keywords": [
"json",
"comment",
// Single line comment, Notice the comma below:
"strip-comment",
],
"license": "MIT",
/*
* This is a multiline
* comment.
*/
"authors": [
{
"name": "Jitendra Adhikari",
"email": "jiten.adhikary@gmail.com",
},
],
"autoload": {
"psr-4": {
"Ahc\\Json\\": "src/",
},
},
"autoload-dev": {
"psr-4": {
"Ahc\\Json\\Test\\": "tests/",
},
},
"require": {
"php": ">=7.0",
"ext-ctype": "*",
},
"require-dev": {
"phpunit/phpunit": "^6.5 || ^7.5 || ^8.5",
},
"scripts": {
"test": "phpunit",
"echo": "echo '// This is not comment'",
"test:cov": "phpunit --coverage-text",
},
}