Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/adhocore/php-json-comment
Lightweight JSON comment and trailing comma stripper library for PHP with support for literal newlines and nested JSON strings.
https://github.com/adhocore/php-json-comment
adhocore comment hacktoberfest human-json json json-comment json5 json5-parser php strip-comment
Last synced: 13 days ago
JSON representation
Lightweight JSON comment and trailing comma stripper library for PHP with support for literal newlines and nested JSON strings.
- Host: GitHub
- URL: https://github.com/adhocore/php-json-comment
- Owner: adhocore
- License: mit
- Created: 2017-08-12T14:24:40.000Z (over 7 years ago)
- Default Branch: main
- Last Pushed: 2023-10-11T12:29:01.000Z (about 1 year ago)
- Last Synced: 2024-10-17T07:38:01.125Z (27 days ago)
- Topics: adhocore, comment, hacktoberfest, human-json, json, json-comment, json5, json5-parser, php, strip-comment
- Language: PHP
- Homepage:
- Size: 67.4 KB
- Stars: 31
- Watchers: 2
- Forks: 6
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
## adhocore/json-comment
[![Latest Version](https://img.shields.io/github/release/adhocore/php-json-comment.svg?style=flat-square)](https://github.com/adhocore/php-json-comment/releases)
[![Travis Build](https://img.shields.io/travis/adhocore/php-json-comment/main.svg?style=flat-square)](https://travis-ci.org/adhocore/php-json-comment?branch=main)
[![Scrutinizer CI](https://img.shields.io/scrutinizer/g/adhocore/php-json-comment.svg?style=flat-square)](https://scrutinizer-ci.com/g/adhocore/php-json-comment/?branch=main)
[![Codecov branch](https://img.shields.io/codecov/c/github/adhocore/php-json-comment/main.svg?style=flat-square)](https://codecov.io/gh/adhocore/php-json-comment)
[![StyleCI](https://styleci.io/repos/100117199/shield)](https://styleci.io/repos/100117199)
[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE)
[![Tweet](https://img.shields.io/twitter/url/http/shields.io.svg?style=social)](https://twitter.com/intent/tweet?text=Flexible+JSON+parser+with+comments+and+newline+support+in+PHP&url=https://github.com/adhocore/php-json-comment&hashtags=php,json,flexible-json,json-comment)
[![Support](https://img.shields.io/static/v1?label=Support&message=%E2%9D%A4&logo=GitHub)](https://github.com/sponsors/adhocore)- Lightweight JSON comment stripper library for PHP.
- Makes possible to have comment in any form of JSON data.
- Supported comments: single line `// comment` or multi line `/* comment */`.
- Also strips trailing comma at the end of array or object, eg:
- `[1,2,,]` => `[1,2]`
- `{"x":1,,}` => `{"x":1}`
- Handles literal LF (newline/linefeed) within string notation so that we can have multiline string
- Supports JSON string inside JSON string (see ticket [#15](https://github.com/adhocore/php-json-comment/issues/15) and PR [#16](https://github.com/adhocore/php-json-comment/pull/16))
- Zero dependency (no vendor bloat).## Installation
```bash
composer require adhocore/json-comment# for php5.6
composer require adhocore/json-comment:^0.2
```## Usage
```php
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);
```### Example
An example JSON that this library can parse:
```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": "[email protected]",
},
],
"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",
},
}
```